1、钉钉群创建机器人
添加完成后,要记住 Webhook 路径;
2、机器人接入文档网址
自定义机器人接入 - 钉钉开放平台
3、JAVA代码
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DingDingSendMsgUtils {
/**
* 超时时间
*/
private static final int timeout = 10000;
/**
* 每个群开通的自定义机器人有webhook,后期可更换或写在配置文件作为参数传入
*/
private static final String webhook = "";
/**
* 自定义关键词,安全设置,后期可更换或写在配置文件作为参数传入
*/
private static final String CUSTOM_KEYWORDS = "退款失败:";
/**
* 提示@所有人
* 文本
* @param msg 通知消息
*/
public static void sendMessageTextAtAll(String msg) {
try{
DingTalkClient client = new DefaultDingTalkClient(webhook);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent( CUSTOM_KEYWORDS + msg);
request.setText(text);
OapiRobotSendResponse response = client.execute(request);
log.info("response="+response);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 提示@所有人
* 图文
* @param msg 通知消息
* @param chargingNumber 订单编号
* @param userName 用户名称
* @param payMoney 支付金额
* @param refundMoney 退款金额
*/
public static void sendMessageMarkdownAtAll(String msg,String chargingNumber,String userName,String payMoney,String refundMoney) {
try{
DingTalkClient client = new DefaultDingTalkClient(webhook);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("markdown");
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle(CUSTOM_KEYWORDS);
markdown.setText("> ### "+msg+"\n" +
"> ###### 订单号:"+chargingNumber+" \n" +
"> ###### 用户名称:"+userName+" \n" +
"> ###### 支付金额:"+payMoney+"元 \n" +
"> ###### 退款金额:"+refundMoney+"元 \n ");
// "> ![screenshot](https://easycharg-station.oss-cn-shanghai.aliyuncs.com/2023/2023/07/05/24df5a45ed374a04a81d6035a6408f02.png)\n" +
// "> ###### \n");
request.setMarkdown(markdown);
OapiRobotSendResponse response = client.execute(request);
log.info("response="+response);
}catch (Exception e){
e.printStackTrace();
}
}
}
结果