using DbusSmsForward.Helper; using DbusSmsForward.Services; using DbusSmsForward.SettingModel; using DbusSmsForward.SMSModel; using System.Security.Cryptography; using System.Text; using System.Text.Json.Nodes; using System.Web; namespace DbusSmsForward.Notifications; public sealed class DingTalkNotificationSender(DbusSmsForwardOptions options) : NotificationSenderBase("dingtalk") { private readonly DingTalkConfig _config = options.Notifications.DingTalk; public override bool IsEnabled => _config.Enabled && !string.IsNullOrWhiteSpace(_config.AccessToken); public override async Task SendAsync(SmsContentModel sms, string body, string deviceName, CancellationToken cancellationToken) { string url = $"{_config.Endpoint}?access_token={HttpUtility.UrlEncode(_config.AccessToken)}"; if (!string.IsNullOrWhiteSpace(_config.Secret)) { long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); string sign = AddSign(timestamp, _config.Secret); url += $"×tamp={timestamp}&sign={sign}"; } JsonObject payload = new() { ["msgtype"] = "text", ["text"] = new JsonObject { ["content"] = body } }; await HttpHelper.PostAsync(url, payload, _config.SkipTlsValidation, cancellationToken); } private static string AddSign(long timestamp, string secret) { string stringToSign = $"{timestamp}\n{secret}"; using HMACSHA256 hmacsha256 = new(Encoding.UTF8.GetBytes(secret)); byte[] hashmessage = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)); return HttpUtility.UrlEncode(Convert.ToBase64String(hashmessage), Encoding.UTF8); } }