using System;
using System.Collections.Generic;
using System.Linq;
using UtilsCore.Result;
namespace UtilsCore
{
///
/// 钉钉自定义机器人
///
public class DingTalkRobot
{
///
/// 调用自定义机器人发Text类型消息
///
/// webHook地址
/// 消息内容
/// 被@人的手机号
/// @所有人时:true,否则为:false
///
public static BaseResult SendTextMessage(string webhook, string content, List atMobiles, bool isAtAll)
{
var result=new BaseResult();
try
{
//@手机号
var mobiles = "";
if (!isAtAll)
{
mobiles = GetAtMobiles(atMobiles);
}
//消息内容
object message = new
{
msgtype = "text",
text = new
{
content = (content+ mobiles).Trim(),
},
at = new
{
atMobiles = atMobiles,
isAtAll = isAtAll
}
};
return SendMessage(webhook, message);
}
catch (Exception ex)
{
result.SetError(ex.Message);
return result;
}
}
///
/// 调用自定义机器人发Link类型消息
///
/// webHook地址
/// 消息标题
/// 消息内容(注:如果太长只会部分展示)
/// 图片url
/// 点击消息跳转的url
///
public static BaseResult SendLinkMessage(string webhook, string title, string text,string picUrl, string messageUrl)
{
var result=new BaseResult();
try
{
object message = new
{
msgtype = "link",
link = new
{
title = title,
text = text,
picUrl = picUrl,
messageUrl = messageUrl
},
};
return SendMessage(webhook, message);
}
catch (Exception ex)
{
result.SetError(ex.Message);
return result;
}
}
///
/// 调用自定义机器人发Markdown类型消息
///
/// webHook地址
/// 消息标题
/// 标题类型等级
/// 消息内容
/// 被@手机号
/// @所有人时:true,否则为:false
///
public static BaseResult SendMarkdownMessage(string webhook, string title, TitleType titleType, List markdownMessages, List atMobiles, bool isAtAll)
{
var result = new BaseResult();
try
{
//@手机号
var mobiles = "";
if (!isAtAll)
{
mobiles = GetAtMobiles(atMobiles);
}
//消息内容头部(标题+被@的人;注:自动换行)
var textTop = GetContentGrade(titleType, title) + "\n >" + mobiles + "\n >";
//消息内容
var text = textTop + RestructureMessage(markdownMessages);
object message = new
{
msgtype = "markdown",
markdown = new
{
title = title,
text = text,
},
at = new
{
atMobiles = atMobiles,
isAtAll = isAtAll,
}
};
return SendMessage(webhook, message);
}
catch (Exception ex)
{
result.SetError(ex.Message);
return result;
}
}
///
/// 调用自定义机器人发整体跳转ActionCard类型消息
///
/// webHook地址
/// 消息标题
/// 消息内容
/// 0-正常发消息者头像,1-隐藏发消息者头像
/// 0-按钮竖直排列,1-按钮横向排列
/// 单个按钮的方案。(设置此项和singleUrl后btns无效。)
/// 点击singleTitle按钮触发的URL
///
public static BaseResult SendActionCardMessage(string webhook, string title, List markdownMessages, int hideAvatar, int btnOrientation, string singleTitle, string singleUrl)
{
var result = new BaseResult();
try
{
object message = new
{
msgtype = "actionCard",
actionCard = new
{
title = title,
text = RestructureMessage(markdownMessages),
hideAvatar = hideAvatar,
btnOrientation = btnOrientation,
singleTitle = singleTitle,
singleURL = singleUrl
},
};
return SendMessage(webhook, message);
}
catch (Exception ex)
{
result.SetError(ex.Message);
return result;
}
}
///
/// 调用自定义机器人发独立跳转ActionCard类型消息
///
/// webHook地址
/// 消息标题
/// 消息内容
/// 0-正常发消息者头像,1-隐藏发消息者头像
/// 0-按钮竖直排列,1-按钮横向排列
/// 按钮集合
///
public static BaseResult SendSingleActionCardMessage(string webhook, string title, List markdownMessages, int hideAvatar, int btnOrientation, List btns)
{
var result = new BaseResult();
try
{
var btnObjs = new List();
if (btns != null && btns.Count > 0)
{
foreach (var item in btns)
{
object obj = new { title = item.Title, actionURL =item.ActionUrl };
btnObjs.Add(obj);
}
}
object message = new
{
actionCard = new
{
title= title,
text = RestructureMessage(markdownMessages),
hideAvatar = hideAvatar,
btnOrientation = btnOrientation,
btns = btnObjs
},
msgtype = "actionCard",
};
return SendMessage(webhook, message);
}
catch (Exception ex)
{
result.SetError(ex.Message);
return result;
}
}
///
/// 调用自定义机器人发FeedCard类型消息
///
/// webHook地址
/// 图片按钮集合
///
public static BaseResult SendFeedCardMessage(string webhook, List links)
{
var result = new BaseResult();
try
{
var linkObjs = new List();
if (links != null && links.Count > 0)
{
foreach (var item in links)
{
object obj = new {title = item.Title, messageURL = item.MessageUrl, picURL = item.PicUrl};
linkObjs.Add(obj);
}
}
object message = new
{
feedCard = new
{
links = linkObjs,
},
msgtype = "feedCard"
};
return SendMessage(webhook, message);
}
catch (Exception ex)
{
result.SetError(ex.Message);
return result;
}
}
///
/// 调用自定义机器人发消息
///
/// webHook地址
/// 消息信息
///
private static BaseResult SendMessage(string webhook, object message)
{
var result=new BaseResult();
try
{
var webHelper=new WebHelper();
result.Result=webHelper.DoPost(webhook, message, out var tips);
result.Msg = tips;
if (tips == "请求成功") return result;
result.SetError(tips);
return result;
}
catch (Exception ex)
{
result.SetError(ex.Message);
return result;
}
}
///
/// 重构消息内容
///
/// 消息内容
///
private static string RestructureMessage(List markdownMessages)
{
var text = "";
foreach (var item in markdownMessages.OrderBy(x => x.Index))
{
switch (item.MarkdownType)
{
case MarkdownType.文本:
{
switch (item.Text.ContentType)
{
case ContentType.默认:
item.Text.Content = item.Text.Content;
break;
case ContentType.加粗:
item.Text.Content = "**" + item.Text.Content + "**";
break;
case ContentType.斜体:
item.Text.Content = "*" + item.Text.Content + "*";
break;
default:
item.Text.Content = item.Text.Content;
break;
}
//文本等级赋值
item.Text.Content = GetContentGrade(item.Text.ContentGrade, item.Text.Content);
//判断是否换行
text += (item.IsLineFeed) ? item.Text.Content + "\n >" : item.Text.Content;
break;
}
case MarkdownType.图片:
text += (item.IsLineFeed) ? "![screenshot](" + item.Text.ImgUrl + ")\n >" : "![screenshot](" + item.Text.ImgUrl + ")";
break;
case MarkdownType.链接:
text += (item.IsLineFeed) ? "[" + item.Text.Content + "](" + item.Text.Url + ")\n >" : "[" + item.Text.Content + "](" + item.Text.Url + ")";
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return text;
}
///
/// 获取被@人的手机号
///
/// 被@人的手机号
///
private static string GetAtMobiles(List atMobiles)
{
var mobiles = "";
if (atMobiles == null || atMobiles.Count <= 0) return mobiles;
foreach (var item in atMobiles)
{
mobiles += "@" + item;
}
return mobiles;
}
///
/// 获取等级文本
///
/// 文本类型
/// 文本
///
private static string GetContentGrade(TitleType titleType, string title)
{
switch (titleType)
{
case TitleType.默认:
break;
case TitleType.一级:
title = "# " + title;
break;
case TitleType.二级:
title = "## " + title;
break;
case TitleType.三级:
title = "### " + title;
break;
case TitleType.四级:
title = "#### " + title;
break;
case TitleType.五级:
title = "##### " + title;
break;
case TitleType.六级:
title = "###### " + title;
break;
default:
break;
}
return title;
}
}
#region 公共模型
///
/// Markdown消息内容类
///
public class MarkdownMessage
{
///
/// 内容坐标索引
///
public int Index { get; set; }
///
/// 内容类型(文字、图片、链接)
///
public MarkdownType MarkdownType { get; set; }
///
/// 内容(注:文字类型的内容中禁止字符["#"、"*"、"["、"]"、"!"];
/// 图片类型和链接类型的内容传可访问的http地址即可)
///
public Text Text { get; set; }
///
/// 是否换行
///
public bool IsLineFeed { get; set; }
}
///
/// 内容
///
public class Text
{
///
/// 文本或链接显示文字
///
public string Content { get; set; }
///
/// 图片链接
///
public string ImgUrl { get; set; }
///
/// 超链接地址
///
public string Url { get; set; }
///
/// 文本类型
///
public ContentType ContentType { get; set; }
///
/// 文本等级
///
public TitleType ContentGrade { get; set; }
}
///
/// 超链接按钮
///
public class Btn
{
///
/// 标题
///
public string Title { get; set; }
///
/// 标题对应的超链接
///
public string ActionUrl { get; set; }
}
///
/// 超链接按钮带图片
///
public class Link
{
///
/// 标题
///
public string Title { get; set; }
///
/// 超链接
///
public string MessageUrl { get; set; }
///
/// 图片超链接
///
public string PicUrl { get; set; }
}
#endregion
#region 枚举
///
/// 文本类型
///
public enum ContentType
{
///
/// 默认
///
默认=0,
///
/// 加粗
///
加粗 = 1,
///
/// 斜体
///
斜体 = 2
}
///
/// Markdown消息内容类型
///
public enum MarkdownType
{
///
/// 文本
///
文本 = 1,
///
/// 图片
///
图片 = 2,
///
/// 链接
///
链接 = 3,
}
///
/// 标题(文本)类型等级
///
public enum TitleType
{
///
/// 默认
///
默认 = 0,
///
/// 一级
///
一级 = 1,
///
/// 二级
///
二级 = 2,
///
/// 三级
///
三级 = 3,
///
/// 四级
///
四级 = 4,
///
/// 五级
///
五级 = 5,
///
/// 六级
///
六级 = 6,
}
#endregion
}
github源码:https://github.com/agoling/UtilsSharp.Standard