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
} 


