??讀完這篇文章里你能收獲到
- 了解博主開源的告警通知項(xiàng)目——EasyNotice
- 傻瓜式擴(kuò)展方法直接使用
- 如何通過(guò)EasyNotice快速實(shí)現(xiàn)郵件/釘釘/飛書/企業(yè)微信的通知發(fā)送
- 感謝點(diǎn)贊+收藏,避免下次找不到~
一、EasyNotice
這是博主開源的一個(gè)基于.NET開源的消息通知組件,它包含了郵件、釘釘、飛書、企業(yè)微信的群機(jī)器人通知,可以幫助我們更容易地發(fā)送程序異常通知!
1. 源碼地址
- https://github.com/Bryan-Cyf/EasyNotice
2. 功能介紹
- 支持[郵件]、[釘釘]、[飛書]、[企業(yè)微信]方式發(fā)送
- 支持自定義發(fā)送間隔,避免同樣的異常頻繁通知
- 傻瓜式配置,開箱即用
3. 平臺(tái)支持
- SMTP郵箱
- 釘釘群機(jī)器人
- 飛書群機(jī)器人
- 企業(yè)微信群機(jī)器人
二、項(xiàng)目接入
1. 郵件通知
郵件通知支持同時(shí)發(fā)送給多個(gè)收件人
- Step 1 : 安裝包,通過(guò)Nuget安裝包
Install-Package EasyNotice.Core
Install-Package EasyNotice.Email
- Step 2 : 配置 Startup 啟動(dòng)類
public class Startup
{
//...
public void ConfigureServices(IServiceCollection services)
{
//configuration
services.AddEasyNotice(config =>
{
config.IntervalSeconds = 10;//同一標(biāo)題的消息,10秒內(nèi)只能發(fā)一條,避免短時(shí)間內(nèi)大量發(fā)送重復(fù)消息
config.UseEmail(option =>
{
option.Host = "smtp.qq.com";//SMTP地址
option.Port = 465;//SMTP端口
option.FromName = "System";//發(fā)送人名字(自定義)
option.FromAddress = "12345@qq.com";//發(fā)送郵箱
option.Password = "passaword";//秘鑰
option.ToAddress = new List<string>()//收件人集合
{
"12345@qq.com"
};
});
});
}
}
- Step 3 : IEmailProvider服務(wù)接口使用
[ApiController]
[Route("[controller]/[action]")]
public class NoticeController : ControllerBase
{
private readonly IEmailProvider _mailProvider;
public NoticeController(IEmailProvider provider)
{
_mailProvider = provider;
}
[HttpGet]
public async Task SendMail([FromQuery] string str)
{
await _mailProvider.SendAsync(str, new Exception(str));
}
}
2. 釘釘通知
配置釘釘群機(jī)器人官方文檔
- Step 1 : 安裝包,通過(guò)Nuget安裝包
Install-Package EasyNotice.Core
Install-Package EasyNotice.Dingtalk
- Step 2 : 配置 Startup 啟動(dòng)類
public class Startup
{
//...
public void ConfigureServices(IServiceCollection services)
{
//configuration
services.AddEasyNotice(config =>
{
config.IntervalSeconds = 10;//同一標(biāo)題的消息,10秒內(nèi)只能發(fā)一條,避免短時(shí)間內(nèi)大量發(fā)送重復(fù)消息
config.UseDingTalk(option =>
{
option.WebHook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx";//通知地址
option.Secret = "secret";//簽名校驗(yàn)
});
});
}
}
- Step 3 : IDingtalkProvider服務(wù)接口使用
[ApiController]
[Route("[controller]/[action]")]
public class NoticeController : ControllerBase
{
private readonly IDingtalkProvider _dingtalkProvider;
public NoticeController(IDingtalkProvider dingtalkProvider)
{
_dingtalkProvider = dingtalkProvider;
}
[HttpGet]
public async Task SendDingTalk([FromQuery] string str)
{
await _dingtalkProvider.SendAsync(str, new Exception(str));
}
}
3. 飛書通知
配置飛書群機(jī)器人官方文檔
- Step 1 : 安裝包,通過(guò)Nuget安裝包
Install-Package EasyNotice.Core
Install-Package EasyNotice.Feishu
- Step 2 : 配置 Startup 啟動(dòng)類
public class Startup
{
//...
public void ConfigureServices(IServiceCollection services)
{
//configuration
services.AddEasyNotice(config =>
{
config.IntervalSeconds = 10;//同一標(biāo)題的消息,10秒內(nèi)只能發(fā)一條,避免短時(shí)間內(nèi)大量發(fā)送重復(fù)消息
config.UseFeishu(option =>
{
option.WebHook = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx";//通知地址
option.Secret = "secret";//簽名校驗(yàn)
});
});
}
}
- Step 3 : IFeishuProvider服務(wù)接口使用
[ApiController]
[Route("[controller]/[action]")]
public class NoticeController : ControllerBase
{
private readonly IFeishuProvider _feishuProvider;
public NoticeController(IFeishuProvider feishuProvider)
{
_feishuProvider = feishuProvider;
}
[HttpGet]
public async Task SendFeishu([FromQuery] string str)
{
await _feishuProvider.SendAsync(str, new Exception(str));
}
}
4. 企業(yè)微信通知
配置企業(yè)微信群機(jī)器人官方文檔
- Step 1 : 安裝包,通過(guò)Nuget安裝包
Install-Package EasyNotice.Core
Install-Package EasyNotice.Weixin
- Step 2 : 配置 Startup 啟動(dòng)類
public class Startup
{
//...
public void ConfigureServices(IServiceCollection services)
{
//configuration
services.AddEasyNotice(config =>
{
config.IntervalSeconds = 10;//同一標(biāo)題的消息,10秒內(nèi)只能發(fā)一條,避免短時(shí)間內(nèi)大量發(fā)送重復(fù)消息
config.UseWeixin(option =>
{
option.WebHook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx";//通知地址
});
});
}
}
- Step 3 : IWeixinProvider服務(wù)接口使用
[ApiController]
[Route("[controller]/[action]")]
public class NoticeController : ControllerBase
{
private readonly IWeixinProvider _weixinProvider;
public NoticeController(IWeixinProvider weixinProvider)
{
_weixinProvider = weixinProvider;
}
[HttpGet]
public async Task SendWexin([FromQuery] string str)
{
await _weixinProvider.SendAsync(str, new Exception(str));
}
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-402218.html
三、更多示例
- 查看 更多使用例子
- 查看 更多測(cè)試用例
- 源碼地址:https://github.com/Bryan-Cyf/EasyNotice
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-402218.html
到了這里,關(guān)于『EasyNotice』.NET開源消息通知組件——快速實(shí)現(xiàn)郵件/釘釘/飛書/企業(yè)微信告警通知的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!