钉钉设置:
1.建立一个钉钉群聊:钉钉右上角发起群聊,选择群成员,创建
2.群智能助手设置
添加机器人:选自定义机器人,设置名称,会有一个webhook地址,这个要保密,不然会有安全问题
进行安全设置,这里选择ip地址段设置,只有允许的ip可以发钉钉消息
配置完成。
项目目录结构:
依赖配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>dingTalk</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.taobao.top</groupId>
<artifactId>taobao-sdk-java-auto</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/lib/taobao-sdk-java-auto-1.0.0.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
public static void main(String[] args) {
sendActionCard();
}
// 独立跳转ActionCard类型
public static void sendActionCard() {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=xxx");
//参数 类型 必选 说明
//msgtype string true 此消息类型为固定actionCard
//title string true 首屏会话透出的展示内容
//text string true markdown格式的消息
//btns array true 按钮的信息:title-按钮方案,actionURL-点击按钮触发的URL
//btnOrientation string false 0-按钮竖直排列,1-按钮横向排列
//hideAvatar string false 0-正常发消息者头像,1-隐藏发消息者头像
OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard();
actionCard.setTitle("杭州天气");
actionCard.setText("#### 杭州天气 @156xxxx8827\n" +
"> 9度,西北风1级,空气良89,相对温度73%\n\n" +
"> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" +
"> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
// 此处默认为0
actionCard.setBtnOrientation("0");
// 此处默认为0
actionCard.setHideAvatar("0");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("actionCard");
request.setActionCard(actionCard);
try {
client.execute(request);
} catch (ApiException e) {
e.printStackTrace();
}
}