dingApiRequest.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Newtonsoft.Json;
  2. using RestSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using XYY.Tool.QuotaReduction;
  10. namespace XYY.Tool.TimingTask
  11. {
  12. public static class dingApiRequest
  13. {
  14. public static void sendDingMessageToChat(string jobName, string message)
  15. {
  16. int count = 0; bool isSucess = false;
  17. do
  18. {
  19. try
  20. {
  21. var client = new RestClient(JobConfig.dingdingApi);
  22. client.Timeout = -1;
  23. var request = new RestRequest(Method.POST);
  24. request.AddHeader("Content-Type", "application/json");
  25. var ddModel = new
  26. {
  27. ServiceType = "钉钉",
  28. TargetName = "定时任务失败",
  29. Content = jobName +"【"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"】 " + (!string.IsNullOrEmpty(message) ? "执行结果,"+ message : "定时任务完成" ),
  30. };
  31. var body = JsonConvert.SerializeObject(ddModel);
  32. request.AddParameter("application/json", body, ParameterType.RequestBody);
  33. IRestResponse response = client.Execute(request);
  34. if (response.StatusCode == System.Net.HttpStatusCode.OK)
  35. {
  36. isSucess = true;
  37. }
  38. else
  39. {
  40. count++;
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. count++;
  46. }
  47. Console.WriteLine($"【{jobName}】钉钉消息发送:{(isSucess ? "成功" : "失败")}");
  48. //睡眠再继续
  49. if (count <= JobConfig.QuotaReductionJobTryTime && !isSucess) { Thread.Sleep(JobConfig.dingdingTryTimeInterval); }
  50. }
  51. while (count <= JobConfig.dingdingTryTime && !isSucess);
  52. }
  53. public static void sendMessageToWeChat(string jobName, string message,List<string>WeChatId)
  54. {
  55. int count = 0; bool isSucess = false;
  56. do
  57. {
  58. try
  59. {
  60. var client = new RestClient(JobConfig.weChatApi);
  61. client.Timeout = -1;
  62. var request = new RestRequest(Method.POST);
  63. request.AddHeader("Content-Type", "application/json");
  64. var weChatModel = new
  65. {
  66. ServiceType="微信",
  67. TargetName = jobName,
  68. Content ="【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】 " + (!string.IsNullOrEmpty(message) ? "执行结果," + message : "定时任务完成"),
  69. UserId=WeChatId,
  70. };
  71. var body = JsonConvert.SerializeObject(weChatModel);
  72. request.AddParameter("application/json", body, ParameterType.RequestBody);
  73. IRestResponse response = client.Execute(request);
  74. if (response.StatusCode == System.Net.HttpStatusCode.OK)
  75. {
  76. isSucess = true;
  77. }
  78. else
  79. {
  80. count++;
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. count++;
  86. }
  87. Console.WriteLine($"【{jobName}】企微消息发送:{(isSucess ? "成功" : "失败")}");
  88. //睡眠再继续
  89. if (count <= JobConfig.QuotaReductionJobTryTime && !isSucess) { Thread.Sleep(JobConfig.dingdingTryTimeInterval); }
  90. }
  91. while (count <= JobConfig.dingdingTryTime && !isSucess);
  92. }
  93. }
  94. }