C#发送内容到钉钉消息

一、前提工作:配置钉钉机器人

见链接:https://star-302.blog.csdn.net/article/details/135649084

下边的代码,钉钉安全设置使用“自定义关键词”

 

二、以text格式发送消息到钉钉

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using static System.Net.Mime.MediaTypeNames;

class Program
{
    static async Task Main(string[] args)
    {
        // 钉钉机器人的Webhook URL
        string webhookUrl = "https://oapi.dingtalk.com/robot/send?access_token=6fbcb84ec92786c94f8513c0338a88aca9dfaaf56ddbeef40ddfgdf52451c";//这里是钉钉机器人复制过来的

        // 自定义关键字
        string customKeyword = "MY_CUSTOM_KEYWORD";//钉钉机器人安全设置中配置

        //**方法一:text格式 * *"
        // 测试者和当前时间
        string tester = "张三";
        DateTime testTime = DateTime.Now;
         测试结果
        string functionalTest = "失败";

        // 构建消息内容,将关键字嵌入到消息体中
        var content = new
        {
            msgtype = "text",
            keyword = customKeyword,
            text = new
            {
                content = $"测试: {customKeyword}\r\n" +
               $"测试者:{tester}\r\n" +
               $"当前时间:{testTime}\r\n" +
               $"测试结果:{functionalTest}\r\n"
            },
            at = new { atMobiles = new string[] { }, isAtAll = true }
        };

        // 序列化JSON数据
        string jsonContent = JsonConvert.SerializeObject(content);

        // 发送POST请求
        using (var client = new HttpClient())
        {
            var requestContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(webhookUrl, requestContent);
            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine($"Failed to send message: {response.StatusCode}");
            }
            else
            {
                Console.WriteLine("Message sent successfully.");
            }
        }
        Console.ReadLine();
    }
}

测试结果:

 

三、以markdown格式发送消息到钉钉

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using static System.Net.Mime.MediaTypeNames;

class Program
{
    static async Task Main(string[] args)
    {
        // 钉钉机器人的Webhook URL
        string webhookUrl = "https://oapi.dingtalk.com/robot/send?access_token=6fbcb84ec92786c94f8513c0338a88acab76aae52451c";

        // 自定义关键字
        string customKeyword = "MY_CUSTOM_KEYWORD";

        //**方法二:markdown格式**"
        // 测试者和当前时间
        string tester = "张三";
        DateTime testTime = DateTime.Now;

        // 测试结果
        bool functionalTest = true;
        // 构建Markdown消息
        string markdownText = $@"
          #### 测试结果{customKeyword}:
      - **测试者**: {tester}
      - **当前时间**: {testTime:yyyy-MM-dd HH:mm:ss}
      - **测试结果**: {(functionalTest ? "**<font color='#008000'>SUCCESS</font>**" : "**<font color='#FF0000'>FAIL</font>**")} ";
     //测试结果加粗、SUCCESS绿色显示,FAIL红色显示
// 构建消息内容 var content = new { msgtype = "markdown", keyword = customKeyword, markdown = new { title = "测试结果", text = markdownText }, at = new { atMobiles = new string[] { }, isAtAll = true } }; // 序列化JSON数据 string jsonContent = JsonConvert.SerializeObject(content); // 发送POST请求 using (var client = new HttpClient()) { var requestContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(webhookUrl, requestContent); if (!response.IsSuccessStatusCode) { Console.WriteLine($"Failed to send message: {response.StatusCode}"); } else { Console.WriteLine("Message sent successfully."); } } Console.ReadLine(); } }

测试结果:

 

posted @ 2024-10-29 19:03  每天进步多一点  阅读(29)  评论(0编辑  收藏  举报