Files
Fishing2Server/Hotfix/Api/Helper/DingTalkHelper.cs
2026-04-01 16:40:34 +08:00

63 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text;
using Fantasy;
using Newtonsoft.Json;
namespace NBF;
public static class DingTalkHelper
{
private static readonly HttpClient _httpClient = new HttpClient();
public static async Task SendCAPTCHA(string account, string code)
{
DingTalkMarkdownData dingTalkTestData = new DingTalkMarkdownData
{
markdown = new DingTalkMarkdownItem
{
title = "NB验证码",
text = $"账号:{account}\n验证码{code}"
}
};
await SendMessageAsync("23457f9c93ac0ae909e1cbf8bcfeb7e0573968ac2d4c4b2c3a961b2f0c9247cb", dingTalkTestData);
}
/// <summary>
/// 通用消息发送方法
/// </summary>
/// <param name="token"></param>
/// <param name="message">消息对象</param>
/// <returns></returns>
private static async Task<bool> SendMessageAsync(string token, DingTalkMarkdownData message)
{
try
{
var json = JsonConvert.SerializeObject(message);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response =
await _httpClient.PostAsync($"https://oapi.dingtalk.com/robot/send?access_token={token}", content);
var responseContent = await response.Content.ReadAsStringAsync();
Log.Info($"钉钉={responseContent}");
// 检查响应是否成功
return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
Console.WriteLine($"发送飞书消息失败: {ex.Message}");
return false;
}
}
[Serializable]
public class DingTalkMarkdownData
{
public string msgtype { get; set; } = "markdown";
public DingTalkMarkdownItem markdown { get; set; }
}
[Serializable]
public class DingTalkMarkdownItem
{
public string title { get; set; }
public string text { get; set; }
}
}