Esempio n. 1
0
        /// <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}&timestamp ={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;
Esempio n. 2
0
        /// <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}&timestamp={timestamp}&sign={sign}";
            //Log.Error($"钉钉消息推送URL;{ webHookUrl}&timestamp ={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);
                }
            });
        }