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