当前位置: 代码网 > it编程>编程语言>Asp.net > C#实现钉钉消息推送过程

C#实现钉钉消息推送过程

2025年07月23日 Asp.net 我要评论
前言想起之前有个项目需要使用钉钉消息推送报警数据到群里,提醒群里人员知道设备报警。这篇是通过c#实现钉钉消息推送,因为钉钉机器人设定需设置安全设置,有三个(自定义关键词、加签、ip地址(段)),目前是

前言

想起之前有个项目需要使用钉钉消息推送报警数据到群里,提醒群里人员知道设备报警。

这篇是通过c#实现钉钉消息推送,因为钉钉机器人设定需设置安全设置,有三个(自定义关键词、加签、ip地址(段)),目前是设置自定义关键词通过c#消息推送。

一、钉钉机器人如何设置

1、钉钉机器人设置需建群(在群里才能机器人)

2、点击群设置,在群管理里点击机器人管理

3、点击添加机器人选择自定义机器人点击添加

4、在添加钉钉机器人时需要确定加密方式有以下几种:

  • 自定义关键字
  • 加签
  • ip地址段

注:因目前所选方式为第一种,所以需输入自定义关键字,如:"报警:"

5、点击完成后,钉钉会给出一个webhook,需复制webhook在c#中使用。

6、如果自定义机器人在群里发送招呼话语,说明已建造设置成功。

二、c#实现钉钉消息推送

1.钉钉消息类型

钉钉消息有六种消息类型,总结如下:

类型是否支持交互关键特性适用场景
文本(text)纯文本,支持@成员简单通知、报警
markdown支持markdown格式化复杂内容(如日志报告)
链接(link)标题+图片+跳转链接带缩略图的通知
actioncard单、多 1-2个可点击按钮审批、任务处理
feedcard多条链接列表聚合通知(如新闻列表)

2.代码实现

使用c#控制台实现钉钉机器人消息推送。

1)dingtalkrobot类型

钉钉机器人信息推送,代码如下(示例):

using system;
using system.net.http;
using system.text;
using system.threading.tasks;

public class dingtalkrobot
{
    private readonly string _webhookurl;
    private readonly httpclient _httpclient;

    public dingtalkrobot(string webhookurl)
    {
        _webhookurl = webhookurl ?? throw new argumentnullexception(nameof(webhookurl));
        _httpclient = new httpclient();
    }

    /// <summary>
    /// 发送文本消息
    /// </summary>
    /// <param name="content">消息内容</param>
    /// <param name="atmobiles">需要@的手机号数组</param>
    /// <param name="isatall">是否@所有人</param>
    public async task sendtextmessageasync(string content, string[] atmobiles = null, bool isatall = false)
    {
        var message = new
        {
            msgtype = "text",
            text = new
            {
                content = content
            },
            at = new
            {
                atmobiles = atmobiles ?? array.empty<string>(),
                isatall = isatall
            }
        };

        await sendmessageasync(message);
    }

    /// <summary>
    /// 发送markdown消息
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="text">markdown格式内容</param>
    /// <param name="atmobiles">需要@的手机号数组</param>
    /// <param name="isatall">是否@所有人</param>
    public async task sendmarkdownmessageasync(string title, string text, string[] atmobiles = null, bool isatall = false)
    {
        var message = new
        {
            msgtype = "markdown",
            markdown = new
            {
                title = title,
                text = text
            },
            at = new
            {
                atmobiles = atmobiles ?? array.empty<string>(),
                isatall = isatall
            }
        };

        await sendmessageasync(message);
    }

    /// <summary>
    /// 发送链接消息
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="text">内容</param>
    /// <param name="messageurl">跳转链接</param>
    /// <param name="picurl">图片链接(可选)</param>
    public async task sendlinkmessageasync(string title, string text, string messageurl, string picurl = "")
    {
        var message = new
        {
            msgtype = "link",
            link = new
            {
                title = title,
                text = text,
                messageurl = messageurl,
                picurl = picurl
            }
        };

        await sendmessageasync(message);
    }

    /// <summary>
    /// 发送 单按钮 actioncard​消息
    /// </summary>
    /// <param name="title">消息标题,显示在消息顶部</param>
    /// <param name="text">消息内容,支持markdown语法</param>
    /// <param name="singletitle">单个按钮的文本(不超过20字符)</param>
    /// <param name="singleurl">按钮跳转链接(需http/https)</param>
    /// <param name="atmobiles">需要@的群成员手机号数组</param>
    /// <param name="isatall">是否@所有人(慎用)</param>
    /// <returns></returns>
    public async task sendactioncardasync(string title, string text, string singletitle, string singleurl, string[] atmobiles = null, bool isatall = false)
    {
        var message = new
        {
            msgtype = "actioncard",
            actioncard = new
            {
                title = title,
                text = text,
                singletitle = singletitle,
                singleurl = singleurl,
                btnorientation = "0" // "0"垂直按钮,"1"水平按钮
            },
            at = new
            {
                atmobiles = atmobiles ?? array.empty<string>(),
                isatall = isatall
            }
        };
        await sendmessageasync(message);
    }

    /// <summary>
    /// 发送 多按钮 actioncard消息
    /// </summary>
    /// <param name="title">消息标题</param>
    /// <param name="text">消息内容</param>
    /// <param name="buttons">按钮列表(最多2个)</param>
    /// <param name="btnorientation">按钮排列方向:“0”(垂直,默认) 或 “1”(水平)</param>
    /// <param name="atmobiles">需要@的手机号数组</param>
    /// <param name="isatall">是否@所有人</param>
    /// <returns></returns>
    public async task sendmultiactioncardasync(string title, string text, list<dingtalkbutton> buttons, string btnorientation = "0", // "0"垂直,"1"水平
    string[] atmobiles = null, bool isatall = false)
    {
        var message = new
        {
            msgtype = "actioncard",
            actioncard = new
            {
                title = title,
                text = text,
                btns = buttons.select(b => new { title = b.title, actionurl = b.url }).toarray(),
                btnorientation = btnorientation
            },
            at = new
            {
                atmobiles = atmobiles ?? array.empty<string>(),
                isatall = isatall
            }
        };
        await sendmessageasync(message);
    }

    /// <summary>
    /// 发送feedcard消息
    /// </summary>
    /// <param name="links">链接列表(最多5条)</param>
    /// <returns></returns>
    public async task sendfeedcardasync(list<feedcardlink> links)
    {
        var message = new
        {
            msgtype = "feedcard",
            feedcard = new
            {
                links = links.select(link => new
                {
                    title = link.title,
                    messageurl = link.messageurl,
                    picurl = link.picurl
                }).toarray()
            }
        };
        await sendmessageasync(message);
    }

    // feedcard 链接定义类
    public class feedcardlink
    {
        //链接标题(小于等于64字符)
        public string title { get; set; }
        //跳转链接(需http/https)
        public string messageurl { get; set; }
        //图片链接(可选,建议尺寸:520x275像素)
        public string picurl { get; set; }
    }

    // 按钮定义类
    public class dingtalkbutton
    {
        //按钮文本(小于等于20字符)
        public string title { get; set; }
        //跳转链接(需http/https)
        public string url { get; set; }
    }

    private async task sendmessageasync(object message)
    {
        var json = newtonsoft.json.jsonconvert.serializeobject(message);
        var content = new stringcontent(json, encoding.utf8, "application/json");

        var response = await _httpclient.postasync(_webhookurl, content);
        response.ensuresuccessstatuscode();

        var responsecontent = await response.content.readasstringasync();
        console.writeline($"钉钉机器人响应: {responsecontent}");
    }
}

2)program类型

钉钉机器人发送消息实现类,代码如下:

using static dingtalkrobot;
using static system.net.mime.mediatypenames;

public class program
{
    static async task main(string[] args)
    {
        // 实际webhook url
        var webhookurl = "https://oapi.dingtalk.com/robot/send?access_token=ea8b2fe200c23a4ec8a9569c88ca9561d4f7ebe9bb10d1146ed07e49904c0627\r\n";
        var dingtalkrobot = new dingtalkrobot(webhookurl);

        try
        {
            //关键字 = “报警:” 需在title、content参数加关键字
            //1、发送文本消息(消息长度不超过20kb)
            await dingtalkrobot.sendtextmessageasync("报警:这是一条测试消息",
                atmobiles: new[] { "13812345678" },
                isatall: false);

            //2、发送markdown消息(1、不支持html和复杂表格 2、总长度不超过8192字节)
            await dingtalkrobot.sendmarkdownmessageasync(
                "报警:项目更新通知",
                "### 项目状态更新\n" +
                "> 项目进度: 80%\n\n" +
                "> 剩余时间: 2天\n\n" +
                "![screenshot](https://example.com/image.png)\n" +
                "###### [查看详情](https://example.com)",
                atmobiles: new[] { "13812345678" });

            //3、发送链接消息(最后两个 跳转链接 图片链接)
            await dingtalkrobot.sendlinkmessageasync(
                "报警:系统报警通知",
                "生产环境出现异常,请及时处理",
                "https://www.bilibili.com/video/bv1zu4y1q7sn/?share_source=copy_web&vd_source=ad0206b6bbe094bb6fa542017f2e474b",
                "https://www.bilibili.com/opus/366829175324803541?from=search&spm_id_from=333.337.0.0");

            //4、发送单按钮 actioncard消息
            await dingtalkrobot.sendactioncardasync(
                title: "报警:任务审批",
                text: "**申请人**: 张三\n**内容**: 采购申请\n请及时处理!",
                singletitle: "点击审批",
                singleurl: "https://example.com/approve?id=123");

            //5、发送多按钮 actioncard消息
            var buttons = new list<dingtalkbutton>
            {
                new dingtalkbutton { title = "同意", url = "https://example.com/agree" },
                new dingtalkbutton { title = "拒绝", url = "https://example.com/reject" }
            };

            await dingtalkrobot.sendmultiactioncardasync(
                title: "报警:请假审批",
                text: "**员工**: 李四\n**天数**: 3天",
                buttons: buttons,
                btnorientation: "1" // 水平排列按钮
            );

            //6、发送feedcard消息
            var links = new list<feedcardlink>
            {
                new feedcardlink
                {
                    title = "报警:系统报警",
                    messageurl = "https://example.com/alert1",
                    picurl = "https://example.com/alert.png"
                },
                new feedcardlink
                {
                    title = "报警:新任务",
                    messageurl = "https://example.com/task1",
                    picurl = "https://example.com/task.png"
                }
            };

            await dingtalkrobot.sendfeedcardasync(links);
        }
        catch (exception ex)
        {
            console.writeline($"发送钉钉消息失败: {ex.message}");
        }
    }
}

3)运行结果

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com