添加群机器人:https://open.work.weixin.qq.com/help2/pc/14931?person_id=1&is_tencent=
1.新建实体类
package com.model;
import com.alibaba.fastjson.JSONObject;
import java.io.Serializable;
/**
* @ClassName QyWetChatSend
* @Description //TODO
* @Author yinjuan
* @Date 2021-12-20 9:52
* @Version 1.0
*/
public class QyWetChatSend implements Serializable {
private String msgtype;
private ContentText text;
private ContentAt at;
public QyWetChatSend(String msg) {
ContentText contentText = new ContentText(msg);
this.msgtype = "text";
this.text = contentText;
this.at = new ContentAt();
}
public QyWetChatSend(String msgtype, ContentText text, ContentAt at) {
this.msgtype = msgtype;
this.text = text;
this.at = at;
}
public QyWetChatSend() {
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public ContentText getText() {
return text;
}
public void setText(ContentText text) {
this.text = text;
}
public ContentAt getAt() {
return at;
}
public void setAt(ContentAt at) {
this.at = at;
}
public static class ContentText implements Serializable {
private String content;
public ContentText(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
public static class ContentAt implements Serializable {
private String[] atMobiles = new String[]{"xxx"};
private boolean isAtAll = false;
public String[] getAtMobiles() {
return atMobiles;
}
public void setAtMobiles(String[] atMobiles) {
this.atMobiles = atMobiles;
}
public boolean isAtAll() {
return isAtAll;
}
public void setAtAll(boolean atAll) {
isAtAll = atAll;
}
}
}
2.工具类
package com.util.qunar;
import com.alibaba.fastjson.JSONObject;
import com.model.QyWetChatSend;
import com.util.HttpClientHelper;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author Foam
* @Description:微信机器人通知工具
* @date: 2022/6/14 9:12
*/
public class WeChatRobotUtils {
private static String defaultToken = "你自己的key";
/**
* @description:推送通知
* @param msg 需要推送的消息
* @param key 机器人的key
* @author: Foam
* @date: 2022/6/14
**/
public static String pushNotice(String msg,String key){
QyWetChatSend qyWetChatSend = new QyWetChatSend(msg);
String json = JSONObject.toJSONString(qyWetChatSend);
if(QUtils.isnull(key)){
key = defaultToken;
}
return HttpClientHelper.doPost("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + key, json, 6000);
}
/**
* @description:使用默认key
* @author: Foam
* @date: 2022/6/14
**/
public static String pushNotice(String msg){
return pushNotice(msg,null);
}
}
3.枚举
package com.tthk.inland.ticket.core.enums.wechat;
/**
* @Description 微信机器人枚举(异常,警告,调试,通知)
* @date 2022/10/10_11:03
* @author Foam
*/
public enum WeChatEnums {
NOTIFY("3876a440-****-****-****-511b62d185e8","日常消息通知"),
DEBUG("3d1faea7-****-****-****-608bcfbc18d9","调试级别通知"),
WARN("796053b1-****-****-****-8d377e2fc917","警告级别通知"),
ERROR("56e3b57e-****-****-****-1036a8c74074","异常,错误消息丢这里");
/**
* @description:机器人密钥
* @Author:Foam
* @Date:2022/10/10 11:06
* @return:null
**/
private String robot;
/**
* @description:备注
* @Author:Foam
* @Date:2022/10/10 11:07
* @return:null
**/
private String remark;
WeChatEnums(String robot, String remark) {
this.robot = robot;
this.remark = remark;
}
public String getRobot() {
return robot;
}
}
参考文档:https://blog.csdn.net/hanne_lovegood/article/details/121681669