0


钉钉机器人webhook推送

钉钉机器人webhook推送

maven依赖

<dependency><groupId>com.aliyun</groupId><artifactId>alibaba-dingtalk-service-sdk</artifactId><version>2.0.0</version></dependency>

一、机器人webhook推送支持的消息类型

消息类型
在这里插入图片描述

二、文本text类型

消息结构

{"at":{"atMobiles":["180xxxxxx"],"atUserIds":["user123"],"isAtAll":false},"text":{"content":"我就是我, @180xxxxxx 是不一样的烟火"},"msgtype":"text"}
1. 不@人
/**
     * 机器人Webhook发送消息
     * 文本类型
     * 不at
     *
     * @throws ApiException
     */publicstaticvoidtextNotAtWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());OapiRobotSendRequest.Text text =newOapiRobotSendRequest.Text();
        text.setContent("333测试文本消息");
        request.setText(text);//设置消息的 提醒人:无OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}

效果
在这里插入图片描述

2. @一个或几个
/**
     * 机器人Webhook发送消息
     * 文本类型
     * at一个人,几个人
     *
     * @throws ApiException
     */publicstaticvoidtextAtOneWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());OapiRobotSendRequest.Text text =newOapiRobotSendRequest.Text();
        text.setContent("333测试@153xxxx940x文本消息");
        request.setText(text);//设置消息的 提醒人:一个人、几个人、必须在文本内容加入 @153xxxx940xOapiRobotSendRequest.At at =newOapiRobotSendRequest.At();
        at.setIsAtAll(false);
        at.setAtMobiles(Arrays.asList("153xxxx940x"));
        request.setAt(at);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}

效果
在这里插入图片描述

3. @ 所有人
/**
     * 机器人Webhook发送消息
     * 文本类型
     * at所有人
     *
     * @throws ApiException
     */publicstaticvoidtextAtAllWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());OapiRobotSendRequest.Text text =newOapiRobotSendRequest.Text();
        text.setContent("333测试文本消息");
        request.setText(text);//设置消息的 提醒人:所有人OapiRobotSendRequest.At at =newOapiRobotSendRequest.At();
        at.setIsAtAll(true);
        request.setAt(at);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}

效果
在这里插入图片描述

二、 文本markdown类型

支持的markdown语法

标题
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题
 
引用
>A man who stands for nothing will fall for anything.
 
文字加粗、斜体
**bold***italic*
 
链接
[this is a link](https://www.dingtalk.com/)
 
图片
![](http://name.com/pic.jpg)
 
无序列表
- item1
- item2
 
有序列表
1. item1
2. item2

代码

/**
     * 机器人Webhook发送消息
     * markdown类型
     * 不at
     *
     * @throws ApiException
     */publicstaticvoidmarkdownAtWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.MARKDOWN.getCode());OapiRobotSendRequest.Markdown markdown =newOapiRobotSendRequest.Markdown();

        markdown.setTitle("我的markdown类型");
        markdown.setText("#### 杭州天气 @150XXXXXXXX \n> 9度,西北风333级,空气良89,相对温度73%\n"+"> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n"+"> ###### 10点20分发布 [天气](https://www.dingtalk.com) \n");

        request.setMarkdown(markdown);//设置消息的 提醒人:无OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}

效果
在这里插入图片描述

三、链接类型

/**
     * 机器人Webhook发送消息
     * 链接类型
     *
     * @throws ApiException
     */publicstaticvoidlinkWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.LINK.getCode());OapiRobotSendRequest.Link link=newOapiRobotSendRequest.Link();
        link.setTitle("我的title");
        link.setText("333消息内容");
        link.setMessageUrl("https://www.baidu.com");//        link.setPicUrl("picture.url");
        request.setLink(link);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}

效果:点击链接跳转到百度
在这里插入图片描述
在这里插入图片描述

四、ActionCard类型

1.整体跳转
/**
     * 机器人Webhook发送消息
     * 跳转类型 --整体跳转
     *
     * @throws ApiException
     */publicstaticvoidactionCard_All_Webhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.ACTION_CARD.getCode());OapiRobotSendRequest.Actioncard actioncard=newOapiRobotSendRequest.Actioncard();
        actioncard.setTitle("我的title:打造一间咖啡厅");
        actioncard.setText("![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n #### 333乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
       actioncard.setSingleTitle("阅读全文");
       actioncard.setSingleURL("https://www.baidu.com");
       request.setActionCard(actioncard);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}

效果:点击任何一个文字,都会跳转到 百度
在这里插入图片描述

2.独立跳转
/**
     * 机器人Webhook发送消息
     * 跳转类型 --独立跳转
     *
     * @throws ApiException
     */publicstaticvoidactionCard_Part_Webhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.ACTION_CARD.getCode());OapiRobotSendRequest.Actioncard actioncard=newOapiRobotSendRequest.Actioncard();
        actioncard.setTitle("我的title:乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身");
        actioncard.setText("![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n #### 333乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");OapiRobotSendRequest.Btns b1=newOapiRobotSendRequest.Btns();
        b1.setTitle("独立跳转1");
        b1.setActionURL("https://www.baidu.com");OapiRobotSendRequest.Btns b2=newOapiRobotSendRequest.Btns();
        b2.setTitle("独立跳转2");
        b2.setActionURL("https://www.dingtalk.com");OapiRobotSendRequest.Btns b3=newOapiRobotSendRequest.Btns();
        b3.setTitle("独立跳转3");
        b3.setActionURL("https://www.emqx.io/docs/zh/latest/messaging/mqtt-wildcard-subscription.html");
        actioncard.setBtns(Arrays.asList(b1,b2,b3));

        request.setActionCard(actioncard);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}

效果:点击跳转链接,可以跳到百度,钉钉,emqx
在这里插入图片描述
五、FeedCard类型

效果:点击不同的区域,跳转不同的链接

/**
     * 机器人Webhook发送消息
     * 跳转类型 --FeedCard类型(也是单独跳转)
     *
     * @throws ApiException
     */publicstaticvoidfeedCard_Webhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.FEED_CARD.getCode());OapiRobotSendRequest.Feedcard feedcard=newOapiRobotSendRequest.Feedcard();OapiRobotSendRequest.Links links1 =newOapiRobotSendRequest.Links();
        links1.setTitle("333--l1");
        links1.setMessageURL("https://www.baidu.com");
        links1.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");OapiRobotSendRequest.Links links2 =newOapiRobotSendRequest.Links();
        links2.setTitle("333--l2");
        links2.setMessageURL("https://www.emqx.io/docs/zh/latest/messaging/mqtt-wildcard-subscription.html");
        links2.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");

        feedcard.setLinks(Arrays.asList(links1,links2));
        request.setFeedCard(feedcard);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}

在这里插入图片描述

六、完整的测试代码

packagecom.example.demo.dingding.common;publicenumRobotMessageTypeEnum{TEXT("text","Text类型"),MARKDOWN("markdown","Markdown类型"),ACTION_CARD("actionCard","跳转ActionCard类型"),FEED_CARD("feedCard","FeedCard类型"),LINK("link","Link链接消息");privatefinalString code;privatefinalString desc;RobotMessageTypeEnum(String code,String desc){this.code = code;this.desc = desc;}publicStringgetCode(){return code;}publicStringgetDesc(){return desc;}}
packagecom.example.demo.dingding.send.api;importcom.dingtalk.api.DefaultDingTalkClient;importcom.dingtalk.api.DingTalkClient;importcom.dingtalk.api.request.OapiRobotSendRequest;importcom.dingtalk.api.response.OapiRobotSendResponse;importcom.example.demo.dingding.common.RobotMessageTypeEnum;importcom.taobao.api.ApiException;importjava.util.Arrays;publicclassApiSend{//机器人的 webhook的地址publicstaticfinalString url="https://oapi.dingtalk.com/robot/send?access_token=0a479815aa31d1e5a08abcfc505716499f46d1fbec805b852a4e8bd0d4e7332c";publicstaticvoidmain(String[] args)throwsApiException{//        textAtAllWebhook();//        textAtOneWebhook();//        textNotAtWebhook();markdownAtWebhook();//        linkWebhook();//        actionCard_All_Webhook();//        actionCard_Part_Webhook();//        feedCard_Webhook();}//----------------------------------文本类型--------------------------------------/**
     * 机器人Webhook发送消息
     * 文本类型
     * at所有人
     *
     * @throws ApiException
     */publicstaticvoidtextAtAllWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());OapiRobotSendRequest.Text text =newOapiRobotSendRequest.Text();
        text.setContent("333测试文本消息");
        request.setText(text);//设置消息的 提醒人:所有人OapiRobotSendRequest.At at =newOapiRobotSendRequest.At();
        at.setIsAtAll(true);
        request.setAt(at);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}/**
     * 机器人Webhook发送消息
     * 文本类型
     * at一个人,几个人
     *
     * @throws ApiException
     */publicstaticvoidtextAtOneWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());OapiRobotSendRequest.Text text =newOapiRobotSendRequest.Text();
        text.setContent("333测试@15342279文本消息");
        request.setText(text);//设置消息的 提醒人:一个人、几个人、必须在文本内容加入 @153422OapiRobotSendRequest.At at =newOapiRobotSendRequest.At();
        at.setIsAtAll(false);
        at.setAtMobiles(Arrays.asList("153422794"));
        request.setAt(at);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}/**
     * 机器人Webhook发送消息
     * 文本类型
     * 不at
     *
     * @throws ApiException
     */publicstaticvoidtextNotAtWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());OapiRobotSendRequest.Text text =newOapiRobotSendRequest.Text();
        text.setContent("333测试文本消息");
        request.setText(text);//设置消息的 提醒人:无OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}//-----------------------------------------markdown类型-------------------------------------------/**
     * 机器人Webhook发送消息
     * markdown类型
     * 不at
     *
     * @throws ApiException
     */publicstaticvoidmarkdownAtWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.MARKDOWN.getCode());OapiRobotSendRequest.Markdown markdown =newOapiRobotSendRequest.Markdown();

        markdown.setTitle("我的markdown类型");
        markdown.setText("#### 杭州天气 @150XXXXXXXX \n> 9度,西北风333级,空气良89,相对温度73%\n"+"> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n"+"> ###### 10点20分发布 [天气](https://www.dingtalk.com) \n");

        request.setMarkdown(markdown);//设置消息的 提醒人:无OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}/*                       支持的markdown语法
    标题
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题

引用
> A man who stands for nothing will fall for anything.

文字加粗、斜体
**bold**
*italic*

链接
[this is a link](https://www.dingtalk.com/)

图片
![](http://name.com/pic.jpg)

无序列表
- item1
- item2

有序列表
1. item1
2. item2
     *///-----------------------------------------链接---------------------------------------------------/**
     * 机器人Webhook发送消息
     * 链接类型
     *
     * @throws ApiException
     */publicstaticvoidlinkWebhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.LINK.getCode());OapiRobotSendRequest.Link link=newOapiRobotSendRequest.Link();
        link.setTitle("我的title");
        link.setText("333消息内容");
        link.setMessageUrl("https://www.baidu.com");
        link.setPicUrl("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
        request.setLink(link);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}//------------------------------------------跳转--------------------------------------------------/**
     * 机器人Webhook发送消息
     * 跳转类型 --整体跳转
     *
     * @throws ApiException
     */publicstaticvoidactionCard_All_Webhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.ACTION_CARD.getCode());OapiRobotSendRequest.Actioncard actioncard=newOapiRobotSendRequest.Actioncard();
        actioncard.setTitle("我的title:打造一间咖啡厅");
        actioncard.setText("![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n #### 333乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
       actioncard.setSingleTitle("阅读全文");
       actioncard.setSingleURL("https://www.baidu.com");
       request.setActionCard(actioncard);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}/**
     * 机器人Webhook发送消息
     * 跳转类型 --独立跳转
     *
     * @throws ApiException
     */publicstaticvoidactionCard_Part_Webhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.ACTION_CARD.getCode());OapiRobotSendRequest.Actioncard actioncard=newOapiRobotSendRequest.Actioncard();
        actioncard.setTitle("我的title:乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身");
        actioncard.setText("![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n #### 333乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");OapiRobotSendRequest.Btns b1=newOapiRobotSendRequest.Btns();
        b1.setTitle("独立跳转1");
        b1.setActionURL("https://www.baidu.com");OapiRobotSendRequest.Btns b2=newOapiRobotSendRequest.Btns();
        b2.setTitle("独立跳转2");
        b2.setActionURL("https://www.dingtalk.com");OapiRobotSendRequest.Btns b3=newOapiRobotSendRequest.Btns();
        b3.setTitle("独立跳转3");
        b3.setActionURL("https://www.emqx.io/docs/zh/latest/messaging/mqtt-wildcard-subscription.html");
        actioncard.setBtns(Arrays.asList(b1,b2,b3));

        request.setActionCard(actioncard);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}//----------------------------------FeedCard类型----------------------------------------------------------/**
     * 机器人Webhook发送消息
     * 跳转类型 --FeedCard类型(也是单独跳转)
     *
     * @throws ApiException
     */publicstaticvoidfeedCard_Webhook()throwsApiException{//创建钉钉客户端DingTalkClient client =newDefaultDingTalkClient(url);//创建请求OapiRobotSendRequest request =newOapiRobotSendRequest();//设置请求的消息类型、消息内容
        request.setMsgtype(RobotMessageTypeEnum.FEED_CARD.getCode());OapiRobotSendRequest.Feedcard feedcard=newOapiRobotSendRequest.Feedcard();OapiRobotSendRequest.Links links1 =newOapiRobotSendRequest.Links();
        links1.setTitle("333--l1");
        links1.setMessageURL("https://www.baidu.com");
        links1.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");OapiRobotSendRequest.Links links2 =newOapiRobotSendRequest.Links();
        links2.setTitle("333--l2");
        links2.setMessageURL("https://www.emqx.io/docs/zh/latest/messaging/mqtt-wildcard-subscription.html");
        links2.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");

        feedcard.setLinks(Arrays.asList(links1,links2));
        request.setFeedCard(feedcard);OapiRobotSendResponse response = client.execute(request);System.out.println(response.getBody());}}
标签: 钉钉 机器人 java

本文转载自: https://blog.csdn.net/qq_43513957/article/details/135753233
版权归原作者 qq_43513957 所有, 如有侵权,请联系我们删除。

“钉钉机器人webhook推送”的评论:

还没有评论