2 changed files with 117 additions and 0 deletions
@ -0,0 +1,92 @@ |
|||
package org.dromara.platform.domain; |
|||
|
|||
import cn.hutool.http.HttpUtil; |
|||
import com.alibaba.fastjson.JSON; |
|||
|
|||
import com.google.common.collect.Lists; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import com.google.common.collect.Maps; |
|||
|
|||
/** |
|||
* 钉钉推送消息demo |
|||
* GJH |
|||
*/ |
|||
@Slf4j |
|||
public class DingDingPush { |
|||
public static void main(String[] args) { |
|||
//把webhook设置成对应群的即可
|
|||
String webhook ="https://oapi.dingtalk.com/robot/send?access_token=361ec6fe80c5b49334f5eab9fc4e92ffcdabd23b25b01269488ab9971ade1963"; |
|||
String content = getContent(); |
|||
ArrayList<String> mobileList = Lists.newArrayList(); |
|||
sendMsgToGroupChat(webhook,false,mobileList,content); |
|||
} |
|||
/** |
|||
* 通知消息发送到群聊 |
|||
* @param webhook 钉钉机器人地址(配置机器人的webhook) |
|||
* @param isAtAll 是否通知所有人 |
|||
* @param mobileList 通知具体人的手机号码列表 |
|||
* @param content 消息内容 |
|||
*/ |
|||
public static void sendMsgToGroupChat(String webhook, boolean isAtAll, List<String> mobileList, String content){ |
|||
try { |
|||
//组装请求内容
|
|||
String reqStr = buildReqStr(content, isAtAll, mobileList); |
|||
//推送消息(http请求)
|
|||
String result = HttpUtil.post(webhook, reqStr); |
|||
log.info("通知响应结果:{}",result); |
|||
}catch (Exception e){ |
|||
log.error("webhook通知失败",e); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 组装请求报文(Map封装) |
|||
* @param content 通知内容 |
|||
* @param isAtAll 是否@所有人 |
|||
* @param mobileList 通知具体人的手机号码 |
|||
* @return |
|||
*/ |
|||
private static String buildReqStr(String content, boolean isAtAll, List mobileList) { |
|||
mobileList.add("18797992307"); |
|||
mobileList.add("13120821702"); |
|||
|
|||
//消息内容
|
|||
Map<String, String> contentMap = Maps.newHashMap(); |
|||
contentMap.put("content", content); |
|||
//通知人
|
|||
Map atMap = Maps.newHashMap(); |
|||
//1.是否通知所有人
|
|||
atMap.put("isAtAll", isAtAll); |
|||
//2.通知具体人的手机号码列表
|
|||
atMap.put("atMobiles", mobileList); |
|||
|
|||
Map reqMap = Maps.newHashMap(); |
|||
reqMap.put("msgtype", "text"); |
|||
reqMap.put("text", contentMap); |
|||
reqMap.put("at", atMap); |
|||
|
|||
return JSON.toJSONString(reqMap); |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 获取通知消息 |
|||
* @return |
|||
*/ |
|||
private static String getContent() { |
|||
//钉钉机器人消息内容
|
|||
String content; |
|||
//通过转码网站http://tool.chinaz.com/Tools/unicode.aspx
|
|||
// 选择中文转Unicode把钉钉表情转换成unicode编码,也可以直接用表情对应的中文设置
|
|||
String NEWLINE = "\n"; |
|||
StringBuffer sb = new StringBuffer(); |
|||
sb.append("测试推送消息,你好有新的工单请及时处理!") |
|||
.append(NEWLINE); |
|||
content = sb.toString(); |
|||
return content; |
|||
} |
|||
} |
Loading…
Reference in new issue