Files
better-genshin-impact/BetterGenshinImpact/Service/Notifier/dingdingwebhookNotifier.cs
禹仔二号 929f4a30b3 添加dingding通知 (#1312)
* 新增图片作为email附件发送功能

* feat(notification): 为 WebhookNotifier 添加 send_to 功能

- 在 WebhookNotifier 类中添加 send_to 属性
- 修改 TransformData 方法,增加 send_to 字段
- 在 NotificationConfig 中添加 webhook_endpoint 和 websocket_endpoint 属性

* refactor(WebhookNotifier): 添加 send_to 属性并调整访问修饰符

* refactor(notification): 重构 WebhookNotifier 并更新 NotificationConfig

- 在 NotificationConfig 中添加 WebhookSendTo 属性
- 重构 WebhookNotifier 构造函数,使用 NotificationConfig 对象进行初始化
- 更新 WebhookNotifier 发送逻辑,使用新的配置属性

* feat: add webhook ui.

* refactor(WebhookNotifier): 重构 TransformData 方法以优化数据结构- 将 notification_data 的内容合并到外层字典,以便于 webhook 接收端获取数据- 新增 event、result、timestamp 等字段,使数据结构

* fix(WebSocketNotifier): 添加消息发送后关闭连接的功能

- 在 SendAsync 方法中添加了 CloseAsync() 调用,确保消息发送后关闭 WebSocket 连接
- 此修改解决了消息发送后未关闭连接的问题,提高了资源利用率和系统稳定性

* fix: implement unified webhook ui

* fix: typos.

* 优化通知前端显示

* feat(notification): 添加钉钉 Webhook 通知支持

- 实现了钉钉 Webhook 通知的发送功能
- 在通知配置中添加了钉钉 Webhook URL 和密钥的设置选项
- 在通知服务中集成了钉钉通知的初始化和注册
- 优化了 WebhookNotifier 类的代码结构

* 新增钉钉通知ui

Signed-off-by: DR-lin-eng <@DR-lin-eng>

* 修正图标

Signed-off-by: DR-lin-eng <@DR-lin-eng>

---------

Signed-off-by: DR-lin-eng <@DR-lin-eng>
Co-authored-by: DR-lin-eng <@DR-lin-eng>
Co-authored-by: 秋云 <physligl@gmail.com>
Co-authored-by: DR-lin-eng <52230594+DR-lin-eng@users.noreply.github.com>
Co-authored-by: 辉鸭蛋 <huiyadanli@gmail.com>
2025-03-16 03:18:02 +08:00

64 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Web;
using BetterGenshinImpact.Service.Notification.Model;
using BetterGenshinImpact.Service.Notifier.Interface;
namespace BetterGenshinImpact.Service.Notifier;
public class DingDingWebhook : INotifier
{
private readonly HttpClient _httpClient;
private readonly string _secret;
private readonly string _webhookUrl;
public DingDingWebhook(HttpClient httpClient, string webhookUrl, string secret)
{
_httpClient = httpClient;
_webhookUrl = webhookUrl;
_secret = secret;
}
public string Name { get; set; } = "DingDing";
public async Task SendAsync(BaseNotificationData content)
{
var timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
var stringToSign = $"{timestamp}\n{_secret}";
var encoding = Encoding.UTF8;
using (var hmac = new HMACSHA256(encoding.GetBytes(_secret)))
{
var data = encoding.GetBytes(stringToSign);
var hash = hmac.ComputeHash(data);
var signature = Convert.ToBase64String(hash);
var encodedSignature = HttpUtility.UrlEncode(signature);
var url = $"{_webhookUrl}&timestamp={timestamp}&sign={encodedSignature}";
var requestJson = new
{
msgtype = "text",
text = new { content = content.Message },
at = new { atUserIds = new List<string>(), isAtAll = false }
};
var json = JsonSerializer.Serialize(requestJson);
var contentToSend = new StringContent(json, encoding, "application/json");
try
{
var response = await _httpClient.PostAsync(url, contentToSend);
response.EnsureSuccessStatusCode();
}
catch (System.Exception ex)
{
throw new System.Exception("Failed to send message to DingDing", ex);
}
}
}
}