当前位置:网站首页>Dingtalk 发送消息
Dingtalk 发送消息
2022-07-02 10:14:00 【阳光下的小鱼】
依赖包:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>代码实现:
package com.sq.proxy.job;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.Arrays;
public class DingTalk3 {
static String webHook = "https://oapi.dingtalk.com/robot/send?access_token=xxx";
public static void main(String[] args_) throws Exception {
Long timestamp = System.currentTimeMillis();
String secret = "SEC6cd99f5d8e817df66c5759c246897b51c6be82a4df8f2ce6b8b39a7adfa3bfff";
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
DingTalkClient client = new DefaultDingTalkClient(webHook+"&sign="+sign+"×tamp="+timestamp);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("测试文本消息");
request.setText(text);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("150xx41","137yyy64"));
// isAtAll类型如果不为Boolean,请升级至最新SDK
at.setIsAtAll(true);
// at.setAtUserIds(Arrays.asList("109929","32099"));
request.setAt(at);
request.setMsgtype("link");
OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
link.setMessageUrl("https://www.dingtalk.com/");
link.setPicUrl("");
link.setTitle("时代的火车向前开");
link.setText("这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林");
request.setLink(link);
// request.setMsgtype("markdown");
// OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
// markdown.setTitle("杭州天气");
// markdown.setText("#### 杭州天气 @156xxxx8827\n" +
// "> 9度,西北风1级,空气良89,相对温度73%\n\n" +
// "> \n" +
// "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
// request.setMarkdown(markdown);
OapiRobotSendResponse response = client.execute(request);
System.out.println(JSON.toJSON(response));
}
}
常见问题:
1:如果要@某些人怎么实现?
1.1 将要@的人的手机号放到setAtMobiles方法里
1.2 在text消息里@手机号 来实现。
参考:
边栏推荐
- OpenAPI generator: simplify the restful API development process
- Unity SKFramework框架(十四)、Extension 扩展函数
- [unity] using GB2312, the solution to abnormal program after packaging
- Memory management 01 - link script
- Fundamentals of machine learning (II) -- division of training set and test set
- I did it with two lines of code. As a result, my sister had a more ingenious way
- De4000h storage installation configuration
- Web Foundation
- Lucky numbers in the [leetcode daily question] matrix
- 文件的下载与图片的预览
猜你喜欢
![[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology](/img/a7/44609a5acf25021f1fca566c3d8c90.png)
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology

Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)

I did it with two lines of code. As a result, my sister had a more ingenious way

Unity skframework framework (XII), score scoring module

挥发性有机物TVOC、VOC、VOCS气体检测+解决方案

MAC (MacOS Monterey 12.2 M1) personal use PHP development

Redis database persistence

Performance optimization of memory function

What are eNB, EPC and PGW?
![[cloud native database] what to do when encountering slow SQL (Part 1)?](/img/ac/a59189aba1901e769beec1ec7e6d32.png)
[cloud native database] what to do when encountering slow SQL (Part 1)?
随机推荐
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
Can automatically update the universal weekly report template, you can use it with your hand!
中文姓名提取(玩具代码——准头太小,权当玩闹)
Lucky numbers in the [leetcode daily question] matrix
Achievements in science and Technology (27)
Don't spend money, spend an hour to build your own blog website
Bridge of undirected graph
研究表明“气味相投”更易成为朋友
Finally, someone explained the supervised learning clearly
Explanation: here is your UFO, Goldbach conjecture
能自动更新的万能周报模板,有手就会用!
Let juicefs help you with "remote backup"
Unity skframework framework (XXI), texture filter map resource filtering tool
三谈exception——错误处理
题解《子数整数》、《欢乐地跳》、《开灯》
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
I did it with two lines of code. As a result, my sister had a more ingenious way
不会看器件手册的工程师不是个好厨子
Redis数据库持久化
PR usage skills, how to use PR to watermark?