DreamSlave.Wecom 1.3.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package DreamSlave.Wecom --version 1.3.1
                    
NuGet\Install-Package DreamSlave.Wecom -Version 1.3.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DreamSlave.Wecom" Version="1.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DreamSlave.Wecom" Version="1.3.1" />
                    
Directory.Packages.props
<PackageReference Include="DreamSlave.Wecom" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DreamSlave.Wecom --version 1.3.1
                    
#r "nuget: DreamSlave.Wecom, 1.3.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DreamSlave.Wecom@1.3.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DreamSlave.Wecom&version=1.3.1
                    
Install as a Cake Addin
#tool nuget:?package=DreamSlave.Wecom&version=1.3.1
                    
Install as a Cake Tool

企业微信相关方法封装

使用说明

本仓库目前为预览版,无任何使用保障,仅在部分项目中引用尚未发现任何问题,请勿用于生产环境

已实现的功能

  • 企业微信Token
  • 企业微信Js-ticket
  • 企业微信加密回调解析
  • 企业微信加密回调响应(仅限文本)
  • 企业微信回调接口
  • 获取企业授权用户登录身份
  • 获取企业用户敏感身份信息
  • 获取企业Web登录的用户信息
  • 企业微信机器人(消息推送)
  • 企业微信发送应用消息(文本、图片、文件、图文、视频、语音、卡片等)

更多功能敬请期待

Token/Ticket处理方式

cfg.AutoRefresh = true;时,将在后台启动BackgroundService服务并在每60分钟自动刷新一次

由于未使用任何第三方库,所以采用了IMemoryCache即内存数据库,当每次程序重启时会自动刷新一次Token/Ticket,请注意遵守企业微信刷新规则限制,如有更高的需求请关闭自动刷新并自行实现此刷新逻辑(但是需要调用RefreshAccessTokenAsyncRefreshJsApiTicketAsync避免内部方法无法获取到Token的问题)

使用教程->企业微信回调/鉴权

该库提供了两种的注入方式方便使用者调用

可从Nuget中下载此库

请勿使用1.1.1版本以前的版本

单一实例注入
builder.Services.AddWecomService(cfg =>
{
    cfg.CorpID = "企业微信Id";
    cfg.CorpSecret = "Secret";
    cfg.AgentId = 1000001;
    cfg.Token = "";
    cfg.EncodingAesKey = "";
    cfg.AutoRefresh = true;
});
多实例注入(类似于IHttpClientFactory)
builder.Services.AddWecomService("名称",cfg =>
{
    cfg.CorpID = "企业微信Id";
    cfg.CorpSecret = "Secret";
    cfg.AgentId = 1000001;
    cfg.Token = "";
    cfg.EncodingAesKey = "";
    cfg.AutoRefresh = true;
});

在不同的实例注入下,使用方法有着本质的区别请注意

如果是单一实例,则直接注入IWecomOAuth2ServiceIWecomCallBackService即可

private readonly ILogger<WecomController> _logger;
private readonly IWecomOAuth2Service _wecomOauth2Service;
private readonly IWecomCallBackService _wecomCallBackService;
public WecomController(ILogger<WecomController> logger,
     IWecomOAuth2Service wecomOAuth2Service,
     IWecomCallBackService wecomCallBackService)
 {
     _logger = logger;
     _wecomCallBackService = wecomCallBackService;
     _wecomOauth2Service = wecomOAuth2Service;
 }

如果是多实例注入,则需要注入IWecomFactory然后获取指定名称的Service

private readonly ILogger<WecomController> _logger;
private readonly IWecomOAuth2Service _wecomTokenService;
private readonly IWecomFactory _wecomFactory;
private readonly IWecomCallBackService _wecomCallBackService;
private readonly IWecomCallBackService _addressCallBackService;
public WecomController(ILogger<WecomController> logger,
    IWecomFactory wecomFactory)
{
    _logger = logger;
    _wecomFactory = wecomFactory;
    _wecomCallBackService = _wecomFactory.GetCallback("oauth2");
    _wecomTokenService = _wecomFactory.GetOAuth2("oauth2");
    _addressCallBackService = _wecomFactory.GetCallback("adress");
}
企业微信回调WebApi示例
/// <summary>
/// 企业微信签名回调
/// </summary>
/// <param name="callback"></param>
/// <returns></returns>
[HttpGet("/api/wecom/callback")]
public IActionResult GetCallBack([FromQuery] Callback callback)
{
    var flag = _wecomCallBackService.CheckSignature(callback);
    if (!flag)
    {
        return Content("签名不符合验证!", "text/plain", System.Text.Encoding.UTF8);
    }

    var data = _wecomCallBackService.DecryptEchostr(callback);
    return Content(data, "text/plain", Encoding.UTF8);
}


[HttpPost("/api/wecom/callback")]
public async Task<IActionResult> PostCallBack([FromQuery] Callback callback)
{
    using var reader = new StreamReader(Request.Body);
    var payload = await reader.ReadToEndAsync();
    var data = _wecomCallBackService.DecryptCallBackData(payload);
    _logger.LogInformation("消息内容:{0}", data);
	//这里的data需要自行处理
    return Content(await _oauthExec.HandleMessageAsync(data), "text/plain", System.Text.Encoding.UTF8);
}

这里的_oauthExec需要进行注入,如果是单实例注入则直接注入WecomCommandExecService即可

为了方便测试,下面放两个使用示例,只需要按照下面的写法进行编写即可,库会自动扫描(反射)处理

text内容

public class DeveloperHandler
{
    [WecomTextCommand("id")]
    public async Task<string> HandleOpenIdCommandAsync(IServiceProvider serviceProvider, MessageReceive messageReceive)
    {
        return await Task.FromResult("您好,您的企业微信Id为:" + messageReceive.fromUserName);
    }
}

event内容

public class EventHandler
{
    [WecomEventCommand("enter_agent")]
    public async Task<string> OnEnterAgent(IServiceProvider provider, MessageReceive message)
    {
        return await Task.FromResult($"您进入了{message.AgentId}");
    }


    [WecomEventCommand("location")]
    public async Task<string> OnLocationEvent(IServiceProvider provider, MessageReceive message)
    {
        return await Task.FromResult($"您的位置是{message.Latitude}/{message.Longitude} {message.Precision}");
    }


    [WecomEventCommand("view")]
    public async Task<string> OnViewEvent(IServiceProvider provider, MessageReceive message)
    {
        return await Task.FromResult($"您点击了<a href='{message.EventKey}'>菜单</a>");
    }

    [WecomEventCommand("click")]
    public async Task <string> OnClickEvent(IServiceProvider provider, MessageReceive message)
    {
        return await Task.FromResult($"您点击了菜单 {message.EventKey}");
    }


    [WecomEventCommand("view_miniprogram")]
    public async Task<string> OnViewMiniprogramEvent(IServiceProvider provider, MessageReceive message)
    {
        return await Task.FromResult($"您点击了小程序 {message.EventKey}");
    }


    [WecomEventCommand("scancode_push")]
    public async Task<string> OnScanCodePushEvent(IServiceProvider provider, MessageReceive message)
    {
        return await Task.FromResult($"您扫码了 {message.ScanCodeInfo.ScanResult}");
    }

    [WecomEventCommand("scancode_waitmsg")]
    public async Task<string> OnScanCodeWaitMsgEvent(IServiceProvider provider, MessageReceive message)
    {
        return await Task.FromResult($"您扫码了 {message.ScanCodeInfo.ScanResult}");
    }



    [WecomEventCommand("change_contact")]
    public async Task<string> OnChangeContactEvent(IServiceProvider provider, MessageReceive message)
    {   
        return await Task.FromResult($"通讯录变更事件 {message.ChangeType}");
    }


    [WecomEventCommand("batch_job_result")]
    public async Task<string> OnBatchJobResultEvent(IServiceProvider provider, MessageReceive message)
    {
        return await Task.FromResult($"批量任务结果 {message.BatchJob.JobId} {message.BatchJob.JobType}");
    }
}

使用教程-机器人推送

Program.cs中添加下列代码,可以添加多个机器人只需要修改name即可

机器人推送除了模板卡片类型,其余所有类型都已内建支持,另外机器人推送的返回参数只告知是否成功会忽略其他的信息

builder.Services.AddWecomBot("运维机器人", "3297fdf1-baf5-4da7-b6ca-68bfa7f65af4");

然后在接口或者实现的服务中进行下列操作

private readonly IWecomBotService _wecomBotService;
public WecomController(ILogger<WecomController> logger,
    [FromKeyedServices("运维机器人")] IWecomBotService wecomBotService)
{
    _logger = logger;
    _wecomBotService = wecomBotService;
}

最后调用方法即可

var flag = await _wecomBotService.SendTextMessageToBotAsync(keyword);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.6.0 38 9/28/2025
1.5.7 123 9/26/2025 1.5.7 is deprecated because it is no longer maintained and has critical bugs.
1.5.6 111 9/26/2025 1.5.6 is deprecated because it is no longer maintained and has critical bugs.
1.5.5 113 9/26/2025 1.5.5 is deprecated because it is no longer maintained and has critical bugs.
1.5.0 139 9/25/2025 1.5.0 is deprecated because it is no longer maintained and has critical bugs.
1.4.0 218 9/24/2025 1.4.0 is deprecated because it is no longer maintained and has critical bugs.
1.3.13 206 9/24/2025 1.3.13 is deprecated because it is no longer maintained and has critical bugs.
1.3.12 287 9/24/2025 1.3.12 is deprecated because it is no longer maintained and has critical bugs.
1.3.11 194 9/24/2025 1.3.11 is deprecated because it is no longer maintained and has critical bugs.
1.3.10 194 9/24/2025 1.3.10 is deprecated because it is no longer maintained and has critical bugs.
1.3.9 190 9/24/2025 1.3.9 is deprecated because it is no longer maintained and has critical bugs.
1.3.8 185 9/24/2025 1.3.8 is deprecated because it is no longer maintained and has critical bugs.
1.3.7 186 9/24/2025 1.3.7 is deprecated because it is no longer maintained and has critical bugs.
1.3.6 267 9/24/2025 1.3.6 is deprecated because it is no longer maintained and has critical bugs.
1.3.5 179 9/24/2025 1.3.5 is deprecated because it is no longer maintained and has critical bugs.
1.3.4 187 9/24/2025 1.3.4 is deprecated because it is no longer maintained and has critical bugs.
1.3.3 191 9/24/2025 1.3.3 is deprecated because it is no longer maintained and has critical bugs.
1.3.2 190 9/24/2025 1.3.2 is deprecated because it is no longer maintained and has critical bugs.
1.3.1 195 9/24/2025 1.3.1 is deprecated because it is no longer maintained and has critical bugs.
1.3.0 196 9/24/2025 1.3.0 is deprecated because it is no longer maintained and has critical bugs.
1.2.0 225 9/23/2025 1.2.0 is deprecated because it is no longer maintained and has critical bugs.
1.1.1 239 9/23/2025 1.1.1 is deprecated because it is no longer maintained and has critical bugs.
1.1.0 222 9/23/2025 1.1.0 is deprecated because it is no longer maintained and has critical bugs.
1.0.7 360 9/16/2025 1.0.7 is deprecated because it is no longer maintained and has critical bugs.
1.0.6 299 9/15/2025 1.0.6 is deprecated because it is no longer maintained and has critical bugs.
1.0.5 295 9/15/2025 1.0.5 is deprecated because it is no longer maintained and has critical bugs.
1.0.4 295 9/15/2025 1.0.4 is deprecated because it is no longer maintained and has critical bugs.
1.0.3 297 9/15/2025 1.0.3 is deprecated because it is no longer maintained and has critical bugs.
1.0.2 296 9/15/2025 1.0.2 is deprecated because it is no longer maintained and has critical bugs.
1.0.1 297 9/15/2025 1.0.1 is deprecated because it is no longer maintained and has critical bugs.
1.0.0 266 9/15/2025 1.0.0 is deprecated because it is no longer maintained and has critical bugs.