当前位置:   article > 正文

Java企业微信机器人实现_wxcpgrouprobotservice

wxcpgrouprobotservice

1. 实体类

  1. package com.robot.message;
  2. /**
  3. * 群机器人的消息类型.
  4. */
  5. public class GroupRobotMsgType {
  6. /**
  7. * 文本消息.
  8. */
  9. public static final String TEXT = "text";
  10. /**
  11. * 图片消息.
  12. */
  13. public static final String IMAGE = "image";
  14. /**
  15. * markdown消息.
  16. */
  17. public static final String MARKDOWN = "markdown";
  18. /**
  19. * 图文消息(点击跳转到外链).
  20. */
  21. public static final String NEWS = "news";
  22. }
  23. //-------------------------------------------------------------------
  24. package com.robot.message;
  25. import lombok.AllArgsConstructor;
  26. import lombok.Data;
  27. import lombok.NoArgsConstructor;
  28. import lombok.experimental.Accessors;
  29. import java.io.Serializable;
  30. /**
  31. * 图文类型
  32. */
  33. @Data
  34. @NoArgsConstructor
  35. @AllArgsConstructor
  36. @Accessors(chain = true)
  37. public class NewArticle implements Serializable {
  38. private static final long serialVersionUID = 4087852055781140659L;
  39. /**
  40. * 标题,不超过128个字节,超过会自动截断
  41. */
  42. private String title;
  43. /**
  44. * 描述,不超过512个字节,超过会自动截断
  45. */
  46. private String description;
  47. /**
  48. * 点击后跳转的链接。
  49. */
  50. private String url;
  51. /**
  52. * 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图1068*455,小图150*150。
  53. */
  54. private String picUrl;
  55. /**
  56. * 按钮文字,仅在图文数为1条时才生效。 默认为“阅读全文”, 不超过4个文字,超过自动截断。该设置只在企业微信上生效,微工作台(原企业号)上不生效。
  57. */
  58. private String btnText;
  59. }
  60. //-------------------------------------------------------------------
  61. package com.robot.message;
  62. import cn.hutool.json.JSONArray;
  63. import cn.hutool.json.JSONObject;
  64. import lombok.AllArgsConstructor;
  65. import lombok.Data;
  66. import lombok.NoArgsConstructor;
  67. import lombok.experimental.Accessors;
  68. import java.util.List;
  69. /**
  70. * 微信群机器人消息
  71. *
  72. * @author xiaomingzhang
  73. */
  74. @Data
  75. @AllArgsConstructor
  76. @NoArgsConstructor
  77. @Accessors(chain = true)
  78. public class WxCpGroupRobotMessage {
  79. /**
  80. * 消息类型
  81. */
  82. private String msgType;
  83. /**
  84. * 文本内容,最长不超过2048个字节,markdown内容,最长不超过4096个字节,必须是utf8编码
  85. * 必填
  86. */
  87. private String content;
  88. /**
  89. * userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list
  90. */
  91. private List<String> mentionedList;
  92. /**
  93. * 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
  94. */
  95. private List<String> mentionedMobileList;
  96. /**
  97. * 图片内容的base64编码
  98. */
  99. private String base64;
  100. /**
  101. * 图片内容(base64编码前)的md5值
  102. */
  103. private String md5;
  104. /**
  105. * 图文消息,一个图文消息支持1到8条图文
  106. */
  107. private List<NewArticle> articles;
  108. public String toJson() {
  109. JSONObject messageJson = new JSONObject().set("msgtype", this.getMsgType());
  110. switch (this.getMsgType()) {
  111. case GroupRobotMsgType.TEXT: {
  112. JSONObject text = new JSONObject();
  113. JSONArray uidJsonArray = new JSONArray();
  114. JSONArray mobileJsonArray = new JSONArray();
  115. text.set("content", this.getContent());
  116. if (this.getMentionedList() != null) {
  117. for (String item : this.getMentionedList()) {
  118. uidJsonArray.add(item);
  119. }
  120. }
  121. if (this.getMentionedMobileList() != null) {
  122. for (String item : this.getMentionedMobileList()) {
  123. mobileJsonArray.add(item);
  124. }
  125. }
  126. text.set("mentioned_list", uidJsonArray);
  127. text.set("mentioned_mobile_list", mobileJsonArray);
  128. messageJson.set("text", text);
  129. break;
  130. }
  131. case GroupRobotMsgType.MARKDOWN: {
  132. JSONObject text = new JSONObject().set("content", this.getContent());
  133. messageJson.set("markdown", text);
  134. break;
  135. }
  136. case GroupRobotMsgType.IMAGE: {
  137. JSONObject text = new JSONObject()
  138. .set("base64", this.getBase64())
  139. .set("md5", this.getMd5());
  140. messageJson.set("image", text);
  141. break;
  142. }
  143. case GroupRobotMsgType.NEWS: {
  144. JSONObject text = new JSONObject();
  145. JSONArray array = new JSONArray();
  146. for (NewArticle article : this.getArticles()) {
  147. JSONObject articleJson = new JSONObject()
  148. .set("title", article.getTitle())
  149. .set("description", article.getDescription())
  150. .set("url", article.getUrl())
  151. .set("picurl", article.getPicUrl());
  152. array.add(articleJson);
  153. }
  154. text.set("articles", array);
  155. messageJson.set("news", text);
  156. break;
  157. }
  158. default:
  159. }
  160. return messageJson.toString();
  161. }
  162. }

2. 接口实现

  1. package com.robot.service;
  2. import com.robot.message.NewArticle;
  3. import java.util.List;
  4. /**
  5. * 微信群机器人消息发送api<br/>
  6. *
  7. * @author xiaomingzhang
  8. * @see <a href="https://work.weixin.qq.com/help?doc_id=13376">文档地址</a>
  9. * @see <a href="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=">调用地址</a>
  10. */
  11. public interface WxCpGroupRobotService {
  12. /**
  13. * 发送text类型的消息
  14. *
  15. * @param content 文本内容,最长不超过2048个字节,必须是utf8编码
  16. * @param mentionedList userId的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userId,可以使用mentioned_mobile_list
  17. * @param mobileList 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
  18. */
  19. void sendText(String content, List<String> mentionedList, List<String> mobileList);
  20. /**
  21. * 发送markdown类型的消息
  22. *
  23. * @param content markdown内容,最长不超过4096个字节,必须是utf8编码
  24. */
  25. void sendMarkdown(String content);
  26. /**
  27. * 发送image类型的消息
  28. *
  29. * @param base64 图片内容的base64编码
  30. * @param md5 图片内容(base64编码前)的md5值
  31. */
  32. void sendImage(String base64, String md5);
  33. /**
  34. * 发送news类型的消息
  35. *
  36. * @param articleList 图文消息,支持1到8条图文
  37. */
  38. void sendNews(List<NewArticle> articleList);
  39. /**
  40. * 发送text类型的消息
  41. *
  42. * @param webhookUrl webhook地址
  43. * @param content 文本内容,最长不超过2048个字节,必须是utf8编码
  44. * @param mentionedList userId的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userId,可以使用mentioned_mobile_list
  45. * @param mobileList 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
  46. */
  47. void sendText(String webhookUrl, String content, List<String> mentionedList, List<String> mobileList);
  48. /**
  49. * 发送markdown类型的消息
  50. *
  51. * @param webhookUrl webhook地址
  52. * @param content markdown内容,最长不超过4096个字节,必须是utf8编码
  53. */
  54. void sendMarkdown(String webhookUrl, String content);
  55. /**
  56. * 发送image类型的消息
  57. *
  58. * @param webhookUrl webhook地址
  59. * @param base64 图片内容的base64编码
  60. * @param md5 图片内容(base64编码前)的md5值
  61. */
  62. void sendImage(String webhookUrl, String base64, String md5);
  63. /**
  64. * 发送news类型的消息
  65. *
  66. * @param webhookUrl webhook地址
  67. * @param articleList 图文消息,支持1到8条图文
  68. */
  69. void sendNews(String webhookUrl, List<NewArticle> articleList);
  70. }
  71. //------------------------------------------------------------------
  72. package com.robot.service.impl;
  73. import cn.hutool.core.util.StrUtil;
  74. import cn.hutool.http.HttpUtil;
  75. import com.robot.WxErrorException;
  76. import com.robot.message.GroupRobotMsgType;
  77. import com.robot.message.NewArticle;
  78. import com.robot.message.WxCpGroupRobotMessage;
  79. import com.robot.service.WxCpGroupRobotService;
  80. import lombok.SneakyThrows;
  81. import java.util.List;
  82. /**
  83. * 企业微信群机器人消息发送API 实现
  84. *
  85. * @author xiaomingzhang
  86. */
  87. public class WxCpGroupRobotServiceImpl implements WxCpGroupRobotService {
  88. public static final String CP_BASE_URL = "https://qyapi.weixin.qq.com";
  89. public static final String WEBHOOK_SEND = "/cgi-bin/webhook/send?key=";
  90. public final String webhookKey;
  91. @SneakyThrows
  92. public WxCpGroupRobotServiceImpl(String webhookKey) {
  93. if (StrUtil.isBlank(webhookKey)) {
  94. throw new WxErrorException("无效的企业微信群机器人KEY");
  95. }
  96. this.webhookKey = webhookKey;
  97. }
  98. private String getWebhookUrl() {
  99. return CP_BASE_URL + WEBHOOK_SEND + webhookKey;
  100. }
  101. @Override
  102. public void sendText(String content, List<String> mentionedList, List<String> mobileList) {
  103. this.sendText(this.getWebhookUrl(), content, mentionedList, mobileList);
  104. }
  105. @Override
  106. public void sendMarkdown(String content) {
  107. this.sendMarkdown(this.getWebhookUrl(), content);
  108. }
  109. @Override
  110. public void sendImage(String base64, String md5) {
  111. this.sendImage(this.getWebhookUrl(), base64, md5);
  112. }
  113. @Override
  114. public void sendNews(List<NewArticle> articleList) {
  115. this.sendNews(this.getWebhookUrl(), articleList);
  116. }
  117. @Override
  118. public void sendText(String webhookUrl, String content, List<String> mentionedList, List<String> mobileList) {
  119. HttpUtil.post(webhookUrl, new WxCpGroupRobotMessage()
  120. .setMsgType(GroupRobotMsgType.TEXT)
  121. .setContent(content)
  122. .setMentionedList(mentionedList)
  123. .setMentionedMobileList(mobileList)
  124. .toJson());
  125. }
  126. @Override
  127. public void sendMarkdown(String webhookUrl, String content) {
  128. HttpUtil.post(webhookUrl, new WxCpGroupRobotMessage()
  129. .setMsgType(GroupRobotMsgType.MARKDOWN)
  130. .setContent(content)
  131. .toJson());
  132. }
  133. @Override
  134. public void sendImage(String webhookUrl, String base64, String md5) {
  135. HttpUtil.post(this.getWebhookUrl(), new WxCpGroupRobotMessage()
  136. .setMsgType(GroupRobotMsgType.IMAGE)
  137. .setBase64(base64)
  138. .setMd5(md5).toJson());
  139. }
  140. @Override
  141. public void sendNews(String webhookUrl, List<NewArticle> articleList) {
  142. HttpUtil.post(this.getWebhookUrl(), new WxCpGroupRobotMessage()
  143. .setMsgType(GroupRobotMsgType.NEWS)
  144. .setArticles(articleList).toJson());
  145. }
  146. }

3. 异常类

  1. package com.robot.exception;
  2. /**
  3. * WxErrorException
  4. */
  5. public class WxErrorException extends Exception {
  6. // ~ Constructors
  7. // ===================================================================================================
  8. /**
  9. * Constructs a <code>WxErrorException</code> with the specified message.
  10. *
  11. * @param msg the detail message
  12. */
  13. public WxErrorException(String msg) {
  14. super(msg);
  15. }
  16. /**
  17. * Constructs a <code>WxErrorException</code> with the specified message and
  18. * root cause.
  19. *
  20. * @param msg the detail message
  21. * @param t root cause
  22. */
  23. public WxErrorException(String msg, Throwable t) {
  24. super(msg, t);
  25. }
  26. }

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号