WebhookService.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.qs.mp.common.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.qs.mp.common.enums.ServerEnvEnum;
  4. import com.qs.mp.common.utils.http.OkHttpUtil;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Component;
  9. @Component
  10. public class WebhookService {
  11. private static Logger logger = LoggerFactory.getLogger(WebhookService.class);
  12. //private String dingToken = "https://oapi.dingtalk.com/robot/send?access_token=a2f955b2c685e4449ac98fd4a41b9ba1238a6badb58aba4da9c6fc50714fc43d";
  13. //final static String alertUrl = "https://oapi.dingtalk.com/robot/send?access_token=79bd929c6cdec2150dbd2d5fdb5b4a499c9ad8137da511eab85bc7d381af9d7a";
  14. //final static String url = "https://oapi.dingtalk.com/robot/send?access_token=a2f955b2c685e4449ac98fd4a41b9ba1238a6badb58aba4da9c6fc50714fc43d";
  15. final static String wxLogMonitorUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4275487c-8706-4676-ae21-e74db900fcb6";
  16. private static String wxRobotUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=dd8d3a48-01f2-44b0-b4bb-406e2f466050";
  17. private static String env;
  18. @Value("${server.env}")
  19. public void setEnv(String serverEnv) {
  20. env = serverEnv;
  21. }
  22. /*
  23. public static void sendDing(String msg) {
  24. sendDing(url, msg, null);
  25. }
  26. public static void sendAlertDing(String msg) {
  27. sendDing(alertUrl, msg, null);
  28. }
  29. public static void sendDing(String token, String msg, String[] mobiles) {
  30. DingTalkClient client = new DefaultDingTalkClient(token);
  31. OapiRobotSendRequest request = new OapiRobotSendRequest();
  32. request.setMsgtype("text");
  33. OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
  34. text.setContent(msg);
  35. request.setText(text);
  36. OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
  37. if (mobiles != null) {
  38. at.setAtMobiles(Arrays.asList(mobiles));
  39. }
  40. request.setAt(at);
  41. try {
  42. OapiRobotSendResponse response = client.execute(request);
  43. logger.info("errorCode:"+response.getErrorCode());
  44. } catch (ApiException e) {
  45. logger.error("钉钉消息发送失败");
  46. }
  47. }*/
  48. public static String sendWXRobot(String url , String msg) {
  49. //System.out.println("企业微信机器人告警发送:" + msg);
  50. String jsonStr = "{\n" +
  51. " \"msgtype\": \"text\",\n" +
  52. " \"text\": {\n" +
  53. " \"content\": "+ JSON.toJSONString(msg)+"\n" +
  54. " }\n" +
  55. " }";
  56. //System.out.println(jsonStr);
  57. return OkHttpUtil.post(url, jsonStr, null);
  58. }
  59. public static String sendLogAlertMsg(String msg) {
  60. return sendWXRobot(wxLogMonitorUrl,msg);
  61. }
  62. public static String sendMonitorData(String msg){
  63. if (ServerEnvEnum.PROD.getCode().equals(env)){
  64. return sendWXRobot(wxRobotUrl,msg);
  65. }
  66. LogUtil.info(logger, "当前环境为{0},不发送业务监控消息。msg:{1}", new Object[]{env, msg});
  67. return null;
  68. }
  69. public static void main(String[] args){
  70. WebhookService.sendLogAlertMsg("测试下\"xx");
  71. }
  72. }