栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.net C# 最全最实用钉钉群机器人封装

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.net C# 最全最实用钉钉群机器人封装

1 打开群设置 > 智能群助手 > 添加机器人 > 自定义

2 我选择的是加签的方法,其他的比较简单。

 

 3 只需要按照需求传入对应的参数即可,当然如果没有加签可以不传签名

public static class DingTalkRobot
    {
        /// 
        /// 发送消息
        /// 
        /// webhook链接
        /// 消息类型
        /// 消息内容
        /// @用户的手机号组
        /// @所有人
        public static void SendMessage(string weebhook, MsgTypeEnum msgType, IBaseMsg msg,string secret="", List atuserlist = null, bool atall = false)
        {
            var _timespan = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() * 1000;
            var url = weebhook;
            if (!string.IsNullOrEmpty(secret))
            {
                var _sign = addSign(_timespan, secret);
                url += $"×tamp={_timespan}&sign={_sign}";
            }
            IDingTalkClient client = new DefaultDingTalkClient(url);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            var _this = typeof(DingTalkRobot);
            var action = _this.GetMethod(msgType.ToString(), BindingFlags.Static | BindingFlags.NonPublic);
            action.Invoke(null,new object[] { request , msg });
            request.AtUser(atuserlist, atall);
            OapiRobotSendResponse response = client.Execute(request);
        }
        /// 
        /// 发送text消息
        /// 
        /// 
        /// 
        /// 
        private static void TextMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "text";
            request.Text_ = msg.Adapt();
        }
        /// 
        /// 发送链接消息
        /// 
        /// 
        /// 消息链接
        /// 图片链接
        /// 标题
        /// 说明
        private static void LinkMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "link";
            request.Link_ = msg.Adapt();
        }
        /// 
        /// 发送Markdown消息
        /// 
        /// 
        /// 
        /// 
        private static void MarkdownMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "markdown";
            request.Markdown_ = msg.Adapt();
        }
        /// 
        ///发送任务卡片消息
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        private static void ActionCardMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "actionCard";
            request.ActionCard_ = msg.Adapt();
        }
        /// 
        /// 发送卡片菜单消息
        /// 
        /// 
        /// 
        private static void FeedCardMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "feedCard";
            request.FeedCard_ = msg.Adapt();
        }
        /// 
        /// @用户
        /// 
        /// 
        /// 
        private static void AtUser(this OapiRobotSendRequest request, List atuserlist,bool IsAtAll=false)
        {
            OapiRobotSendRequest.AtDomain at = new OapiRobotSendRequest.AtDomain();
            if (!IsAtAll)
            {
                at.AtMobiles = atuserlist;
            }
            at.IsAtAll = IsAtAll;
            request.At_ = at;
        }
        /// 
        /// 加签
        /// 
        /// 当前时间戳
        /// 
        private static string addSign(long zTime,string secret)
        {
            string stringToSign = zTime + "n" + secret;
            var encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] messageBytes = encoding.GetBytes(stringToSign);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                return HttpUtility.UrlEncode(Convert.ToBase64String(hashmessage), Encoding.UTF8);
            }
        }
    }
public class TextDomainInput : IBaseMsg
    {
        public string Content { get; set; }   
    }
    public class LinkDomainInput : IBaseMsg
    {
        public string MessageUrl { get; set; }
        public string PicUrl { get; set; }
        public string Title { get; set; }
        public string Text { get; set; }
    }
    public class MarkdownDomainInput : IBaseMsg
    {
        public string Title { get; set; }
        public string Text { get; set; }
    }
    public class ActioncardDomainInput : IBaseMsg
    {
        public string Title { get; set; }
        public string Text { get; set; }
        public string SingleTitle { get; set; }
        public string SingleURL { get; set; }
        public string btnOrientation { get; set; }
        public List Btns { get; set; }
    }
    public class ActioncardBtn : IBaseMsg
    {
        public string Title { get; set; }
        public string ActionURL { get; set; }
    }
    public class FeedcardDomainInput: IBaseMsg
    {
        public List Links { get; set; }
    }
    public interface IBaseMsg
    { 
    }
    public enum MsgTypeEnum
    {
        /// 
        /// 文本消息
        /// 
        TextMessage,
        /// 
        /// 链接消息
        /// 
        LinkMessage,
        /// 
        /// Markdown消息
        /// 
        MarkdownMessage,
        /// 
        /// 卡片消息
        /// 
        ActionCardMessage,
        /// 
        /// 菜单消息
        /// 
        FeedCardMessage
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910546.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号