DingTalkWarningNoticeService.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using DingTalk.Api;
  2. using DingTalk.Api.Request;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using XYY.Service.Standard.DingDing;
  8. namespace XYY.Api.WarningNotice.WarningNoticeService
  9. {
  10. public class DingTalkWarningNoticeService : BaseService
  11. {
  12. private readonly Dictionary<string, string> tokens;
  13. private IDingDingService _dingDingService;
  14. public DingTalkWarningNoticeService(IDingDingService dingDingService)
  15. {
  16. _dingDingService = dingDingService;
  17. tokens = new Dictionary<string, string>
  18. {
  19. {"定时任务失败", "ff52710d8e80cf44bc7981e557bfb9e123ea491ad9050e2304a634b1fcf0530d"},
  20. {"发送消息", "ff52710d8e80cf44bc7981e557bfb9e123ea491ad9050e2304a634b1fcf0530d"},
  21. {"邮政公开价更新", "eae7c7dcea1b5617dcf3d8deb1691aae92ce70310be0751550e89da6034da760"},
  22. {"定时任务", "ff52710d8e80cf44bc7981e557bfb9e123ea491ad9050e2304a634b1fcf0530d"},
  23. };
  24. }
  25. private string getObjcetToken(WarningNoticeTarget target)
  26. {
  27. if (tokens.TryGetValue(target.TargetName, out var token))
  28. {
  29. return token;
  30. }
  31. throw new Exception($"未找到相关机器人:{target.TargetName}");
  32. }
  33. public override bool NoticeText(TextMessageNoticeTarget target)
  34. {
  35. string token = getObjcetToken(target);
  36. string url = $"https://oapi.dingtalk.com/robot/send?access_token={token}";
  37. //return url;
  38. //string url = CreateMsgUrl(target);
  39. //string token = getObjcetToken(target);
  40. var client = new DefaultDingTalkClient(url);
  41. OapiRobotSendRequest request = new OapiRobotSendRequest();
  42. request.Msgtype = "text";
  43. OapiRobotSendRequest.TextDomain text = new OapiRobotSendRequest.TextDomain();
  44. text.Content = target.Content;
  45. request.Text_ = text;
  46. OapiRobotSendRequest.AtDomain at = new OapiRobotSendRequest.AtDomain();
  47. at.IsAtAll = true;
  48. var response = client.Execute(request);
  49. return response.IsError;
  50. }
  51. public override bool NoticeLink(LinkMessageNoticeTarget target)
  52. {
  53. string url = CreateMsgUrl(target);
  54. var client = new DefaultDingTalkClient(url);
  55. OapiRobotSendRequest request = new OapiRobotSendRequest();
  56. request.Msgtype = "link";
  57. OapiRobotSendRequest.LinkDomain link = new OapiRobotSendRequest.LinkDomain();
  58. link.MessageUrl = target.Link;
  59. link.Text = target.Text;
  60. link.Title = target.Title;
  61. request.Link_ = link;
  62. OapiRobotSendRequest.AtDomain at = new OapiRobotSendRequest.AtDomain();
  63. at.IsAtAll = target.ToAllUser;
  64. at.AtUserIds = target.UserIds;
  65. var response = client.Execute(request);
  66. return response.IsError;
  67. }
  68. public async override Task<bool> NoticeActionCard(ActionCardNoticeTarget target)
  69. {
  70. List<dynamic> buttonJsonList = new List<dynamic>();
  71. foreach (var btn in target.Buttons)
  72. {
  73. var btnItem = new
  74. {
  75. title = btn.Text,
  76. action_url = btn.Link,
  77. };
  78. buttonJsonList.Add(btnItem);
  79. }
  80. List<int> userIds = target.UserIds;
  81. List<string> ddUserIds = new List<string>();
  82. foreach (var uid in userIds)
  83. {
  84. var ddUserInfo = await _dingDingService.GetDingDingUserInfo(uid);
  85. if (ddUserInfo != null)
  86. {
  87. ddUserIds.Add(ddUserInfo.UserId);
  88. }
  89. }
  90. string userId = string.Join(",", ddUserIds);
  91. dynamic actionCard2 = new
  92. {
  93. msgtype = "action_card",
  94. action_card = new
  95. {
  96. title = target.Title + $"_{DateTime.Now.ToString("yyyyMMddHHmmss")}",
  97. markdown = target.Text,
  98. btn_orientation = target.ButtonOrient,
  99. btn_json_list = buttonJsonList
  100. },
  101. to_all_user = false,
  102. userid_list = userId
  103. };
  104. string errMsg = await _dingDingService.SendMessage(userId, actionCard2);
  105. return !string.IsNullOrEmpty(errMsg);
  106. }
  107. private string CreateMsgUrl(WarningNoticeTarget target)
  108. {
  109. string token = getObjcetToken(target);
  110. string url = $"https://oapi.dingtalk.com/robot/send?access_token={token}";
  111. return url;
  112. }
  113. }
  114. }