OkHttpUtils.postString() .mediaType(MediaType.parse("application/json; charset=utf-8")) .url(webhook).content("{\"text\":{\"content\":\"关键词: 我就是我,333是不一样444的烟火\"},\"msgtype\":\"text\"}") .build().execute(new Stri
.url(webhook).content("{\"text\":{\"content\":\"关键词: 我就是我,333是不一样444的烟火\"},\"msgtype\":\"text\"}")
.build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int i) {
e.printStackTrace();
}
@Override
public void onResponse(String s, int i) {
System.out.println("s = " + s);
}
});
参考
Java实现钉钉机器人推送消息 - CodeAntenna
<dependencies><dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.0.12</version>
</dependency>
</dependencies>import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import java.util.List;
import java.util.Map;
/**
* @author AlanFin
* @date 2021/5/10
*/
public class DingDingSendMsgUtils {
/**
* 超时时间
*/
private static final int timeout = 10000;
/**
* 每个群开通的自定义机器人有webhook,后期可更换或写在配置文件作为参数传入
*/
private static final String webhook = "刚刚获得Webhook地址";
/**
* 自定义关键词,安全设置,后期可更换或写在配置文件作为参数传入
*/
private static final String CUSTOM_KEYWORDS = "刚刚写的自定义关键字:";
/**
* 提示@所有人
*
* @param msg 通知消息
*/
private static void sendMessageAtAll(String msg) {
Map<String, Object> atMap = MapUtil.newHashMap();
atMap.put("isAtAll", true);
String reqString = getString(msg, atMap);
HttpRequest.post(webhook).body(reqString).timeout(timeout).execute().body();
}
/**
* 提示@具体选择的某个人
*
* @param msg 通知消息
* @param mobileList 通知人的电话号码
*/
private static void sendMessageAtChosePerson(String msg, List<String> mobileList) {
Map<String, Object> atMap = MapUtil.newHashMap();
atMap.put("isAtAll", false);
atMap.put("atMobiles", mobileList);
String reqString = getString(msg, atMap);
HttpRequest.post(webhook).body(reqString).timeout(timeout).execute().body();
}
private static String getString(String msg, Map<String, Object> atMap) {
String content = CUSTOM_KEYWORDS + msg;
Map<String, String> contentMap = MapUtil.newHashMap();
contentMap.put("content", content);
Map<String, Object> reqMap = MapUtil.newHashMap();
reqMap.put("msgtype", "text");
reqMap.put("text", contentMap);
reqMap.put("at", atMap);
String reqString = JSON.toJSONString(reqMap);
return reqString;
}
}