WxCpGroupRobotMessage.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package cn.nosum.wx.cp.entity.message;
  2. import com.google.gson.JsonArray;
  3. import com.google.gson.JsonObject;
  4. import lombok.AllArgsConstructor;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. import lombok.experimental.Accessors;
  8. import cn.nosum.wx.cp.entity.article.NewArticle;
  9. import java.io.Serializable;
  10. import java.util.List;
  11. import static cn.nosum.wx.cp.constant.WxCpConstants.GroupRobotMsgType.*;
  12. /**
  13. * 微信群机器人消息
  14. *
  15. * @author Young
  16. */
  17. @AllArgsConstructor
  18. @NoArgsConstructor
  19. @Accessors(chain = true)
  20. @Data
  21. public class WxCpGroupRobotMessage implements Serializable {
  22. private static final long serialVersionUID = -4301684507150486556L;
  23. /**
  24. * 消息类型
  25. */
  26. private String msgType;
  27. /**
  28. * 文本内容,最长不超过2048个字节,markdown内容,最长不超过4096个字节,必须是utf8编码
  29. * 必填
  30. */
  31. private String content;
  32. /**
  33. * userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list
  34. */
  35. private List<String> mentionedList;
  36. /**
  37. * 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
  38. */
  39. private List<String> mentionedMobileList;
  40. /**
  41. * 图片内容的base64编码
  42. */
  43. private String base64;
  44. /**
  45. * 图片内容(base64编码前)的md5值
  46. */
  47. private String md5;
  48. /**
  49. * 图文消息,一个图文消息支持1到8条图文
  50. */
  51. private List<NewArticle> articles;
  52. public String toJson() {
  53. JsonObject messageJson = new JsonObject();
  54. messageJson.addProperty("msgtype", this.getMsgType());
  55. switch (this.getMsgType()) {
  56. case TEXT: {
  57. JsonObject text = new JsonObject();
  58. JsonArray uidJsonArray = new JsonArray();
  59. JsonArray mobileJsonArray = new JsonArray();
  60. text.addProperty("content", this.getContent());
  61. if (this.getMentionedList() != null) {
  62. for (String item : this.getMentionedList()) {
  63. uidJsonArray.add(item);
  64. }
  65. }
  66. if (this.getMentionedMobileList() != null) {
  67. for (String item : this.getMentionedMobileList()) {
  68. mobileJsonArray.add(item);
  69. }
  70. }
  71. text.add("mentioned_list", uidJsonArray);
  72. text.add("mentioned_mobile_list", mobileJsonArray);
  73. messageJson.add("text", text);
  74. break;
  75. }
  76. case MARKDOWN: {
  77. JsonObject text = new JsonObject();
  78. text.addProperty("content", this.getContent());
  79. messageJson.add("markdown", text);
  80. break;
  81. }
  82. case IMAGE: {
  83. JsonObject text = new JsonObject();
  84. text.addProperty("base64", this.getBase64());
  85. text.addProperty("md5", this.getMd5());
  86. messageJson.add("image", text);
  87. break;
  88. }
  89. case NEWS: {
  90. JsonObject text = new JsonObject();
  91. JsonArray array = new JsonArray();
  92. for (NewArticle article : this.getArticles()) {
  93. JsonObject articleJson = new JsonObject();
  94. articleJson.addProperty("title", article.getTitle());
  95. articleJson.addProperty("description", article.getDescription());
  96. articleJson.addProperty("url", article.getUrl());
  97. articleJson.addProperty("picurl", article.getPicUrl());
  98. array.add(articleJson);
  99. }
  100. text.add("articles", array);
  101. messageJson.add("news", text);
  102. break;
  103. }
  104. default:
  105. }
  106. return messageJson.toString();
  107. }
  108. }