From 8ed775863acaafdcf084a50161171beab239e777 Mon Sep 17 00:00:00 2001 From: gjh <1421wake> Date: Tue, 26 Aug 2025 10:06:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5DD=E6=9C=BA=E5=99=A8=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-modules/guoyan-platform/pom.xml | 25 +++++ .../dromara/platform/domain/DingDingPush.java | 92 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/domain/DingDingPush.java diff --git a/ruoyi-modules/guoyan-platform/pom.xml b/ruoyi-modules/guoyan-platform/pom.xml index 87389a2..f20120a 100644 --- a/ruoyi-modules/guoyan-platform/pom.xml +++ b/ruoyi-modules/guoyan-platform/pom.xml @@ -13,6 +13,31 @@ guoyan-platform + + + com.aliyun + alibaba-dingtalk-service-sdk + 1.0.1 + + + log4j + log4j + + + + + + com.alibaba + fastjson + 1.2.83 + + + + com.google.guava + guava + 32.1.3-jre + + org.dromara ruoyi-common-core diff --git a/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/domain/DingDingPush.java b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/domain/DingDingPush.java new file mode 100644 index 0000000..f3ed83f --- /dev/null +++ b/ruoyi-modules/guoyan-platform/src/main/java/org/dromara/platform/domain/DingDingPush.java @@ -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 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 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 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; + } +}