原 项目接入钉钉自定义机器人
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1338
这个步骤大家自行进行创建。
2.2 点击添加机器人
2.3 点击添加按钮
2.4 我这里选择Webhook(大家根据自己的需求进行选择)。
2.5 点击添加
2.6 安全设置我们这里选择最简单的方式。自定义关键词。
2.7 点击完成
2.8 完成后的界面
这里的地址就是2.7 步骤中生成的地址。
https://oapi.dingtalk.com/robot/send?access_token=xxxx
消息内容:
{
"msgtype": "text",
"text": {
"content": "91名师指路,开始我的第一条消息。"
}
}
注意:我们这里选择的是自定义关键词:91名师指路,所以请求的时候需要带上“91名师指路”这个关键词才能发送成功。
3.2 钉钉群里发送消息后的效果截图
四:java代码实现钉钉机器人消息
当前自定义机器人有如下消息类型:文本 (text)、链接 (link)、markdown(markdown)、ActionCard、FeedCard
4.1 引入taobao-sdk-java-auto_1479188381469-20210926.jar
jar包下载地址:https://developers.dingtalk.com/document/app/download-the-server-side-sdk/title-12y-g4g-zn2
或者引入jar
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>1.0.1</version>
</dependency>
4.2 文本消息(text)效果图
4.3 链接消息(link)效果图
4.4 markdown消息(markdown)
完整代码如下:
package com.mszl.blog.controller;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
/**
* @author zxb
* https://91mszl.com
*/
@RequestMapping("/dingtalk")
@RestController
public class DingDingMsg {
/**
* 功能:文本消息
*/
@PostMapping("/textmsg")
public void sendDingTalk() throws ApiException {
// 1 定义OapiRobotSendRequest对象
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=xxx");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
// 2 定义消息类型和内容
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("91名师指路,开始我的第一条消息。"); // 消息内容。
request.setText(text); // 消息类型,此时固定为:text
// 3 定义是否@所有人
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
// at.setAtMobiles(Arrays.asList("132xxx")); // 被@人的手机号
// at.setAtUserIds(Arrays.asList("109929","32099")); // 被@人的用户userid
at.setIsAtAll(true); // 是否@所有人
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
}
/**
* 功能:link
*/
@PostMapping("/link")
public void link() throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=xxx");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("link");
OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
link.setMessageUrl("https://91mszl.com/"); // 链接
link.setPicUrl("");
link.setTitle("91名师指路,让能力变成价值"); // 标题
link.setText("91名师指路,愿你的每一行代码都能改变世界"); // 内容
request.setLink(link);
OapiRobotSendResponse response = client.execute(request);
}
/**
* 功能:markdown
*/
@PostMapping("/markdown")
public void markdown() throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=xxx");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("markdown");
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle("上海天气");
markdown.setText("#### 91名师指路,上海天气\n" +
"> 28度,西北风1级,空气良89,相对温度73%\n\n" +
"> \n" +
"> ###### 10点20分发布 [天气](https://91mszl.com/) \n");
request.setMarkdown(markdown);
OapiRobotSendResponse response = client.execute(request);
}
}
常见问题:
1)消息内容中不包含任何关键词
{
"errcode":310000,
"errmsg":"keywords not in content"
}
2)timestamp 无效
{
"errcode":310000,
"errmsg":"invalid timestamp"
}
3)签名不匹配
{
"errcode":310000,
"errmsg":"sign not match"
}
4)IP地址不在白名单
{
"errcode":310000,
"errmsg":"ip X.X.X.X not in whitelist"
}
参考资料:
https://developers.dingtalk.com/document/robots/custom-robot-access
2021-09-26 13:54:31 阅读(859)
名师出品,必属精品 https://www.91mszl.com