package com.qs.mp.common.utils; import com.alibaba.fastjson.JSON; import com.qs.mp.common.enums.ServerEnvEnum; import com.qs.mp.common.utils.http.OkHttpUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class WebhookService { private static Logger logger = LoggerFactory.getLogger(WebhookService.class); //private String dingToken = "https://oapi.dingtalk.com/robot/send?access_token=a2f955b2c685e4449ac98fd4a41b9ba1238a6badb58aba4da9c6fc50714fc43d"; //final static String alertUrl = "https://oapi.dingtalk.com/robot/send?access_token=79bd929c6cdec2150dbd2d5fdb5b4a499c9ad8137da511eab85bc7d381af9d7a"; //final static String url = "https://oapi.dingtalk.com/robot/send?access_token=a2f955b2c685e4449ac98fd4a41b9ba1238a6badb58aba4da9c6fc50714fc43d"; final static String wxLogMonitorUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4275487c-8706-4676-ae21-e74db900fcb6"; private static String wxRobotUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=dd8d3a48-01f2-44b0-b4bb-406e2f466050"; private static String env; @Value("${server.env}") public void setEnv(String serverEnv) { env = serverEnv; } /* public static void sendDing(String msg) { sendDing(url, msg, null); } public static void sendAlertDing(String msg) { sendDing(alertUrl, msg, null); } public static void sendDing(String token, String msg, String[] mobiles) { DingTalkClient client = new DefaultDingTalkClient(token); OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype("text"); OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text(); text.setContent(msg); request.setText(text); OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); if (mobiles != null) { at.setAtMobiles(Arrays.asList(mobiles)); } request.setAt(at); try { OapiRobotSendResponse response = client.execute(request); logger.info("errorCode:"+response.getErrorCode()); } catch (ApiException e) { logger.error("钉钉消息发送失败"); } }*/ public static String sendWXRobot(String url , String msg) { //System.out.println("企业微信机器人告警发送:" + msg); String jsonStr = "{\n" + " \"msgtype\": \"text\",\n" + " \"text\": {\n" + " \"content\": "+ JSON.toJSONString(msg)+"\n" + " }\n" + " }"; //System.out.println(jsonStr); return OkHttpUtil.post(url, jsonStr, null); } public static String sendLogAlertMsg(String msg) { return sendWXRobot(wxLogMonitorUrl,msg); } public static String sendMonitorData(String msg){ if (ServerEnvEnum.PROD.getCode().equals(env)){ return sendWXRobot(wxRobotUrl,msg); } LogUtil.info(logger, "当前环境为{0},不发送业务监控消息。msg:{1}", new Object[]{env, msg}); return null; } public static void main(String[] args){ WebhookService.sendLogAlertMsg("测试下\"xx"); } }