搭建消息告警模块以及跨网推送服务,支持企微,飞书,钉钉通知
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Common.Notify
|
||||
{
|
||||
/// <summary>
|
||||
/// IM 机器人 Webhook 加签工具(钉钉/飞书)
|
||||
/// 供 IOT_API 直连推送与 AlertBridge 跨网转发程序共用,保证两种模式签名算法一致
|
||||
/// </summary>
|
||||
public static class ImWebhookSigner
|
||||
{
|
||||
/// <summary>
|
||||
/// 钉钉机器人加签:sign = UrlEncode(Base64(HmacSHA256(timestamp + "\n" + secret, key=secret)))
|
||||
/// </summary>
|
||||
/// <param name="timestampMs">毫秒时间戳</param>
|
||||
/// <param name="secret">加签密钥(SEC 开头)</param>
|
||||
/// <returns>URL 编码后的签名</returns>
|
||||
public static string DingTalkSign(long timestampMs, string secret)
|
||||
{
|
||||
string stringToSign = $"{timestampMs}\n{secret}";
|
||||
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
|
||||
byte[] signData = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign));
|
||||
return Uri.EscapeDataString(Convert.ToBase64String(signData));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 飞书机器人加签:sign = Base64(HmacSHA256(空串, key=timestamp + "\n" + secret))
|
||||
/// 注意与钉钉相反:飞书以"时间戳\n密钥"为 key、对空内容签名
|
||||
/// </summary>
|
||||
/// <param name="timestampSec">秒级时间戳</param>
|
||||
/// <param name="secret">加签密钥</param>
|
||||
/// <returns>Base64 签名</returns>
|
||||
public static string FeishuSign(long timestampSec, string secret)
|
||||
{
|
||||
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes($"{timestampSec}\n{secret}"));
|
||||
byte[] signData = hmac.ComputeHash(Array.Empty<byte>());
|
||||
return Convert.ToBase64String(signData);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user