首页 > 编程语言 >C#钉钉群机器人封装

C#钉钉群机器人封装

时间:2024-10-29 19:02:54浏览次数:3  
标签:set 封装 string get C# 机器人 request IBaseMsg public

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

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

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

public static class DingTalkRobot
    {
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="weebhook">webhook链接</param>
        /// <param name="msgType">消息类型</param>
        /// <param name="msg">消息内容</param>
        /// <param name="atuserlist">@用户的手机号组</param>
        /// <param name="atall">@所有人</param>
        public static void SendMessage(string weebhook, MsgTypeEnum msgType, IBaseMsg msg,string secret="", List<string> 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 += $"&timestamp={_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);
        }
        /// <summary>
        /// 发送text消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="atuserlist"></param>
        /// <returns></returns>
        private static void TextMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "text";
            request.Text_ = msg.Adapt<OapiRobotSendRequest.TextDomain>();
        }
        /// <summary>
        /// 发送链接消息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="MessageUrl">消息链接</param>
        /// <param name="PicUrl">图片链接</param>
        /// <param name="Title">标题</param>
        /// <param name="Text">说明</param>
        private static void LinkMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "link";
            request.Link_ = msg.Adapt<OapiRobotSendRequest.LinkDomain>();
        }
        /// <summary>
        /// 发送Markdown消息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="Title"></param>
        /// <param name="Text"></param>
        private static void MarkdownMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "markdown";
            request.Markdown_ = msg.Adapt<OapiRobotSendRequest.MarkdownDomain>();
        }
        /// <summary>
        ///发送任务卡片消息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="Title"></param>
        /// <param name="Text"></param>
        /// <param name="SingleTitle"></param>
        /// <param name="SingleURL"></param>
        private static void ActionCardMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "actionCard";
            request.ActionCard_ = msg.Adapt<OapiRobotSendRequest.ActioncardDomain>();
        }
        /// <summary>
        /// 发送卡片菜单消息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="msglist"></param>
        private static void FeedCardMessage( OapiRobotSendRequest request, IBaseMsg msg)
        {
            request.Msgtype = "feedCard";
            request.FeedCard_ = msg.Adapt<OapiRobotSendRequest.FeedcardDomain>();
        }
        /// <summary>
        /// @用户
        /// </summary>
        /// <param name="request"></param>
        /// <param name="atuserlist"></param>
        private static void AtUser(this OapiRobotSendRequest request, List<string> atuserlist,bool IsAtAll=false)
        {
            OapiRobotSendRequest.AtDomain at = new OapiRobotSendRequest.AtDomain();
            if (!IsAtAll)
            {
                at.AtMobiles = atuserlist;
            }
            at.IsAtAll = IsAtAll;
            request.At_ = at;
        }
        /// <summary>
        /// 加签
        /// </summary>
        /// <param name="zTime">当前时间戳</param>
        /// <returns></returns>
        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<ActioncardBtn> Btns { get; set; }
    }
    public class ActioncardBtn : IBaseMsg
    {
        public string Title { get; set; }
        public string ActionURL { get; set; }
    }
    public class FeedcardDomainInput: IBaseMsg
    {
        public List<LinkDomainInput> Links { get; set; }
    }
    public interface IBaseMsg
    { 
    }
    public enum MsgTypeEnum
    {
        /// <summary>
        /// 文本消息
        /// </summary>
        TextMessage,
        /// <summary>
        /// 链接消息
        /// </summary>
        LinkMessage,
        /// <summary>
        /// Markdown消息
        /// </summary>
        MarkdownMessage,
        /// <summary>
        /// 卡片消息
        /// </summary>
        ActionCardMessage,
        /// <summary>
        /// 菜单消息
        /// </summary>
        FeedCardMessage
    }

 

标签:set,封装,string,get,C#,机器人,request,IBaseMsg,public
From: https://www.cnblogs.com/lgx5/p/18514163

相关文章

  • 题解:P7306 [COCI2018-2019#1] Strah
    分享一个\(O(nm\logm)\)的方法。分析考虑每次在\(x\)轴上固定左端点,然后移动\(x\)轴上的右端点,并统计答案。考虑如何统计一个\(1\timesn\)的区域的贡献。显然长度为\(i\)的区间有\(n-i+1\)个,所以贡献即为:\[\begin{aligned}f(n)=&\sum^n_{i=1}i\left(n-i+1\ri......
  • 题解:P8245 [COCI2013-2014#3] PAROVI
    题意定义两个整数\(A,B\)之间的距离为这两个整数所有对应位上的数的差的绝对值之和,记为\(\operatorname{dist}(A,B)\)。特别地,如果\(A,B\)两数的位数不相同,则在位数较小的数前补足前导\(0\)。现在,给定两个整数\(L,R\),请你求出所有在区间\([L,R]\)内的整数对的距离和。......
  • CSP-S 2024 游记
    Day0回顾了一下各类字符串算法,切了几道ACAM的题。(果然没考)然后就摆了。Day1上午狠狠的摆。下午去考场。考试过程中被小孩哥干扰,左边砸鼠标,右边砸键盘。有点缺德。T1签。记\(cnt_i\)为战力为\(i\)的怪兽的个数,答案即为\(\max(cnt_i)\)。T2转换成每个车能被下......
  • CCPC 2024 哈尔滨游记
    CCPC2024哈尔滨游记坐标SC,打星队伍,队伍基本上是临时搭伙的。我们学校共有四支队伍参加。Day0走之前模板都没怎么准备,教练说他会准备一些,所以就在走之前随便印了几张。凌晨从天府机场坐飞机到哈尔滨,一下飞机被哈尔滨的寒风吹傻了。这时发现教练给的计算几何板子是电子版......
  • react.js中useMemo和useEffect的区别
    1、当messages发生变化时执行scrollToBottom方法useEffect(()=>{scrollToBottom();},[messages]);constscrollToBottom=()=>{//页面滚动到底部messagesEndRef.current?.scrollIntoView({behavior:"smooth"});}以上例子中,useEffect可以用useMemo代替吗?不可......
  • C#发送内容到钉钉消息
    一、前提工作:配置钉钉机器人见链接:https://star-302.blog.csdn.net/article/details/135649084下边的代码,钉钉安全设置使用“自定义关键词” 二、以text格式发送消息到钉钉usingSystem;usingSystem.Net.Http;usingSystem.Text;usingSystem.Threading.Tasks;using......
  • YOLO11改进 | 卷积模块 | 无卷积步长用于低分辨率图像和小物体的新 CNN 模块SPD-Conv
    秋招面试专栏推荐 :深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转......
  • LeetCode15:三数之和
    原题地址:.-力扣(LeetCode)题目描述给你一个整数数组 nums ,判断是否存在三元组 [nums[i],nums[j],nums[k]] 满足 i!=j、i!=k 且 j!=k ,同时还满足 nums[i]+nums[j]+nums[k]==0 。请你返回所有和为 0 且不重复的三元组。注意:答案中不可以包含重复......
  • LeetCode16:最接近的三数之和
    原题地址:.-力扣(LeetCode)题目描述给你一个长度为 n 的整数数组 nums 和一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在恰好一个解。示例1:输入:nums=[-1,2,1,-4],target=1输出:2......
  • 文本列的性能优化?深入Oracle全文索引
    一.什么是全文索引?全文索引通过分析和处理文本,将文档中的单词分解为词条(tokens),然后存储词条与其所在文档的映射关系。这使得数据库可以快速定位包含特定关键字的记录,而不必对所有文本逐字匹配。二.实验CREATETABLEtest(TEST_IDNUMBER(6),TEST_NAMEVARCHAR2......