using Newtonsoft.Json; using RestSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using XYY.Tool.QuotaReduction; namespace XYY.Tool.TimingTask { public static class dingApiRequest { public static void sendDingMessageToChat(string jobName, string message) { int count = 0; bool isSucess = false; do { try { var client = new RestClient(JobConfig.dingdingApi); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); var ddModel = new { ServiceType = "钉钉", TargetName = "定时任务失败", Content = jobName +"【"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"】 " + (!string.IsNullOrEmpty(message) ? "执行结果,"+ message : "定时任务完成" ), }; var body = JsonConvert.SerializeObject(ddModel); request.AddParameter("application/json", body, ParameterType.RequestBody); IRestResponse response = client.Execute(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) { isSucess = true; } else { count++; } } catch (Exception ex) { count++; } Console.WriteLine($"【{jobName}】钉钉消息发送:{(isSucess ? "成功" : "失败")}"); //睡眠再继续 if (count <= JobConfig.QuotaReductionJobTryTime && !isSucess) { Thread.Sleep(JobConfig.dingdingTryTimeInterval); } } while (count <= JobConfig.dingdingTryTime && !isSucess); } public static void sendMessageToWeChat(string jobName, string message,ListWeChatId) { int count = 0; bool isSucess = false; do { try { var client = new RestClient(JobConfig.weChatApi); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); var weChatModel = new { ServiceType="微信", TargetName = jobName, Content ="【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】 " + (!string.IsNullOrEmpty(message) ? "执行结果," + message : "定时任务完成"), UserId=WeChatId, }; var body = JsonConvert.SerializeObject(weChatModel); request.AddParameter("application/json", body, ParameterType.RequestBody); IRestResponse response = client.Execute(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) { isSucess = true; } else { count++; } } catch (Exception ex) { count++; } Console.WriteLine($"【{jobName}】企微消息发送:{(isSucess ? "成功" : "失败")}"); //睡眠再继续 if (count <= JobConfig.QuotaReductionJobTryTime && !isSucess) { Thread.Sleep(JobConfig.dingdingTryTimeInterval); } } while (count <= JobConfig.dingdingTryTime && !isSucess); } } }