钉钉通知到群
1. 代码
1.1 javabean
import java.util.List; public class DingTalkMsg { private String msgtype; private TextBean text; private AtBean at; public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public TextBean getText() { return text; } public void setText(TextBean text) { this.text = text; } public AtBean getAt() { return at; } public void setAt(AtBean at) { this.at = at; } public static class TextBean { /** * content : 我就是我, 是不一样的烟火 */ private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } } public static class AtBean { /** * atMobiles : ["156xxxx8827","189xxxx8325"] * isAtAll : false */ private boolean isAtAll; private List<String> atMobiles; public boolean isIsAtAll() { return isAtAll; } public void setIsAtAll(boolean isAtAll) { this.isAtAll = isAtAll; } public List<String> getAtMobiles() { return atMobiles; } public void setAtMobiles(List<String> atMobiles) { this.atMobiles = atMobiles; } } }
1.2 service
import java.util.List; public interface RobotService { /** * 机器人监控警报 */ void monitorAlarm(String content, List<String> phones); }
1.3 serviceImpl
import com.alibaba.fastjson.JSONObject; import org.apache.commons.collections4.CollectionUtils; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.Arrays; import java.util.List; @Service public class RobotServiceImpl implements RobotService { // @Resource private RestTemplate httpsRestTemplate = new RestTemplate(); /** * @author * @description 报警机器人(不传手机号默认全部) * @param content 消息内容 * @param phones 通知人手机号 * @return * @date 2017-12-27 13:14:08 */ @Override public void monitorAlarm(String content, List<String> phones) { String WEBHOOK_TOKEN = "https://oapi.dingtalk.com/robot/send?access_token=#####"; // 不传手机号默认全部 boolean isAtAll = Boolean.FALSE; if(CollectionUtils.isEmpty(phones)){ isAtAll = Boolean.TRUE; } DingTalkMsg dingTalkMsg = new DingTalkMsg(); dingTalkMsg.setMsgtype("text"); DingTalkMsg.TextBean textBean = new DingTalkMsg.TextBean(); textBean.setContent(content); dingTalkMsg.setText(textBean); DingTalkMsg.AtBean atBean = new DingTalkMsg.AtBean(); atBean.setAtMobiles(phones); atBean.setIsAtAll(isAtAll); dingTalkMsg.setAt(atBean); //设置请求头 HttpHeaders httpHeader = new HttpHeaders(); httpHeader.setContentType(MediaType.APPLICATION_JSON); HttpEntity request = new HttpEntity<>(dingTalkMsg, httpHeader); try { JSONObject jsonResult = httpsRestTemplate.postForObject(WEBHOOK_TOKEN, request, JSONObject.class); } catch (RestClientException e) { e.printStackTrace(); } } public static void main(String[] args){ RobotService robotService = new RobotServiceImpl(); robotService.monitorAlarm("到此一游", Arrays.asList("131****3736")); } }
2. 效果