64 lines
2.4 KiB
C#
64 lines
2.4 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BZPT.Domains.Application
|
|
{
|
|
public class MessageApplication
|
|
{
|
|
public static async Task SendCIMessage(string message)
|
|
{
|
|
|
|
string webhookUrl =!string.IsNullOrEmpty(ConfigAccessor.Instance["DingDingCI"]) ?
|
|
ConfigAccessor.Instance["DingDingCI"]:
|
|
"https://oapi.dingtalk.com/robot/send?access_token=43532a4c00bbf14f3ba0618cf46186827694eb39aaad46db708efa0e37ed0f18";
|
|
await SendDingTalkMessage(webhookUrl, message);
|
|
}
|
|
public static async Task SendErrorMessage(string message)
|
|
{
|
|
#if DEBUG
|
|
#else
|
|
|
|
string webhookUrl =!string.IsNullOrEmpty(ConfigAccessor.Instance["DingDingCI"]) ?
|
|
ConfigAccessor.Instance["DingDingCI"]:"https://oapi.dingtalk.com/robot/send?access_token=22da976d55d01b08655641767580db466d528d7b0499d7fc1975338df4dd6a65";
|
|
await SendDingTalkMessage(webhookUrl, message);
|
|
#endif
|
|
}
|
|
static async Task SendDingTalkMessage(string webhookUrl, string message)
|
|
{
|
|
try
|
|
{
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
// 构建消息内容的 JSON 对象
|
|
var messageContent = new
|
|
{
|
|
msgtype = "text",
|
|
text = new
|
|
{
|
|
content =$"BZPT.日志:{message}"
|
|
}
|
|
};
|
|
// 将对象序列化为 JSON 字符串
|
|
string json = JsonConvert.SerializeObject(messageContent);
|
|
// 创建包含消息内容的 HttpContent 对象
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
// 发送 POST 请求到钉钉机器人的 Webhook 地址
|
|
HttpResponseMessage response = await client.PostAsync(webhookUrl, content);
|
|
// 检查响应是否成功
|
|
response.EnsureSuccessStatusCode();
|
|
Console.WriteLine("消息发送成功");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"消息发送失败: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|