企业微信机器人,开箱即用。目前已实现:
- 纯文本消息
- Markdown消息
- 1.3.0: 替换掉Flurl.Http,因为其可能会出现不能向后兼容的情况
var webhookKey = "d77f7c20-90ba-462a-9251-36ecdd63c5d8";
var wxWorkBotClient = WxWorkBotClient.WithKey(webhookKey);
await wxWorkBotClient.SendText("测试");
先安装 WxWorkRobot.MsDependency包:
配置服务AddWxWorkBotService:
public void ConfigureServices(IServiceCollection services)
{
// 默认webhook key
var webhookKey = "d77f7c20-90ba-462a-9251-36ecdd63c5d8";
services.AddWxWorkBotService(webhookKey);
}
或appsettings.json中配置有:
"WxWorkRobot": {
"WebhookKey": "d77f7c20-90ba-462a-9251-36ecdd63c5d8"
}
然后依然是AddWxWorkBotService:
public void ConfigureServices(IServiceCollection services)
{
services.AddWxWorkBotService(Configuration);
}
构造函数直接构造实例并使用,参考使用示例:
public class SampleService
{
private readonly WxWorkBotClient client;
public SampleService(WxWorkBotClient client)
{
this.client = client;
}
public async Task SendTextTest()
{
await client.SendText("测试");
}
}
如果想要使用非默认webhook key:
private readonly WxWorkBotClient client;
public WxWorkBotClientTest(WxWorkBotClient client)
{
this.client = client;
}
[Theory]
[InlineData("Test1")]
public async Task SendText(string text)
{
// 覆盖默认webhook key
client.SetKey("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
await client.SendText(text);
}