/// <summary> /// /// </summary> /// <param name="content">内容</param> /// <param name="msgEnum">消息类型</param> /// <param name="msgType">消息内容类型</param> /// <param name="names">通知人名</param> public static void Notify(NotifyMsgEnum msgEnum, string content, NotifyMsgTypeEnum msgType, string[] names) { //#if DEBUG // return; //#else //#endif var configs = Configs.Where(x => !x.IsChat && x.Type == NotifyTypeEnum.Default); foreach (var config in configs) { switch (config.Platform) { case NotifyPlatformEnum.DingTalk: NotifyDingTalk(config, msgEnum, content, msgType, names); break; case NotifyPlatformEnum.WorkWeiXin: NotifyWeiXin(config, msgEnum, content, msgType, names); break; default: break; } } }
/// <summary> /// 消息通知 /// </summary> /// <summary> /// /// </summary> /// <param name="platform">平台类型</param> /// <param name="notifyType">群类别</param> /// <param name="msgEnum">消息类型</param> /// <param name="content">内容</param> /// <param name="msgType">消息内容类型</param> /// <param name="atMobiles">通知人手机</param> /// <param name="isAtAll">是否@所有人</param> public static void NotifyChat(NotifyPlatformEnum platform, NotifyTypeEnum notifyType, NotifyMsgEnum msgEnum, string content, NotifyMsgTypeEnum msgType, string[] atMobiles = null, bool isAtAll = false) { #if !DEBUG var localIp = "?"; var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily.ToString() == "InterNetwork") { localIp = ip.ToString(); } } if (localIp == "192.168.1.184") { return; } #else //notifyType = NotifyTypeEnum.Test; #endif var groups = Configs.Where(x => x.IsChat && x.Type == notifyType && x.Platform == platform); foreach (var group in groups) { switch (group.Platform) { case NotifyPlatformEnum.DingTalk: RobotNotifyDingTalkChat(group, msgEnum, content, msgType, atMobiles, isAtAll); break; case NotifyPlatformEnum.WorkWeiXin: RobotNotifyWeiXinChat(group, msgEnum, content, msgType, atMobiles, isAtAll); break; default: break; } } }
public Result Notify([FromQuery] string content, NotifyMsgEnum msgEnum, NotifyTypeEnum notifyType, NotifyMsgTypeEnum msgType, string[] atMobiles, bool isAtAll = false) { return(Result.GenError <Result>(Error.Success)); }
/// <summary> /// 钉钉应用通知 /// https://open.work.weixin.qq.com/api/doc/90000/90135/90236 /// </summary> private static void NotifyDingTalk(NotifyWebhook notifyWebhook, NotifyMsgEnum msgEnum, string content, NotifyMsgTypeEnum msgType, string[] names) { if (!names.Any()) { Log.Error($"钉钉消息推送缺少推送人"); return; } var webHookUrl = notifyWebhook.Webhook; if (webHookUrl.IsNullOrEmpty()) { Log.Error($"钉钉消息推送失败;webHookUrl缺失"); return; } var agentId = notifyWebhook.AgentId; if (agentId.IsNullOrEmpty()) { Log.Error($"钉钉消息推送失败;AgentId缺失"); return; } var key = $"{nRedisPre}:Token_{notifyWebhook.CorpId}_{notifyWebhook.AgentId}"; var token = GetDingTalkAccessToken(key); if (token.IsNullOrEmpty()) { Log.Error($"钉钉消息推送失败1;{token}"); return; } var fullUrl = $"{webHookUrl}?access_token={token}"; //Log.Error($"钉钉消息推送URL;{ webHookUrl}×tamp ={timestamp}&sign ={sign}"); object sendData = null; var userList = GetDingTalkUserList(notifyWebhook); var userIds = userList.Where(x => names.Contains(x.name)).Select(x => x.userid).Join("|"); //msgType = NotifyMsgTypeEnum.markdown; var title = msgEnum.GetAttribute <DescriptionAttribute>()?.Description ?? ""; switch (msgType) { case NotifyMsgTypeEnum.text: //touser、toparty、totag不能同时为空。 sendData = new { //发送消息时使用的微应用的AgentID。 agent_id = agentId, //接收者的userid列表,最大用户列表长度100。 userid_list = userIds, //接收者的部门id列表,最大列表长度20。 dept_id_list = notifyWebhook.DepId, //是 消息类型,此时固定为:text msgtype = msgType.ToString(), //是否发送给企业全部用户。 to_all_user = false, //消息内容,最长不超过2048个字节 msg = new { //消息类型,此时固定为:text msgtype = msgType.ToString(), text = new { content = $"[{title}]{(notifyWebhook.Url.IsNullOrEmpty() ? "" : $" <a href=\"{notifyWebhook.Url}\">[查看]</a>")}\n {content}" }, }, }; break;
/// <summary> /// 机器人通知钉钉群 /// </summary> private static void RobotNotifyDingTalkChat(NotifyWebhook notifyWebhook, NotifyMsgEnum msgEnum, string content, NotifyMsgTypeEnum msgType, string[] atMobiles, bool isAtAll) { atMobiles = atMobiles ?? new string[] { }; var timestamp = DateTime.Now.ToTimestamp(); var secret = notifyWebhook.Secret; var sign = Sign(timestamp, secret); var webHookUrl = notifyWebhook.Webhook; if (webHookUrl.IsNullOrEmpty()) { return; } var fullUrl = $"{webHookUrl}×tamp={timestamp}&sign={sign}"; //Log.Error($"钉钉消息推送URL;{ webHookUrl}×tamp ={timestamp}&sign ={sign}"); object sendData = null; //msgType = NotifyMsgTypeEnum.markdown; var title = msgEnum.GetAttribute <DescriptionAttribute>()?.Description ?? ""; switch (msgType) { case NotifyMsgTypeEnum.text: sendData = new { msgtype = msgType.ToString(), text = new { content = $"[{title}]\n {content}({notifyWebhook.Url})" }, at = new { atMobiles, isAtAll }, }; break; case NotifyMsgTypeEnum.markdown: sendData = new { msgtype = msgType.ToString(), markdown = new { title, text = $"#### **{title}** \n ##### {content} [查看]({notifyWebhook.Url})" }, at = new { atMobiles, isAtAll }, }; break; } if (sendData == null) { return; } var httpClient = new HttpClient(fullUrl) { Verb = HttpVerb.POST, ContentType = HttpClient.ContentType_Json, RawData = sendData.ToJSON() }; httpClient.AsyncGetString((s, e) => { if (e != null) { Log.ErrorFormat("钉钉机器人消息推送失败1;{0}", e); } else if (!s.ToLower().Contains("ok")) { Log.ErrorFormat("钉钉机器人消息推送失败2;{0}", s); } else { //Log.DebugFormat("钉钉消息推送成功:{0}", s); } }); }