提交
This commit is contained in:
63
Hotfix/Api/Helper/DingTalkHelper.cs
Normal file
63
Hotfix/Api/Helper/DingTalkHelper.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
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 code)
|
||||
{
|
||||
DingTalkMarkdownData dingTalkTestData = new DingTalkMarkdownData
|
||||
{
|
||||
markdown = new DingTalkMarkdownItem
|
||||
{
|
||||
title = "NB验证码",
|
||||
text = $"验证码:{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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user