Beispiel #1
0
        /// <summary>
        /// 发送自定义机器人消息(FeedCard类型:公号文章列表)
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="linkUrl"></param>
        /// <param name="picUrl"></param>
        /// <param name="mobiles"></param>
        /// <returns></returns>
        public static bool SendFeedCardMsg(string access_token, string title, string content, List <FeedCard> feedcardList)
        {
            IDingTalkClient      client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
            OapiRobotSendRequest request = new OapiRobotSendRequest();

            request.Msgtype = "feedCard";
            OapiRobotSendRequest.FeedcardDomain     feedCard = new OapiRobotSendRequest.FeedcardDomain();
            List <OapiRobotSendRequest.LinksDomain> links    = new List <OapiRobotSendRequest.LinksDomain>();

            foreach (FeedCard card in feedcardList)
            {
                OapiRobotSendRequest.LinksDomain link = new OapiRobotSendRequest.LinksDomain();
                link.Title      = card.Title;
                link.MessageURL = card.MessageURL;
                link.PicURL     = card.PicURL;
                links.Add(link);
            }
            feedCard.Links = links;
            OapiRobotSendResponse response = client.Execute(request);

            if (response.Errcode == 0)
            {
                return(true);
            }
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// 发送自定义机器人消息(Actioncard类型)
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="linkUrl"></param>
        /// <param name="picUrl"></param>
        /// <param name="mobiles"></param>
        /// <returns></returns>
        public static bool SendActionCardMsg(string access_token, string title, string content, string linkUrl, string picUrl, List <string> mobiles)
        {
            IDingTalkClient      client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
            OapiRobotSendRequest request = new OapiRobotSendRequest();

            request.Msgtype = "actionCard";
            OapiRobotSendRequest.ActioncardDomain actionCard = new OapiRobotSendRequest.ActioncardDomain();
            actionCard.Title          = title;
            actionCard.Text           = content;
            actionCard.SingleTitle    = "查看详情";
            actionCard.SingleURL      = linkUrl;
            actionCard.BtnOrientation = "1"; //0-按钮竖直排列,1-按钮横向排列
            actionCard.HideAvatar     = "1"; //0-正常发消息者头像,1-隐藏发消息者头像
            request.ActionCard_       = actionCard;
            if (mobiles != null && mobiles.Count > 0)
            {
                OapiRobotSendRequest.AtDomain at = new OapiRobotSendRequest.AtDomain();
                at.AtMobiles = mobiles;
                request.At_  = at;
            }
            OapiRobotSendResponse response = client.Execute(request);

            if (response.Errcode == 0)
            {
                return(true);
            }
            return(false);
        }
Beispiel #3
0
        private static string SendDingDingMsg(RobotSetting setting, string content)
        {
            long   timestamp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
            string strSign   = timestamp + "\n" + setting.Secret;
            string sign      = string.Empty;

            using (var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(setting.Secret)))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(strSign));
                sign = HttpUtility.UrlEncode(Convert.ToBase64String(hashmessage), Encoding.UTF8);
            }

            IDingTalkClient client = new DefaultDingTalkClient($"{setting.SendUrl}&timestamp={timestamp}&sign={sign}");

            OapiRobotSendRequest.TextDomain text = new OapiRobotSendRequest.TextDomain()
            {
                Content = content
            };
            OapiRobotSendRequest request = new OapiRobotSendRequest()
            {
                Msgtype = "text",
                Text_   = text
            };
            OapiRobotSendResponse response = client.Execute(request);

            return(response.Body);
        }
Beispiel #4
0
        /// <summary>
        /// 发送机器人推送
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static Result SendRobotMsg(string msg)
        {
            Result result = new Result()
            {
                Code = 1
            };
            string               robotToken = "da95d6bcd4e389e3bce4eb518eb08dc23a6f315ea5987eb247c21989f2d00376";
            IDingTalkClient      client     = new DefaultDingTalkClient(dingUrl + "/robot/send");
            OapiRobotSendRequest req        = new OapiRobotSendRequest()
            {
                Msgtype = "text",
                Text_   = new OapiRobotSendRequest.TextDomain
                {
                    Content = "推送:" + msg
                },
                At_ = new OapiRobotSendRequest.AtDomain
                {
                    AtMobiles = new List <string> {
                        "13968414187"
                    },
                    IsAtAll = false
                }
            };
            DateTime timeNow   = DateTime.Now;
            long     timestamp = timeNow.ToUnixTimestampByMilliseconds();
            String   secret    = "SEC9070624c833e627790c5aaad181a96c050cbc5e43c1653a09b65259f1a67f091";

            req.AddOtherParameter("sign", ExHelper.AddSign(secret, timestamp));
            req.SetHttpMethod("POST");
            OapiRobotSendResponse rsp = client.Execute(req, robotToken, timeNow);

            if (rsp != null && rsp.Errcode == 0)
            {
                result.Obj     = rsp;
                result.Message = "发送成功";
            }
            else if (rsp != null && rsp.Errcode != 0)
            {
                result.Code    = 0;
                result.Obj     = rsp;
                result.Message = rsp.Errmsg;
            }
            else
            {
                result.Code    = 0;
                result.Message = "发送机器人推送失败";
            }
            return(result);
        }
        /// <summary>
        /// 发送钉钉消息
        /// </summary>
        /// <param name="serverUrl">服务地址</param>
        /// <param name="message">消息内容</param>
        /// <param name="atMobiles">@手机号</param>
        /// <returns></returns>
        public static string SendMessage(string serverUrl, string message, params string[] atMobiles)
        {
            try
            {
                if (!message.IsValid())
                {
                    return("消息内容不能为空");
                }

                var client = GetClient(serverUrl);
                if (null == client)
                {
                    return("获取客户端实例失败");
                }

                var textDomain = new OapiRobotSendRequest.TextDomain
                {
                    Content = message.GetValue(),
                };

                var request = new OapiRobotSendRequest
                {
                    Msgtype = "text",
                    Text_   = textDomain,
                };

                if (!atMobiles.IsEmpty())
                {
                    request.At_ = new OapiRobotSendRequest.AtDomain
                    {
                        AtMobiles = atMobiles.ToList(),
                    };
                }

                var response = client.Execute(request);
                if (null == response || response.IsError)
                {
                    return($"发送消息失败:{(response?.ErrMsg).GetValue("未知")}");
                }

                return(string.Empty);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Beispiel #6
0
        /// <summary>
        /// 发送自定义机器人消息(text类型)
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="content"></param>
        /// <param name="mobiles"></param>
        public static bool SendTextMsg(string access_token, string content, List <string> mobiles)
        {
            IDingTalkClient      client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
            OapiRobotSendRequest request = new OapiRobotSendRequest();

            request.Msgtype = "text";
            OapiRobotSendRequest.TextDomain text = new OapiRobotSendRequest.TextDomain();
            text.Content  = content;
            request.Text_ = text;
            OapiRobotSendRequest.AtDomain at = new OapiRobotSendRequest.AtDomain();
            at.AtMobiles = mobiles;
            request.At_  = at;
            OapiRobotSendResponse response = client.Execute(request);

            if (response.Errcode == 0)
            {
                return(true);
            }
            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// 发送自定义机器人消息(markdown类型)
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="linkUrl"></param>
        /// <param name="picUrl"></param>
        /// <param name="mobiles"></param>
        public static bool SendMarkdownMsg(string access_token, string title, string content, string linkUrl, List <string> mobiles)
        {
            IDingTalkClient      client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
            OapiRobotSendRequest request = new OapiRobotSendRequest();

            request.Msgtype = "markdown";
            OapiRobotSendRequest.MarkdownDomain markdown = new OapiRobotSendRequest.MarkdownDomain();
            markdown.Title    = title;
            markdown.Text     = content;
            request.Markdown_ = markdown;
            OapiRobotSendRequest.AtDomain at = new OapiRobotSendRequest.AtDomain();
            at.AtMobiles = mobiles;
            request.At_  = at;
            OapiRobotSendResponse response = client.Execute(request);

            if (response.Errcode == 0)
            {
                return(true);
            }
            return(false);
        }
Beispiel #8
0
        /// <summary>
        /// 发送自定义机器人消息(link类型)
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="linkUrl"></param>
        /// <param name="picUrl"></param>
        /// <param name="mobiles"></param>
        public static bool SendLinkMsg(string access_token, string title, string content, string linkUrl, string picUrl, List <string> mobiles)
        {
            IDingTalkClient      client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
            OapiRobotSendRequest request = new OapiRobotSendRequest();

            request.Msgtype = "link";
            OapiRobotSendRequest.LinkDomain link = new OapiRobotSendRequest.LinkDomain();
            link.MessageUrl = linkUrl;
            link.PicUrl     = picUrl;
            link.Title      = title;
            link.Text       = content;
            request.Link_   = link;
            OapiRobotSendRequest.AtDomain at = new OapiRobotSendRequest.AtDomain();
            at.AtMobiles = mobiles;
            request.At_  = at;
            OapiRobotSendResponse response = client.Execute(request);

            if (response.Errcode == 0)
            {
                return(true);
            }
            return(false);
        }