diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index a1e6329f4..9a87d2f80 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -14,9 +14,19 @@ name: "CodeQL" on: push: branches: [ main, develop, release/* ] + paths: + - '**/*.js' + - '**/*.cs' + - '**/*.cshtml' + - '**/*.csproj' pull_request: # The branches below must be a subset of the branches above branches: [ main, develop ] + paths: + - '**/*.js' + - '**/*.cs' + - '**/*.cshtml' + - '**/*.csproj' jobs: analyze: diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index ed8e5a977..035d082be 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -4,8 +4,13 @@ on: workflow_dispatch: inputs: manualTag: - description: 'tag' + description: 'Manual Tag' required: true + manualWithLatestTag: + description: 'Manual With Latest Tag' + required: true + default: true + type: boolean release: types: [published] @@ -77,7 +82,7 @@ jobs: run: | DOCKER_IMAGE_TAG="zai7lou/bilibili_tool_pro:${{ steps.getRepoTag.outputs.repoTag }}" DOCKER_IMAGE_LATEST_TAG=",zai7lou/bilibili_tool_pro:latest" - if [ "${{ github.event.inputs.manualTag }}" ] ; then + if [ "${{ github.event.inputs.manualTag }}" ] && [ "${{ github.event.inputs.manualWithLatestTag }}" == "false" ] ; then DOCKER_IMAGE_LATEST_TAG="" fi DOCKER_IMAGE_TAGS=$DOCKER_IMAGE_TAG$DOCKER_IMAGE_LATEST_TAG diff --git a/CHANGELOG.md b/CHANGELOG.md index 9937bb391..b813b341e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,3 +29,8 @@ - 【#27】更新README ## 0.0.9 - 【#47】青龙安装dotnet环境,支持arm架构服务器 +## 0.1.0 +- 【#62】codeql-analysis.yml可以指定检查的文件类型 +- 【#61】publish-image.yml手动打镜像时支持指定是否打latest的tag +- 【#32】新增企业微信的应用推送,实现微信接受推送消息 +- 优化日志格式 diff --git a/Dockerfile b/Dockerfile index 2bd2d2c71..11daee9d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,8 @@ COPY ["src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.OtherApiBatched/Ray.Serilog.Sinks COPY ["src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.DingTalkBatched/Ray.Serilog.Sinks.DingTalkBatched.csproj", "src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.DingTalkBatched/"] COPY ["src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.PushPlusBatched/Ray.Serilog.Sinks.PushPlusBatched.csproj", "src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.PushPlusBatched/"] COPY ["src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.ServerChanBatched/Ray.Serilog.Sinks.ServerChanBatched.csproj", "src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.ServerChanBatched/"] +COPY ["src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/Ray.Serilog.Sinks.MicrosoftTeamsBatched.csproj", "src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/"] +COPY ["src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/Ray.Serilog.Sinks.WorkWeiXinAppBatched.csproj", "src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/"] RUN dotnet restore "src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj" COPY . . WORKDIR "/code/src/Ray.BiliBiliTool.Console" diff --git a/README.md b/README.md index 0dcd8a20d..a79e1e0af 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ Ray.BiliBiliTool.Console ![微信推送图示](docs/imgs/wechat-push.png) -目前默认支持**PushPlus推送、企业微信推送、钉钉推送、Telegram推送、Server酱推送和酷推QQ推送**(以上顺序即为个人推荐的排序),如果需要推送到其他端,也可以配置为任意的可以接受消息的Api地址,关于如何配置推送请详见下面的**个性化自定义配置**章节。 +目前默认支持**PushPlus推送、企业微信应用推送、企业微信推送、钉钉推送、Microsoft Teams推送、Telegram推送、Server酱推送和酷推QQ推送**(以上顺序即为个人推荐的排序),如果需要推送到其他端,也可以配置为任意的可以接受消息的Api地址,关于如何配置推送请详见下面的**个性化自定义配置**章节。 ## 2. 功能任务说明 这里的**任务**是指一组功能的集合,是工具每次运行的最小单位。工具启动后会根据我们输入的任务编码,运行相应的任务,如: diff --git a/Ray.BiliBiliTool.sln b/Ray.BiliBiliTool.sln index 41fd70ae3..203bb3b25 100644 --- a/Ray.BiliBiliTool.sln +++ b/Ray.BiliBiliTool.sln @@ -148,7 +148,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{75A9CC5C docker\build\buildImage_arm64.cmd = docker\build\buildImage_arm64.cmd EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ray.Serilog.Sinks.MicrosoftTeams", "src\Ray.Serilog.Sinks\Ray.Serilog.Sinks.MicrosoftTeams\Ray.Serilog.Sinks.MicrosoftTeams.csproj", "{F249A822-EFD3-4F0A-9C10-95A96676D61A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ray.Serilog.Sinks.WorkWeiXinAppBatched", "src\Ray.Serilog.Sinks\Ray.Serilog.Sinks.WorkWeiXinAppBatched\Ray.Serilog.Sinks.WorkWeiXinAppBatched.csproj", "{099C4E10-1114-495C-A664-63A15E77B31A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ray.Serilog.Sinks.MicrosoftTeamsBatched", "src\Ray.Serilog.Sinks\Ray.Serilog.Sinks.MicrosoftTeamsBatched\Ray.Serilog.Sinks.MicrosoftTeamsBatched.csproj", "{FB9A43DE-00F0-42C4-BF92-AF61D752CCA2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -228,10 +230,14 @@ Global {F6B8ED3A-5428-4D26-8172-8B41FBF0C4CF}.Debug|Any CPU.Build.0 = Debug|Any CPU {F6B8ED3A-5428-4D26-8172-8B41FBF0C4CF}.Release|Any CPU.ActiveCfg = Release|Any CPU {F6B8ED3A-5428-4D26-8172-8B41FBF0C4CF}.Release|Any CPU.Build.0 = Release|Any CPU - {F249A822-EFD3-4F0A-9C10-95A96676D61A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F249A822-EFD3-4F0A-9C10-95A96676D61A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F249A822-EFD3-4F0A-9C10-95A96676D61A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F249A822-EFD3-4F0A-9C10-95A96676D61A}.Release|Any CPU.Build.0 = Release|Any CPU + {099C4E10-1114-495C-A664-63A15E77B31A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {099C4E10-1114-495C-A664-63A15E77B31A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {099C4E10-1114-495C-A664-63A15E77B31A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {099C4E10-1114-495C-A664-63A15E77B31A}.Release|Any CPU.Build.0 = Release|Any CPU + {FB9A43DE-00F0-42C4-BF92-AF61D752CCA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB9A43DE-00F0-42C4-BF92-AF61D752CCA2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB9A43DE-00F0-42C4-BF92-AF61D752CCA2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB9A43DE-00F0-42C4-BF92-AF61D752CCA2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -269,7 +275,8 @@ Global {830361B7-BCC1-4853-879A-761B0FD86826} = {73DD457B-E06E-45ED-A6BA-7E3C02F8BDF1} {F6B8ED3A-5428-4D26-8172-8B41FBF0C4CF} = {E9BDDCBE-A57D-4E3B-8252-708088386ADF} {75A9CC5C-DF92-4D72-A14C-625AA902855B} = {A93210FD-27B6-40E4-B08D-391F96CA2754} - {F249A822-EFD3-4F0A-9C10-95A96676D61A} = {4BAFC980-7A73-45C3-9460-8B8CCB87939B} + {099C4E10-1114-495C-A664-63A15E77B31A} = {4BAFC980-7A73-45C3-9460-8B8CCB87939B} + {FB9A43DE-00F0-42C4-BF92-AF61D752CCA2} = {4BAFC980-7A73-45C3-9460-8B8CCB87939B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {197319DA-1148-4A99-847C-8B270B6A29AB} diff --git a/common.props b/common.props index 590025d77..eaec7b658 100644 --- a/common.props +++ b/common.props @@ -1,7 +1,7 @@ Ray - 0.0.9 + 0.1.0 $(NoWarn);CS1591;CS0436 diff --git a/docs/configuration.md b/docs/configuration.md index e1be7ebe6..09e4559de 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -61,6 +61,10 @@ - [3.6.8.4. PushPlus的Webhook](#3684-pushplus的webhook) - [3.6.9. Microsoft Teams](#369-microsoft-teams) - [3.6.9.1. Microsoft Teams的Webhook](#3691-microsoft-teams的webhook) + - [3.6.10. 企业微信应用推送](#3610-企业微信应用推送) + - [3.6.10.1. 企业微信应用推送的corpId](#36101-企业微信应用推送的corpid) + - [3.6.10.2. 企业微信应用推送的agentId](#36102-企业微信应用推送的agentid) + - [3.6.10.3. 企业微信应用推送的secret](#36103-企业微信应用推送的secret) - [3.7. 日志相关](#37-日志相关) - [3.7.1. Console日志输出等级](#371-console日志输出等级) - [3.7.2. Console日志输出样式](#372-console日志输出样式) @@ -686,6 +690,51 @@ webhook的完整地址,在Teams的Channel中获取,详细获取方式请参 | 命令行示范 | | | GitHub Secrets | | + +#### 3.6.10. 企业微信应用推送 + +官网: https://developer.work.weixin.qq.com/tutorial/application-message + +当`corpId`、`agentId`、`secret`均不为空时,自动开启推送,否则关闭。 + +`toUser`、`toParty`、`toTag`3个配置非必填,但不可同时为空,默认`toUser`为`@all`,向所有用户推送。 + + +##### 3.6.10.1. 企业微信应用推送的corpId + +| TITLE | CONTENT | +| ---------- | -------------- | +| 配置Key | `Serilog:WriteTo:11:Args:corpId` | +| 值域 | 一串字符串 | +| 默认值 | 空 | +| 环境变量 | `Serilog__WriteTo__11__Args__corpId` | +| 命令行示范 | | +| GitHub Secrets | | + + +##### 3.6.10.2. 企业微信应用推送的agentId + +| TITLE | CONTENT | +| ---------- | -------------- | +| 配置Key | `Serilog:WriteTo:11:Args:agentId` | +| 值域 | 一串字符串 | +| 默认值 | 空 | +| 环境变量 | `Serilog__WriteTo__11__Args__agentId` | +| 命令行示范 | | +| GitHub Secrets | | + + +##### 3.6.10.3. 企业微信应用推送的secret + +| TITLE | CONTENT | +| ---------- | -------------- | +| 配置Key | `Serilog:WriteTo:11:Args:secret` | +| 值域 | 一串字符串 | +| 默认值 | 空 | +| 环境变量 | `Serilog__WriteTo__11__Args__secret` | +| 命令行示范 | | +| GitHub Secrets | | + ### 3.7. 日志相关 diff --git a/src/Ray.BiliBiliTool.Agent/Ray.BiliBiliTool.Agent.csproj b/src/Ray.BiliBiliTool.Agent/Ray.BiliBiliTool.Agent.csproj index 10d94607d..42789ea9c 100644 --- a/src/Ray.BiliBiliTool.Agent/Ray.BiliBiliTool.Agent.csproj +++ b/src/Ray.BiliBiliTool.Agent/Ray.BiliBiliTool.Agent.csproj @@ -5,11 +5,11 @@ - - - - - + + + + + diff --git a/src/Ray.BiliBiliTool.Application/Ray.BiliBiliTool.Application.csproj b/src/Ray.BiliBiliTool.Application/Ray.BiliBiliTool.Application.csproj index 425876e4b..9de70e91c 100644 --- a/src/Ray.BiliBiliTool.Application/Ray.BiliBiliTool.Application.csproj +++ b/src/Ray.BiliBiliTool.Application/Ray.BiliBiliTool.Application.csproj @@ -5,9 +5,9 @@ - - - + + + diff --git a/src/Ray.BiliBiliTool.Config/Ray.BiliBiliTool.Config.csproj b/src/Ray.BiliBiliTool.Config/Ray.BiliBiliTool.Config.csproj index 73297cbb2..ff4851aa8 100644 --- a/src/Ray.BiliBiliTool.Config/Ray.BiliBiliTool.Config.csproj +++ b/src/Ray.BiliBiliTool.Config/Ray.BiliBiliTool.Config.csproj @@ -5,12 +5,12 @@ - - - - - - + + + + + + diff --git a/src/Ray.BiliBiliTool.Console/BiliBiliToolHostedService.cs b/src/Ray.BiliBiliTool.Console/BiliBiliToolHostedService.cs index 1b46d75af..0ae6740a6 100644 --- a/src/Ray.BiliBiliTool.Console/BiliBiliToolHostedService.cs +++ b/src/Ray.BiliBiliTool.Console/BiliBiliToolHostedService.cs @@ -60,7 +60,7 @@ public Task StartAsync(CancellationToken cancellationToken) for (int i = 0; i < _cookieStrFactory.Count; i++) { _cookieStrFactory.CurrentNum = i + 1; - _logger.LogInformation("账号 {num} :" + Environment.NewLine, _cookieStrFactory.CurrentNum); + _logger.LogInformation("========= 账号 {num} =========" + Environment.NewLine, _cookieStrFactory.CurrentNum); try { @@ -155,13 +155,13 @@ private void DoTasks(string[] tasks) private void LogAppInfo() { _logger.LogInformation( - "{newLine}---------------------------------------{newLine} RayBiliBiliToolPro-v{version} in {env} env.{newLine}开源 by {url}", + "{newLine}========================{newLine} v{version} in {env} env.{newLine}开源 by {url}", Environment.NewLine + Environment.NewLine, Environment.NewLine + Environment.NewLine, typeof(Program).Assembly.GetCustomAttribute()?.InformationalVersion, Global.HostingEnvironment.EnvironmentName, Environment.NewLine, - Constants.SourceCodeUrl + Constants.SourceCodeUrl + Environment.NewLine ); //_logger.LogInformation("【当前IP】{ip} ", IpHelper.GetIp()); } diff --git a/src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj b/src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj index 2a525d2db..dae6aa66d 100644 --- a/src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj +++ b/src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj @@ -47,22 +47,22 @@ - - - - - - - - - + + + + + + + + + - - - - + + + + - + @@ -76,11 +76,12 @@ - + + diff --git a/src/Ray.BiliBiliTool.Console/appsettings.json b/src/Ray.BiliBiliTool.Console/appsettings.json index 149f00fd5..e2c3bda2e 100644 --- a/src/Ray.BiliBiliTool.Console/appsettings.json +++ b/src/Ray.BiliBiliTool.Console/appsettings.json @@ -61,7 +61,9 @@ "Ray.Serilog.Sinks.ServerChanBatched", "Ray.Serilog.Sinks.CoolPushBatched", "Ray.Serilog.Sinks.OtherApiBatched", - "Ray.Serilog.Sinks.PushPlusBatched" + "Ray.Serilog.Sinks.PushPlusBatched", + "Ray.Serilog.Sinks.MicrosoftTeamsBatched", + "Ray.Serilog.Sinks.WorkWeiXinAppBatched" ], "MinimumLevel": { "Default": "Debug", @@ -162,6 +164,19 @@ "webhook": "", //webhook完整地址 "restrictedToMinimumLevel": "Information" } + }, + //11.企业微信应用推送 + { + "Name": "WorkWeiXinAppBatched", + "Args": { + "corpId": "", //必填 + "agentId": "", //必填 + "secret": "", //必填 + "toUser": "@all", + "toParty": "", + "toTag": "", + "restrictedToMinimumLevel": "Information" + } } ], "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ] diff --git a/src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs b/src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs index 05ecc6e2c..6f34ee5ce 100644 --- a/src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs +++ b/src/Ray.BiliBiliTool.DomainService/AccountDomainService.cs @@ -72,7 +72,8 @@ public UserInfo LoginByCookie() } else { - _logger.LogInformation("【当前经验】{0} (您已是 Lv6 的大佬了,无敌是多么寂寞~)", useInfo.Level_info.Current_exp); + _logger.LogInformation("【当前经验】{0}", useInfo.Level_info.Current_exp); + _logger.LogInformation("您已是 Lv6 的大佬了,无敌是多么寂寞~"); } return useInfo; diff --git a/src/Ray.BiliBiliTool.DomainService/Ray.BiliBiliTool.DomainService.csproj b/src/Ray.BiliBiliTool.DomainService/Ray.BiliBiliTool.DomainService.csproj index efcd9fa06..675e9aafd 100644 --- a/src/Ray.BiliBiliTool.DomainService/Ray.BiliBiliTool.DomainService.csproj +++ b/src/Ray.BiliBiliTool.DomainService/Ray.BiliBiliTool.DomainService.csproj @@ -5,9 +5,9 @@ - - - + + + diff --git a/src/Ray.BiliBiliTool.Infrastructure/Ray.BiliBiliTool.Infrastructure.csproj b/src/Ray.BiliBiliTool.Infrastructure/Ray.BiliBiliTool.Infrastructure.csproj index 185359b4f..ea1bccbe6 100644 --- a/src/Ray.BiliBiliTool.Infrastructure/Ray.BiliBiliTool.Infrastructure.csproj +++ b/src/Ray.BiliBiliTool.Infrastructure/Ray.BiliBiliTool.Infrastructure.csproj @@ -5,9 +5,9 @@ - - - + + + \ No newline at end of file diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsApiClient.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsApiClient.cs similarity index 90% rename from src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsApiClient.cs rename to src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsApiClient.cs index 1865379c0..fec744259 100644 --- a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsApiClient.cs +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsApiClient.cs @@ -1,9 +1,7 @@ -using System; -using System.Net.Http; -using System.Text; +using System.Text; using Ray.Serilog.Sinks.Batched; -namespace Ray.Serilog.Sinks.MicrosoftTeams +namespace Ray.Serilog.Sinks.MicrosoftTeamsBatched { public class MicrosoftTeamsApiClient : PushService { diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsBatchedSink.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsBatchedSink.cs similarity index 91% rename from src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsBatchedSink.cs rename to src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsBatchedSink.cs index 1d7d00e67..f71ef6b7b 100644 --- a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsBatchedSink.cs +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsBatchedSink.cs @@ -1,8 +1,7 @@ -using System; -using Ray.Serilog.Sinks.Batched; +using Ray.Serilog.Sinks.Batched; using Serilog.Events; -namespace Ray.Serilog.Sinks.MicrosoftTeams +namespace Ray.Serilog.Sinks.MicrosoftTeamsBatched { public class MicrosoftTeamsBatchedSink : BatchedSink { diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsConfigurationExtensions.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsConfigurationExtensions.cs similarity index 94% rename from src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsConfigurationExtensions.cs rename to src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsConfigurationExtensions.cs index e85fd6fcf..3eb8dde5a 100644 --- a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsConfigurationExtensions.cs +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/MicrosoftTeamsConfigurationExtensions.cs @@ -1,10 +1,9 @@ -using System; -using Ray.Serilog.Sinks.Batched; +using Ray.Serilog.Sinks.Batched; using Serilog; using Serilog.Configuration; using Serilog.Events; -namespace Ray.Serilog.Sinks.MicrosoftTeams +namespace Ray.Serilog.Sinks.MicrosoftTeamsBatched { public static class MicrosoftTeamsConfigurationExtensions { diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/Ray.Serilog.Sinks.MicrosoftTeams.csproj b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/Ray.Serilog.Sinks.MicrosoftTeamsBatched.csproj similarity index 92% rename from src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/Ray.Serilog.Sinks.MicrosoftTeams.csproj rename to src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/Ray.Serilog.Sinks.MicrosoftTeamsBatched.csproj index 55eb04647..8c31e019d 100644 --- a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/Ray.Serilog.Sinks.MicrosoftTeams.csproj +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeamsBatched/Ray.Serilog.Sinks.MicrosoftTeamsBatched.csproj @@ -3,7 +3,6 @@ net6.0 enable - enable diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/Ray.Serilog.Sinks.WorkWeiXinAppBatched.csproj b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/Ray.Serilog.Sinks.WorkWeiXinAppBatched.csproj new file mode 100644 index 000000000..03f0eaef1 --- /dev/null +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/Ray.Serilog.Sinks.WorkWeiXinAppBatched.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + + + + + + + + diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppApiClient.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppApiClient.cs new file mode 100644 index 000000000..b5df26b41 --- /dev/null +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppApiClient.cs @@ -0,0 +1,92 @@ +using System.Text; +using Ray.Serilog.Sinks.Batched; + +namespace Ray.Serilog.Sinks.WorkWeiXinAppBatched +{ + public class WorkWeiXinAppApiClient : PushService + { + // https://developer.work.weixin.qq.com/tutorial/application-message + // https://developer.work.weixin.qq.com/document/34479 + // https://github.com/JeffreySu/WeiXinMPSDK + + private readonly Uri _apiUrl; + private readonly HttpClient _httpClient = new HttpClient(); + private readonly string _corpId; + private readonly string _agentId; + private readonly string _secret; + + private readonly string _toUser; + private readonly string _toParty; + private readonly string _toTag; + + public WorkWeiXinAppApiClient( + string corpid, + string agentId, + string secret, + string toUser = "", + string toParty = "", + string toTag = "" + ) + { + + _corpId = corpid; + _agentId = agentId; + _secret = secret; + _toUser = toUser; + _toParty = toParty; + _toTag = toTag; + + // token + var token = GetAccessToken(corpid, secret); + _apiUrl = new Uri($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}"); + } + + public override string ClientName => "WorkWeiXinApp"; + + protected override string NewLineStr => "\n"; + + public override HttpResponseMessage DoSend() + { + var json = new + { + touser = _toUser, + toparty = _toParty, + totag = _toTag, + agentid = _agentId, + msgtype = "text", + text = new + { + content = Msg + } + }.ToJson(); + + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = _httpClient.PostAsync(_apiUrl, content).GetAwaiter().GetResult(); + return response; + } + + private string GetAccessToken(string corpId, string secret) + { + var token = ""; + + try + { + var uri = new Uri($"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpId}&corpsecret={secret}"); + var response = _httpClient.GetAsync(uri).GetAwaiter().GetResult(); + var content = response.Content.ReadAsStringAsync() + .GetAwaiter().GetResult(); + + var re = content.ToObject(); + + if (re.errcode == 0) return re.access_token; + } + catch (Exception) + { + //ignore + } + + return token; + } + } +} diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppBatchedSink.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppBatchedSink.cs new file mode 100644 index 000000000..f837d4f2f --- /dev/null +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppBatchedSink.cs @@ -0,0 +1,59 @@ +using System; +using Ray.Serilog.Sinks.Batched; +using Serilog.Events; + +namespace Ray.Serilog.Sinks.WorkWeiXinAppBatched +{ + public class WorkWeiXinAppBatchedSink : BatchedSink + { + private readonly string _corpId; + private readonly string _agentId; + private readonly string _secret; + + private readonly string _toUser; + private readonly string _toParty; + private readonly string _toTag; + + public WorkWeiXinAppBatchedSink( + string corpId, + string agentId, + string secret, + string toUser, + string toParty, + string toTag, + Predicate predicate, + bool sendBatchesAsOneMessages, + string outputTemplate, + IFormatProvider formatProvider, + LogEventLevel minimumLogEventLevel + ) + : base(predicate, sendBatchesAsOneMessages, outputTemplate, formatProvider, minimumLogEventLevel) + { + _corpId = corpId; + _agentId = agentId; + _secret = secret; + _toUser = toUser; + _toParty = toParty; + _toTag = toTag; + } + + public override void Emit(LogEvent logEvent) + { + if (_corpId.IsNullOrEmpty()||_secret.IsNullOrEmpty()||_agentId.IsNullOrEmpty()) return; + base.Emit(logEvent); + } + + protected override PushService PushService => new WorkWeiXinAppApiClient( + _corpId, + _agentId, + _secret, + _toUser, + _toParty, + _toTag); + + public override void Dispose() + { + //todo + } + } +} diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppConfigurationExtensions.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppConfigurationExtensions.cs new file mode 100644 index 000000000..f2566e14d --- /dev/null +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppConfigurationExtensions.cs @@ -0,0 +1,50 @@ +using System; +using Ray.Serilog.Sinks.Batched; +using Serilog; +using Serilog.Configuration; +using Serilog.Events; + +namespace Ray.Serilog.Sinks.WorkWeiXinAppBatched +{ + public static class WorkWeiXinAppConfigurationExtensions + { + public static LoggerConfiguration WorkWeiXinAppBatched( + this LoggerSinkConfiguration loggerSinkConfiguration, + string corpId, + string agentId, + string secret, + string toUser, + string toParty, + string toTag, + string containsTrigger = Constants.DefaultContainsTrigger, + bool sendBatchesAsOneMessages = true, + string outputTemplate = Constants.DefaultOutputTemplate, + IFormatProvider formatProvider = null, + LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose + ) + { + if (loggerSinkConfiguration == null) + throw new ArgumentNullException(nameof(loggerSinkConfiguration)); + if (outputTemplate == null) + throw new ArgumentNullException(nameof(outputTemplate)); + + if (containsTrigger.IsNullOrEmpty()) containsTrigger = Constants.DefaultContainsTrigger; + Predicate predicate = x => x.MessageTemplate.Text.Contains(containsTrigger); + + return loggerSinkConfiguration.Sink( + new WorkWeiXinAppBatchedSink( + corpId, + agentId, + secret, + toUser, + toParty, + toTag, + predicate, + sendBatchesAsOneMessages, + outputTemplate, + formatProvider, + restrictedToMinimumLevel), + restrictedToMinimumLevel); + } + } +} diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppTokenResponse.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppTokenResponse.cs new file mode 100644 index 000000000..382421e70 --- /dev/null +++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.WorkWeiXinAppBatched/WorkWeiXinAppTokenResponse.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ray.Serilog.Sinks.WorkWeiXinAppBatched +{ + public class WorkWeiXinAppTokenResponse + { + public int errcode { get; set; } + + public string errmsg { get; set; } + + public string access_token { get; set; } + + public int expires_in { get; set; } + } +} diff --git a/test/BiliAgentTest/BiliAgentTest.csproj b/test/BiliAgentTest/BiliAgentTest.csproj index e0afedba2..0a8272606 100644 --- a/test/BiliAgentTest/BiliAgentTest.csproj +++ b/test/BiliAgentTest/BiliAgentTest.csproj @@ -8,13 +8,13 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/ConfigTest/ConfigTest.csproj b/test/ConfigTest/ConfigTest.csproj index 9015e6ada..59cead1c9 100644 --- a/test/ConfigTest/ConfigTest.csproj +++ b/test/ConfigTest/ConfigTest.csproj @@ -6,13 +6,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/LogTest/LogTest.csproj b/test/LogTest/LogTest.csproj index 97f27ee26..6c443a99b 100644 --- a/test/LogTest/LogTest.csproj +++ b/test/LogTest/LogTest.csproj @@ -7,13 +7,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -21,7 +21,7 @@ - + diff --git a/test/LogTest/TestMicrosoftTeams.cs b/test/LogTest/TestMicrosoftTeams.cs index e5e2d131c..491098d7b 100644 --- a/test/LogTest/TestMicrosoftTeams.cs +++ b/test/LogTest/TestMicrosoftTeams.cs @@ -6,7 +6,7 @@ using Ray.BiliBiliTool.Console; using Ray.BiliBiliTool.Infrastructure; using Ray.Serilog.Sinks.CoolPushBatched; -using Ray.Serilog.Sinks.MicrosoftTeams; +using Ray.Serilog.Sinks.MicrosoftTeamsBatched; using Ray.Serilog.Sinks.PushPlusBatched; using Ray.Serilog.Sinks.ServerChanBatched; using Xunit; diff --git a/test/LogTest/TestWorkWeiXinApp.cs b/test/LogTest/TestWorkWeiXinApp.cs new file mode 100644 index 000000000..b78ab0426 --- /dev/null +++ b/test/LogTest/TestWorkWeiXinApp.cs @@ -0,0 +1,42 @@ +using System; +using System.Diagnostics; +using Ray.BiliBiliTool.Console; +using Ray.BiliBiliTool.Infrastructure; +using Ray.Serilog.Sinks.WorkWeiXinAppBatched; +using Xunit; + +namespace LogTest +{ + public class TestWorkWeiXinApp + { + private string _agentId; + private string _secret; + private string _corpId; + private string _toUser; + + public TestWorkWeiXinApp() + { + Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); + Program.CreateHost(new string[] { }); + + _agentId = Global.ConfigurationRoot["Serilog:WriteTo:11:Args:agentId"]; + _secret = Global.ConfigurationRoot["Serilog:WriteTo:11:Args:secret"]; + _corpId = Global.ConfigurationRoot["Serilog:WriteTo:11:Args:corpId"]; + + _toUser = Global.ConfigurationRoot["Serilog:WriteTo:11:Args:toUser"]; + } + + [Fact] + public void Test() + { + var client = new WorkWeiXinAppApiClient(_corpId,_agentId, _secret, _toUser); + + var msg = LogConstants.Msg2; + + var result = client.PushMessage(msg); + Debug.WriteLine(result.Content.ReadAsStringAsync().Result); + + Assert.True(result.StatusCode == System.Net.HttpStatusCode.OK); + } + } +}