下面是 Spring Boot 实现钉钉机器人消息推送的完整攻略。
1. 准备工作
在开始之前,需要完成以下几个步骤:
- 在钉钉工作台上创建一个自定义机器人。
- 获取钉钉机器人的 Webhook 值。
2. 新建 Spring Boot 项目
使用 Spring Initializr 创建一个新的 Spring Boot 项目。在这个项目中,我们需要引入以下依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<dependency>
<groupId>com.dingtalk</groupId>
<artifactId>taobao-sdk-java</artifactId>
<version>20210420</version>
</dependency>
3. 创建钉钉机器人类和消息类
创建一个名为 DingtalkRobot
的类,用于发送消息和处理异常。代码如下:
import java.util.Collections;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.OapiRobotSendRequest;
import com.taobao.api.response.OapiRobotSendResponse;
public class DingtalkRobot {
private static final String DINGTALK_URL = "https://oapi.dingtalk.com/robot/send";
private final String accessKey;
public DingtalkRobot(String accessKey) {
this.accessKey = accessKey;
}
public boolean sendMessage(String message) {
TaobaoClient client = new DefaultTaobaoClient(DINGTALK_URL, accessKey, "");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
request.setText(new OapiRobotSendRequest.Text());
request.getText().setContent(message);
request.setAt(new OapiRobotSendRequest.At());
request.getAt().setAtMobiles(Collections.singletonList("12345678901"));
request.getAt().setIsAtAll(false);
try {
OapiRobotSendResponse response = client.execute(request);
if ("ok".equals(response.getErrmsg())) {
return true;
}
} catch (ApiException e) {
e.printStackTrace();
}
return false;
}
}
创建一个名为 DingtalkMessage
的类,用于存储需要发送的消息。这个类包含了通用的消息格式。代码如下:
import java.util.HashMap;
import java.util.Map;
public class DingtalkMessage {
public static final String MARKDOWN_TYPE = "markdown";
public static final String TEXT_TYPE = "text";
private String type;
private Map<String, Object> content;
private boolean atAll = false;
private String[] atMobiles = new String[0];
public DingtalkMessage() {
this.type = TEXT_TYPE;
this.content = new HashMap<>();
}
public DingtalkMessage(String type) {
this.type = type;
this.content = new HashMap<>();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Map<String, Object> getContent() {
return content;
}
public void setContent(Map<String, Object> content) {
this.content = content;
}
public boolean isAtAll() {
return atAll;
}
public void setAtAll(boolean atAll) {
this.atAll = atAll;
}
public String[] getAtMobiles() {
return atMobiles;
}
public void setAtMobiles(String[] atMobiles) {
this.atMobiles = atMobiles;
}
public DingtalkMessage addContent(String key, Object value) {
this.content.put(key, value);
return this;
}
public DingtalkMessage addAtMobile(String mobile) {
String[] newAtMobiles = new String[atMobiles.length + 1];
System.arraycopy(atMobiles, 0, newAtMobiles, 0, atMobiles.length);
newAtMobiles[atMobiles.length] = mobile;
this.atMobiles = newAtMobiles;
return this;
}
}
4. 发送消息
要发送一条消息,可以按照以下步骤操作:
- 创建一个
DingtalkRobot
对象,并传入钉钉机器人的 accessKey。 - 创建一个
DingtalkMessage
对象。 - 设置
DingtalkMessage
的内容和 atMobiles 属性。 - 调用
DingtalkRobot
的sendMessage
方法,发送消息。
示例:
public static void main(String[] args) {
// 1. 创建一个 DingtalkRobot 对象并传入钉钉机器人的 accessKey
String accessKey = "钉钉机器人的 accessKey";
DingtalkRobot robot = new DingtalkRobot(accessKey);
// 2. 创建一个 DingtalkMessage 对象
String title = "这是一条测试消息";
String content = "测试内容...";
DingtalkMessage message = new DingtalkMessage();
message.setType(DingtalkMessage.MARKDOWN_TYPE);
message.addContent("title", title)
.addContent("text", content)
.addAtMobile("12345678901");
// 3. 发送消息
robot.sendMessage(message);
}
5. 示例代码
下面给出一个更完整的代码示例:
import java.util.Collections;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.OapiRobotSendRequest;
import com.taobao.api.response.OapiRobotSendResponse;
public class DingtalkRobot {
private static final String DINGTALK_URL = "https://oapi.dingtalk.com/robot/send";
private final String accessKey;
public DingtalkRobot(String accessKey) {
this.accessKey = accessKey;
}
public boolean sendMessage(DingtalkMessage message) {
TaobaoClient client = new DefaultTaobaoClient(DINGTALK_URL, accessKey, "");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(message.getType());
switch (message.getType()) {
case DingtalkMessage.TEXT_TYPE:
request.setText(new OapiRobotSendRequest.Text());
request.getText().setContent((String) message.getContent().get("text"));
break;
case DingtalkMessage.MARKDOWN_TYPE:
request.setMarkdown(new OapiRobotSendRequest.Markdown());
request.getMarkdown().setTitle((String) message.getContent().get("title"));
request.getMarkdown().setText((String) message.getContent().get("text"));
break;
default:
throw new IllegalArgumentException("Unsupported message type");
}
if (message.isAtAll()) {
request.setAt(new OapiRobotSendRequest.At().setIsAtAll(true));
} else {
request.setAt(new OapiRobotSendRequest.At().setAtMobiles(convertToJavaList(message.getAtMobiles())));
}
try {
OapiRobotSendResponse response = client.execute(request);
if ("ok".equals(response.getErrmsg())) {
return true;
}
} catch (ApiException e) {
e.printStackTrace();
}
return false;
}
private <T> java.util.List<T> convertToJavaList(T[] arr) {
if (arr == null) {
return Collections.emptyList();
}
return java.util.Arrays.asList(arr);
}
}
import java.util.HashMap;
import java.util.Map;
public class DingtalkMessage {
public static final String MARKDOWN_TYPE = "markdown";
public static final String TEXT_TYPE = "text";
private String type;
private Map<String, Object> content;
private boolean atAll = false;
private String[] atMobiles = new String[0];
public DingtalkMessage() {
this.type = TEXT_TYPE;
this.content = new HashMap<>();
}
public DingtalkMessage(String type) {
this.type = type;
this.content = new HashMap<>();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Map<String, Object> getContent() {
return content;
}
public void setContent(Map<String, Object> content) {
this.content = content;
}
public boolean isAtAll() {
return atAll;
}
public void setAtAll(boolean atAll) {
this.atAll = atAll;
}
public String[] getAtMobiles() {
return atMobiles;
}
public void setAtMobiles(String[] atMobiles) {
this.atMobiles = atMobiles;
}
public DingtalkMessage addContent(String key, Object value) {
this.content.put(key, value);
return this;
}
public DingtalkMessage addAtMobile(String mobile) {
String[] newAtMobiles = new String[atMobiles.length + 1];
System.arraycopy(atMobiles, 0, newAtMobiles, 0, atMobiles.length);
newAtMobiles[atMobiles.length] = mobile;
this.atMobiles = newAtMobiles;
return this;
}
}
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
String accessKey = "钉钉机器人的 accessKey";
DingtalkRobot robot = new DingtalkRobot(accessKey);
String title = "这是一条测试消息";
String content = "测试内容...";
DingtalkMessage message = new DingtalkMessage();
message.setType(DingtalkMessage.MARKDOWN_TYPE);
message.addContent("title", title)
.addContent("text", content)
.addAtMobile("12345678901");
robot.sendMessage(message);
}
}
上面的代码演示了一个用于发送钉钉机器人消息的示例。如果你需要发送多条消息,只需要创建多个 DingtalkMessage
对象,并循环调用 DingtalkRobot
的 sendMessage
方法即可。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot实现钉钉机器人消息推送的示例代码 - Python技术站