1.安装依赖
<dependency>
<groupId>com.taobao.dingding</groupId>
<artifactId>taobao-sdk</artifactId>
<version>1.0.0</version>
</dependency>
2.编写工具类
@Slf4j
public class DingDingUtils {
private static String ROBOT_SEND_MESSAGE_URL = "https://oapi.dingtalk.com/robot/send?access_token=";
/**
* 发送钉钉群消息
* @param content
*/
public static void sendDingDingGroupMsg(String accessToken, String content) {
String textMsg = "{ "msgtype": "text", "text": {"content": "" + content + ""}}";
dealDingDingMsgSend(accessToken, textMsg);
}
/**
* 处理发送的钉钉消息
*
* @param textMsg
*/
private static void dealDingDingMsgSend(String accessToken, String textMsg) {
HttpClient httpclient = HttpClients.createDefault();
String robotSendMessageUrl = ROBOT_SEND_MESSAGE_URL + accessToken;
HttpPost httppost = new HttpPost(robotSendMessageUrl);
httppost.addHeader("Content-Type", "application/json; charset=utf-8");
StringEntity se = new StringEntity(textMsg, "utf-8");
httppost.setEntity(se);
try {
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
log.info("Send DingDing Message Success! Response Content: " + JSON.toJSONString(result));
}
} catch (Exception e) {
log.error("Send DingDing Message Error! Error Message:" + e.getMessage(), e);
}
}
}
3.测试
安全设置
我设置了关键字,消息里面有这个hello关键字才能成功发送消息
Webhook
代码编写
public static void main(String[] args) {
String str = "hello,钉钉";
String token = "这里填写你自己的钉钉token";
DingDingUtils.sendDingDingGroupMsg(token,str);
}
运行测试
4.艾特全部功能
钉钉提供了艾特全部功能
{
"at": {
"atMobiles":[
"180xxxxxx"
],
"atUserIds":[
"user123"
],
"isAtAll": false
},
"text": {
"content":"我就是我, @XXX 是不一样的烟火"
},
"msgtype":"text"
}
修改sendDingDingGroupMsg方法,第三个参数表示是否艾特全部
/**
* 发送钉钉群消息
* @param content
*/
public static void sendDingDingGroupMsg(String accessToken, String content,Boolean atAll) {
String textMsg = "{ "msgtype": "text", "text": {"content": "" + content + ""},"at": {"isAtAll": "" + atAll + ""}}";
dealDingDingMsgSend(accessToken, textMsg);
}
觉得有用点个赞叭
原文地址:https://blog.csdn.net/m0_55627541/article/details/134673956
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_8197.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。