• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#markdown实操(用于钉钉的消息通知)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

先上效果:

 

 再上代码(只有markdown的部分):

StringBuilder content = new StringBuilder();
            content.Append("**销售订单系统提醒:**");
            content.Append("\n\n");//换行
            content.Append("------"); //下划线
            content.Append("\n\n");//换行            
            content.Append(txtSendContent.Text);
            content.Append("\n\n  ");//多个\n只能换一行,加上一个空格即可辅助完成换行
            content.Append("\n\n");
            content.Append("<br>"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var sr = m_DingDingHelper.SendActionCardMessageToUser("销售订单管理系统", content.ToString(), "查看详情", "https://ding-doc.dingtalk.com/", txtusesid.Text);
            

DingDingHelper类自己封装的,仅用于参考,功能很少,大家见谅

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DingDingAPP
{
    public class DingDingHelper
    {
        
        string appkey = "*****";
        string appsecret = "******";
        string agentid = "*****";  
        string corpId = "******";//企业号
        string AccessToken = String.Empty;

        #region 通过免登授权码和access_token获取用户的userid
        public string GetUserIdByCode(string code)
        {
            GetToken();
            if (string.IsNullOrWhiteSpace(AccessToken) || string.IsNullOrWhiteSpace(code))
                return "";

            try
            {
                string AccessUrl = string.Format("https://oapi.dingtalk.com/user/getuserinfo?access_token={0}&code={1}", AccessToken, code);
                //请求获取token  GET方式
                Newtonsoft.Json.Linq.JToken json = Newtonsoft.Json.Linq.JToken.Parse(HttpHelper.HttpGet(AccessUrl, null));
                return json["userid"].ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        #endregion

        #region 根据手机号获取用户id
        /// <summary>
        /// 根据手机号获取用户id
        /// </summary>
        /// <param name="phoneNumber">用户手机号</param>
        /// <returns></returns>
        public string GetUserIdByPhoneNumber(string phoneNumber)
        {
            GetToken();
            if (string.IsNullOrWhiteSpace(AccessToken) || string.IsNullOrWhiteSpace(phoneNumber))
                return "";

            string MessageUrl = String.Format("https://oapi.dingtalk.com/user/get_by_mobile?access_token={0}&mobile={1}", AccessToken, phoneNumber);
            
            try
            {
                var sr = HttpHelper.HttpGet(MessageUrl, null);
                //Newtonsoft.Json.Linq.JToken json = Newtonsoft.Json.Linq.JToken.Parse(sr);
                return sr;// json["userid"].ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
            
        }
        #endregion

        #region 根据用户id获取用户详细信息
        /// <summary>
        /// 根据用户id获取用户详细信息
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns></returns>
        public string GetUseInfoByUid(string userid)
        {
            GetToken();
            if (string.IsNullOrWhiteSpace(AccessToken) || string.IsNullOrWhiteSpace(userid))
                return "";

            try
            {
                string AccessUrl = string.Format("https://oapi.dingtalk.com/user/get?access_token={0}&userid={1}", AccessToken, userid);
                //请求获取token  GET方式
                return HttpHelper.HttpGet(AccessUrl, null);               
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        #endregion

        #region 获取TOKEN
        public void GetToken()
        {
            if (string.IsNullOrWhiteSpace(AccessToken))
            {
                string AccessUrl = string.Format("https://oapi.dingtalk.com/gettoken?appkey={0}&appsecret={1}", appkey, appsecret);
                //请求获取token  GET方式
                Newtonsoft.Json.Linq.JToken json = Newtonsoft.Json.Linq.JToken.Parse(HttpHelper.HttpGet(AccessUrl, null));
                AccessToken = json["access_token"].ToString();
            }
        }
        #endregion

        #region 消息推送
        /// <summary>
        /// 文本消息推送(Text格式)
        /// </summary>
        /// <param name="text">推送内容</param>
        /// <param name="userid_list">用户id,多个用户用,隔开(如:123,456)</param>
        public string SendTextMessageToUser(string text,string userid_list)
        {
            GetToken();
            if (string.IsNullOrWhiteSpace(AccessToken) || string.IsNullOrWhiteSpace(text)|| string.IsNullOrWhiteSpace(userid_list))
                return "";

            string MessageUrl = String.Format("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={0}", AccessToken);
            

            var json_req = SendCommonText(text, userid_list);
            //var json_req = SendCommonTextAndTitle("销售订单管理系统", content.ToString(), txtusesid.Text);
            //var json_req = SendCardMsg("销售订单管理系统", content.ToString(), "查看详情", "https://ding-doc.dingtalk.com/", txtusesid.Text);

            string jsonRequest = JsonConvert.SerializeObject(json_req);//将对象转换为json
            //POST请求 消息推送
            var sr = HttpHelper.HttpPost(MessageUrl, jsonRequest);
            return sr;
        }

        /// <summary>
        /// 文本消息推送(MarkDown格式)
        /// </summary>
        /// <param name="title">提醒用户的标题</param>
        /// <param name="text">提醒内容(支持markdown文本解析)</param>
        /// <param name="userid_list">用户id,多个用户用,隔开(如:123,456)</param>
        /// <returns></returns>
        public string SendMarkDownMessageToUser(string title,string text, string userid_list)
        {
            GetToken();
            if (string.IsNullOrWhiteSpace(AccessToken) || string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(userid_list))
                return "";

            string MessageUrl = String.Format("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={0}", AccessToken);


            //var json_req = SendCommonText(text, userid_list);
            var json_req = SendCommonTextAndTitle(title, text, userid_list);
            //var json_req = SendCardMsg("销售订单管理系统", content.ToString(), "查看详情", "https://ding-doc.dingtalk.com/", txtusesid.Text);

            string jsonRequest = JsonConvert.SerializeObject(json_req);//将对象转换为json
            //POST请求 消息推送
            var sr = HttpHelper.HttpPost(MessageUrl, jsonRequest);
            return sr;
        }

        /// <summary>
        /// 文本消息推送(MarkDown格式)
        /// </summary>
        /// <param name="title">提醒用户的标题</param>
        /// <param name="text">提醒内容(支持markdown文本解析)</param>
        /// <param name="linkText">链接文本</param>
        /// <param name="linkUrl">链接地址</param>
        /// <param name="userid_list">用户id,多个用户用,隔开(如:123,456)</param>
        /// <returns></returns>
        public string SendActionCardMessageToUser(string title, string text,string linkText,string linkUrl ,string userid_list)
        {
            GetToken();
            if (string.IsNullOrWhiteSpace(AccessToken) || string.IsNullOrWhiteSpace(text)                 
                || string.IsNullOrWhiteSpace(userid_list))
                return "";

            string MessageUrl = String.Format("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={0}", AccessToken);

            var json_req = SendCardMsg(title, text, linkText, linkUrl, userid_list);

            string jsonRequest = JsonConvert.SerializeObject(json_req);//将对象转换为json
            //POST请求 消息推送
            var sr = HttpHelper.HttpPost(MessageUrl, jsonRequest);
            return sr;
        }
        #endregion

        #region 推送消息类型
        /// <summary>
        /// 推送普通文本内容
        /// </summary>
        /// <param name="content"></param>
        /// <param name="usesid"></param>
        /// <returns></returns>
        public object SendCommonText(string content, string usesid)
        {
            return new
            {
                userid_list = usesid,  //接受推送userid,不同用户用|分割
                //dept_id_list = "127577105",   //接受推送部门id
                agent_id = agentid,//应用Id
                msgtype = "text", //推送类型
                msg = new
                {
                    msgtype = "text",
                    text = new { content = content.ToString() }//内容text

                }
            };
        }
        /// <summary>
        /// 推送markdown内容
        /// </summary>
        /// <param name="_title"></param>
        /// <param name="_content"></param>
        /// <param name="usesid"></param>
        /// <returns></returns>
        public object SendCommonTextAndTitle(string _title, string _content, string usesid)
        {
            return new
            {
                userid_list = usesid,  //接受推送userid,不同用户用|分割
                dept_id_list = "",   //接受推送部门id
                agent_id = agentid,//应用Id                
                msg = new
                {
                    msgtype = "markdown",//推送类型
                    markdown = new
                    {
                        title = _title,
                        text = _content//内容text
                    }
                }
            };
        }
        /// <summary>
        /// 推送action_card格式内容
        /// </summary>
        /// <param name="_title">标题</param>
        /// <param name="_content">内容</param>
        /// <param name="_single_title">链接标题</param>
        /// <param name="_single_url">链接地址</param>
        /// <param name="usesid"></param>
        /// <returns></returns>
        public object SendCardMsg(string _title, string _content, string _single_title, string _single_url, string usesid)
        {
            return new
            {
                userid_list = usesid,  //接受推送userid,不同用户用|分割
                //dept_id_list = "",   //接受推送部门id
                agent_id = agentid,//应用Id               
                msg = new
                {
                    msgtype = "action_card",
                    action_card = new
                    {
                        title = _title,//提示信息
                        markdown = _content,
                        single_title = _single_title,
                        single_url = _single_url
                    }
                }
            };
        }

        #endregion
    }
    
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#Datetime各种格式发布时间:2022-07-10
下一篇:
C#读取EXCEL文件的三种经典方法发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap