钉钉机器人webhook推送
qq_43513957 2024-07-21 17:33:01 阅读 61
钉钉机器人webhook推送
maven依赖
<code><dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
一、机器人webhook推送支持的消息类型
消息类型
二、文本text类型
消息结构
<code>{
"at": {
"atMobiles": [
"180xxxxxx"
],
"atUserIds": [
"user123"
],
"isAtAll": false
},
"text": {
"content": "我就是我, @180xxxxxx 是不一样的烟火"
},
"msgtype": "text"
}
1. 不@人
/**
* 机器人Webhook发送消息
* 文本类型
* 不at
*
* @throws ApiException
*/
public static void textNotAtWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("333测试文本消息");
request.setText(text);
//设置消息的 提醒人:无
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.getBody());
}
效果
2. @一个或几个
<code>/**
* 机器人Webhook发送消息
* 文本类型
* at一个人,几个人
*
* @throws ApiException
*/
public static void textAtOneWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("333测试@153xxxx940x文本消息");
request.setText(text);
//设置消息的 提醒人:一个人、几个人、必须在文本内容加入 @153xxxx940x
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setIsAtAll(false);
at.setAtMobiles(Arrays.asList("153xxxx940x"));
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.getBody());
}
效果
3. @ 所有人
<code> /**
* 机器人Webhook发送消息
* 文本类型
* at所有人
*
* @throws ApiException
*/
public static void textAtAllWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("333测试文本消息");
request.setText(text);
//设置消息的 提醒人:所有人
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setIsAtAll(true);
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.getBody());
}
效果
二、 文本markdown类型
支持的markdown语法
<code>标题
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题
引用
> 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
*/
public static void markdownAtWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.MARKDOWN.getCode());
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.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());
}
效果
三、链接类型
<code> /**
* 机器人Webhook发送消息
* 链接类型
*
* @throws ApiException
*/
public static void linkWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.LINK.getCode());
OapiRobotSendRequest.Link link=new OapiRobotSendRequest.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.整体跳转
<code>/**
* 机器人Webhook发送消息
* 跳转类型 --整体跳转
*
* @throws ApiException
*/
public static void actionCard_All_Webhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.ACTION_CARD.getCode());
OapiRobotSendRequest.Actioncard actioncard=new OapiRobotSendRequest.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.独立跳转
<code> /**
* 机器人Webhook发送消息
* 跳转类型 --独立跳转
*
* @throws ApiException
*/
public static void actionCard_Part_Webhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.ACTION_CARD.getCode());
OapiRobotSendRequest.Actioncard actioncard=new OapiRobotSendRequest.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=new OapiRobotSendRequest.Btns();
b1.setTitle("独立跳转1");
b1.setActionURL("https://www.baidu.com");
OapiRobotSendRequest.Btns b2=new OapiRobotSendRequest.Btns();
b2.setTitle("独立跳转2");
b2.setActionURL("https://www.dingtalk.com");
OapiRobotSendRequest.Btns b3=new OapiRobotSendRequest.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类型
效果:点击不同的区域,跳转不同的链接
<code> /**
* 机器人Webhook发送消息
* 跳转类型 --FeedCard类型(也是单独跳转)
*
* @throws ApiException
*/
public static void feedCard_Webhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.FEED_CARD.getCode());
OapiRobotSendRequest.Feedcard feedcard=new OapiRobotSendRequest.Feedcard();
OapiRobotSendRequest.Links links1 =new OapiRobotSendRequest.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 =new OapiRobotSendRequest.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());
}
六、完整的测试代码
<code>package com.example.demo.dingding.common;
public enum RobotMessageTypeEnum {
TEXT("text", "Text类型"),
MARKDOWN("markdown", "Markdown类型"),
ACTION_CARD("actionCard", "跳转ActionCard类型"),
FEED_CARD("feedCard", "FeedCard类型"),
LINK("link", "Link链接消息");
private final String code;
private final String desc;
RobotMessageTypeEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public String getCode() {
return code;
}
public String getDesc() {
return desc;
}
}
package com.example.demo.dingding.send.api;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.example.demo.dingding.common.RobotMessageTypeEnum;
import com.taobao.api.ApiException;
import java.util.Arrays;
public class ApiSend {
//机器人的 webhook的地址
public static final String url="https://oapi.dingtalk.com/robot/send?access_token=0a479815aa31d1e5a08abcfc505716499f46d1fbec805b852a4e8bd0d4e7332c";code>
public static void main(String[] args) throws ApiException {
// textAtAllWebhook();
// textAtOneWebhook();
// textNotAtWebhook();
markdownAtWebhook();
// linkWebhook();
// actionCard_All_Webhook();
// actionCard_Part_Webhook();
// feedCard_Webhook();
}
//----------------------------------文本类型--------------------------------------
/**
* 机器人Webhook发送消息
* 文本类型
* at所有人
*
* @throws ApiException
*/
public static void textAtAllWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("333测试文本消息");
request.setText(text);
//设置消息的 提醒人:所有人
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setIsAtAll(true);
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.getBody());
}
/**
* 机器人Webhook发送消息
* 文本类型
* at一个人,几个人
*
* @throws ApiException
*/
public static void textAtOneWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("333测试@15342279文本消息");
request.setText(text);
//设置消息的 提醒人:一个人、几个人、必须在文本内容加入 @153422
OapiRobotSendRequest.At at = new OapiRobotSendRequest.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
*/
public static void textNotAtWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.TEXT.getCode());
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("333测试文本消息");
request.setText(text);
//设置消息的 提醒人:无
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.getBody());
}
//-----------------------------------------markdown类型-------------------------------------------
/**
* 机器人Webhook发送消息
* markdown类型
* 不at
*
* @throws ApiException
*/
public static void markdownAtWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.MARKDOWN.getCode());
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.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
*/
public static void linkWebhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.LINK.getCode());
OapiRobotSendRequest.Link link=new OapiRobotSendRequest.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
*/
public static void actionCard_All_Webhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.ACTION_CARD.getCode());
OapiRobotSendRequest.Actioncard actioncard=new OapiRobotSendRequest.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
*/
public static void actionCard_Part_Webhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.ACTION_CARD.getCode());
OapiRobotSendRequest.Actioncard actioncard=new OapiRobotSendRequest.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=new OapiRobotSendRequest.Btns();
b1.setTitle("独立跳转1");
b1.setActionURL("https://www.baidu.com");
OapiRobotSendRequest.Btns b2=new OapiRobotSendRequest.Btns();
b2.setTitle("独立跳转2");
b2.setActionURL("https://www.dingtalk.com");
OapiRobotSendRequest.Btns b3=new OapiRobotSendRequest.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
*/
public static void feedCard_Webhook() throws ApiException {
//创建钉钉客户端
DingTalkClient client = new DefaultDingTalkClient(url);
//创建请求
OapiRobotSendRequest request = new OapiRobotSendRequest();
//设置请求的消息类型、消息内容
request.setMsgtype(RobotMessageTypeEnum.FEED_CARD.getCode());
OapiRobotSendRequest.Feedcard feedcard=new OapiRobotSendRequest.Feedcard();
OapiRobotSendRequest.Links links1 =new OapiRobotSendRequest.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 =new OapiRobotSendRequest.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());
}
}
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。