C#钉钉群机器人封装
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 += $"×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); } /// <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 }