From 287f961914847e9e0bfdbaaa80311deb568d80fc Mon Sep 17 00:00:00 2001 From: ChengKeJ Date: Thu, 2 Feb 2023 13:51:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?:new:=E3=80=90=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E3=80=91=E6=A8=A1=E7=89=88=E5=8D=A1=E7=89=87=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=A2=9E=E5=8A=A0=E6=9B=B4=E6=96=B0=E4=B8=BA=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E5=8D=A1=E7=89=87=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpTaskCardService.java | 2 + .../cp/api/impl/WxCpTaskCardServiceImpl.java | 7 + .../cp/bean/message/TemplateCardMessage.java | 276 ++++++++++++++++++ 3 files changed, 285 insertions(+) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/TemplateCardMessage.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTaskCardService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTaskCardService.java index ee28d81e58..303d22f692 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTaskCardService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTaskCardService.java @@ -1,6 +1,7 @@ package me.chanjar.weixin.cp.api; import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.cp.bean.message.TemplateCardMessage; import java.util.List; @@ -48,4 +49,5 @@ void updateTemplateCardButton(List userIds, List partyIds, List tagIds, Integer atAll, String responseCode, String replaceName) throws WxErrorException; + void updateTemplateCardButton(TemplateCardMessage templateCardMessage) throws WxErrorException; } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTaskCardServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTaskCardServiceImpl.java index 0180efddd3..4802c5549c 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTaskCardServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTaskCardServiceImpl.java @@ -5,6 +5,7 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpTaskCardService; +import me.chanjar.weixin.cp.bean.message.TemplateCardMessage; import java.util.HashMap; import java.util.List; @@ -60,4 +61,10 @@ public void updateTemplateCardButton(List userIds, List partyId this.mainService.post(url, WxGsonBuilder.create().toJson(data)); } + + @Override + public void updateTemplateCardButton(TemplateCardMessage templateCardMessage) throws WxErrorException { + String url = this.mainService.getWxCpConfigStorage().getApiUrl(UPDATE_TEMPLATE_CARD); + this.mainService.post(url, WxGsonBuilder.create().toJson(templateCardMessage)); + } } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/TemplateCardMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/TemplateCardMessage.java new file mode 100644 index 0000000000..417b5e80e7 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/TemplateCardMessage.java @@ -0,0 +1,276 @@ +package me.chanjar.weixin.cp.bean.message; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.List; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class TemplateCardMessage implements Serializable { + private static final long serialVersionUID = 8833792280163704239L; + + @JsonProperty("userids") + private List userids; + @JsonProperty("partyids") + private List partyids; + @JsonProperty("tagids") + private List tagids; + @JsonProperty("atall") + private Integer atall; + @JsonProperty("agentid") + private Integer agentid; + @JsonProperty("response_code") + private String responseCode; + @JsonProperty("enable_id_trans") + private Integer enableIdTrans; + @JsonProperty("template_card") + private TemplateCardDTO templateCard; + + @NoArgsConstructor + @Data + public static class TemplateCardDTO { + @JsonProperty("card_type") + private String cardType; + @JsonProperty("source") + private SourceDTO source; + @JsonProperty("main_title") + private MainTitleDTO mainTitle; + @JsonProperty("select_list") + private List selectList; + @JsonProperty("submit_button") + private SubmitButtonDTO submitButton; + @JsonProperty("replace_text") + private String replaceText; + + @JsonProperty("checkbox") + private CheckboxDTO checkbox; + + + @JsonProperty("action_menu") + private ActionMenuDTO actionMenu; + @JsonProperty("quote_area") + private QuoteAreaDTO quoteArea; + @JsonProperty("sub_title_text") + private String subTitleText; + @JsonProperty("horizontal_content_list") + private List horizontalContentList; + @JsonProperty("card_action") + private CardActionDTO cardAction; + @JsonProperty("button_selection") + private ButtonSelectionDTO buttonSelection; + @JsonProperty("button_list") + private List buttonList; + + @JsonProperty("image_text_area") + private ImageTextAreaDTO imageTextArea; + @JsonProperty("card_image") + private CardImageDTO cardImage; + @JsonProperty("vertical_content_list") + private List verticalContentList; + @JsonProperty("jump_list") + private List jumpList; + + + @NoArgsConstructor + @Data + public static class SourceDTO { + @JsonProperty("icon_url") + private String iconUrl; + @JsonProperty("desc") + private String desc; + @JsonProperty("desc_color") + private Integer descColor; + } + + @NoArgsConstructor + @Data + public static class ActionMenuDTO { + @JsonProperty("desc") + private String desc; + @JsonProperty("action_list") + private List actionList; + } + + @NoArgsConstructor + @Data + public static class QuoteAreaDTO { + @JsonProperty("type") + private Integer type; + @JsonProperty("url") + private String url; + @JsonProperty("title") + private String title; + @JsonProperty("quote_text") + private String quoteText; + } + + @NoArgsConstructor + @Data + public static class CardActionDTO { + @JsonProperty("type") + private Integer type; + @JsonProperty("url") + private String url; + @JsonProperty("appid") + private String appid; + @JsonProperty("pagepath") + private String pagepath; + } + + @NoArgsConstructor + @Data + public static class ButtonSelectionDTO { + @JsonProperty("question_key") + private String questionKey; + @JsonProperty("title") + private String title; + @JsonProperty("option_list") + private List optionList; + @JsonProperty("selected_id") + private String selectedId; + } + + @NoArgsConstructor + @Data + public static class HorizontalContentListDTO { + @JsonProperty("keyname") + private String keyname; + @JsonProperty("value") + private String value; + @JsonProperty("type") + private Integer type; + @JsonProperty("url") + private String url; + @JsonProperty("media_id") + private String mediaId; + @JsonProperty("userid") + private String userid; + } + + @NoArgsConstructor + @Data + public static class ButtonListDTO { + @JsonProperty("text") + private String text; + @JsonProperty("style") + private Integer style; + @JsonProperty("key") + private String key; + } + + + @NoArgsConstructor + @Data + public static class CheckboxDTO { + @JsonProperty("question_key") + private String questionKey; + @JsonProperty("option_list") + private List optionList; + @JsonProperty("disable") + private Boolean disable; + @JsonProperty("mode") + private Integer mode; + + @NoArgsConstructor + @Data + public static class OptionListDTO { + @JsonProperty("id") + private String id; + @JsonProperty("text") + private String text; + @JsonProperty("is_checked") + private Boolean isChecked; + } + + } + + @NoArgsConstructor + @Data + public static class MainTitleDTO { + @JsonProperty("title") + private String title; + @JsonProperty("desc") + private String desc; + } + + @NoArgsConstructor + @Data + public static class SubmitButtonDTO { + @JsonProperty("text") + private String text; + @JsonProperty("key") + private String key; + } + + @NoArgsConstructor + @Data + public static class SelectListDTO { + @JsonProperty("question_key") + private String questionKey; + @JsonProperty("title") + private String title; + @JsonProperty("selected_id") + private String selectedId; + @JsonProperty("disable") + private Boolean disable; + @JsonProperty("option_list") + private List optionList; + + @NoArgsConstructor + @Data + public static class OptionListDTO { + @JsonProperty("id") + private String id; + @JsonProperty("text") + private String text; + } + } + + @NoArgsConstructor + @Data + public static class ImageTextAreaDTO { + @JsonProperty("type") + private Integer type; + @JsonProperty("url") + private String url; + @JsonProperty("title") + private String title; + @JsonProperty("desc") + private String desc; + @JsonProperty("image_url") + private String imageUrl; + } + + @NoArgsConstructor + @Data + public static class CardImageDTO { + @JsonProperty("url") + private String url; + @JsonProperty("aspect_ratio") + private Double aspectRatio; + } + + @NoArgsConstructor + @Data + public static class JumpListDTO { + @JsonProperty("type") + private Integer type; + @JsonProperty("title") + private String title; + @JsonProperty("url") + private String url; + @JsonProperty("appid") + private String appid; + @JsonProperty("pagepath") + private String pagepath; + } + + + } + +} From d3d8a5e1d818024a8accbe0357be56c5eb1b2188 Mon Sep 17 00:00:00 2001 From: ChengKeJ Date: Tue, 21 Feb 2023 15:23:19 +0800 Subject: [PATCH 2/3] =?UTF-8?q?:new:=E3=80=90=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E3=80=91=E5=AE=A2=E6=9C=8D=E6=B6=88=E6=81=AF=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BB=A5=E5=AE=A2=E6=9C=8D=E7=BA=AC=E5=BA=A6=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chanjar/weixin/cp/api/WxCpKfService.java | 4 +++ .../weixin/cp/api/impl/WxCpKfServiceImpl.java | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpKfService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpKfService.java index 6f47ab9084..48a51a16b6 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpKfService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpKfService.java @@ -135,9 +135,13 @@ WxCpKfServiceStateTransResp transServiceState(String openKfid, String externalUs * @return 微信消息 wx cp kf msg list resp * @throws WxErrorException 异常 */ + @Deprecated WxCpKfMsgListResp syncMsg(String cursor, String token, Integer limit, Integer voiceFormat) throws WxErrorException; + WxCpKfMsgListResp syncMsg(String cursor, String token, Integer limit, Integer voiceFormat,String open_kfid) + throws WxErrorException; + /** * 发送消息 * 当微信客户处于“新接入待处理”或“由智能助手接待”状态下,可调用该接口给用户发送消息。 diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpKfServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpKfServiceImpl.java index 247b1d0905..754aaa0643 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpKfServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpKfServiceImpl.java @@ -149,6 +149,31 @@ public WxCpKfMsgListResp syncMsg(String cursor, String token, Integer limit, Int return WxCpKfMsgListResp.fromJson(responseContent); } + @Override + public WxCpKfMsgListResp syncMsg(String cursor, String token, Integer limit, Integer voiceFormat, String openKfId) throws WxErrorException { + String url = cpService.getWxCpConfigStorage().getApiUrl(SYNC_MSG); + + JsonObject json = new JsonObject(); + if (cursor!=null) { + json.addProperty("cursor", cursor); + } + if (token!=null) { + json.addProperty("token", token); + } + if (limit!=null) { + json.addProperty("limit", limit); + } + if (voiceFormat!=null) { + json.addProperty("voice_format", voiceFormat); + } + if (openKfId != null) { + json.addProperty("open_kfid", openKfId); + } + + String responseContent = cpService.post(url, json); + return WxCpKfMsgListResp.fromJson(responseContent); + } + @Override public WxCpKfMsgSendResp sendMsg(WxCpKfMsgSendRequest request) throws WxErrorException { String url = cpService.getWxCpConfigStorage().getApiUrl(SEND_MSG); From 0c02f5adf35fae620a353902a196fc48e935bbe3 Mon Sep 17 00:00:00 2001 From: ChengKeJ Date: Tue, 21 Feb 2023 15:29:04 +0800 Subject: [PATCH 3/3] =?UTF-8?q?:new:=E3=80=90=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E3=80=91=E5=8F=91=E9=80=81=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=94=AF=E6=8C=81=E6=A8=A1=E6=9D=BF=E5=8D=A1?= =?UTF-8?q?=E7=89=87=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpGroupRobotService.java | 10 + .../api/impl/WxCpGroupRobotServiceImpl.java | 5 + .../bean/message/WxCpGroupRobotMessage.java | 324 ++++++++++++++++++ .../weixin/cp/constant/WxCpConsts.java | 5 + 4 files changed, 344 insertions(+) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java index 769cab6f7a..bc6e130548 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java @@ -2,6 +2,7 @@ import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.cp.bean.article.NewArticle; +import me.chanjar.weixin.cp.bean.message.WxCpGroupRobotMessage; import java.util.List; @@ -96,4 +97,13 @@ public interface WxCpGroupRobotService { * @throws WxErrorException 异常 */ void sendFile(String webhookUrl, String mediaId) throws WxErrorException; + + /** + * 发送模板卡片消息 + * @param webhookUrl + * @param wxCpGroupRobotMessage + * @throws WxErrorException + */ + void sendTemplateCardMessage(String webhookUrl, WxCpGroupRobotMessage wxCpGroupRobotMessage) throws WxErrorException; + } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java index a5749cbc1c..30e4474992 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java @@ -92,4 +92,9 @@ public void sendFile(String webhookUrl, String mediaId) throws WxErrorException .setMediaId(mediaId).toJson()); } + @Override + public void sendTemplateCardMessage(String webhookUrl, WxCpGroupRobotMessage wxCpGroupRobotMessage) throws WxErrorException { + this.cpService.postWithoutToken(webhookUrl, wxCpGroupRobotMessage.toJson()); + } + } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpGroupRobotMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpGroupRobotMessage.java index a0e9be359e..6c889b6cec 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpGroupRobotMessage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpGroupRobotMessage.java @@ -7,11 +7,14 @@ import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import me.chanjar.weixin.cp.bean.article.NewArticle; +import me.chanjar.weixin.cp.bean.templatecard.*; +import org.apache.commons.lang3.StringUtils; import java.io.Serializable; import java.util.List; import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.*; +import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.TEMPLATE_CARD; /** * 微信群机器人消息 @@ -61,6 +64,154 @@ public class WxCpGroupRobotMessage implements Serializable { */ private String mediaId; + private Integer agentId; + + // 模板型卡片特有属性 + /** + * 模板卡片类型,文本通知型卡片填写 “text_notice”, + * 图文展示型卡片此处填写 “news_notice”, + * 按钮交互型卡片填写”button_interaction”, + * 投票选择型卡片填写”vote_interaction”, + * 多项选择型卡片填写 “multiple_interaction” + */ + private String cardType; + + /** + * 卡片来源样式信息,不需要来源样式可不填写 + * 来源图片的url + */ + private String sourceIconUrl; + /** + * 卡片来源样式信息,不需要来源样式可不填写 + * 来源图片的描述,建议不超过20个字 + */ + private String sourceDesc; + + /** + * 来源文字的颜色,目前支持:0(默认) 灰色,1 黑色,2 红色,3 绿色 + */ + private Integer sourceDescColor; + + /** + * 更多操作界面的描述 + */ + private String actionMenuDesc; + + /** + * 操作列表,列表长度取值范围为 [1, 3] + */ + private List actionMenuActionList; + + /** + * 一级标题,建议不超过36个字 + */ + private String mainTitleTitle; + /** + * 标题辅助信息,建议不超过44个字 + */ + private String mainTitleDesc; + + /** + * 图文展示型的卡片必须有图片字段。 + * 图片的url. + */ + private String cardImageUrl; + + /** + * 图片的宽高比,宽高比要小于2.25,大于1.3,不填该参数默认1.3 + */ + private Float cardImageAspectRatio; + /** + * 关键数据样式 + * 关键数据样式的数据内容,建议不超过14个字 + */ + private String emphasisContentTitle; + /** + * 关键数据样式的数据描述内容,建议不超过22个字 + */ + private String emphasisContentDesc; + + /** + * 二级普通文本,建议不超过160个字 + */ + private String subTitleText; + + /** + * 卡片二级垂直内容,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过4 + */ + private List verticalContents; + + /** + * 二级标题+文本列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过6 + */ + private List horizontalContents; + + /** + * 跳转指引样式的列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过3 + */ + private List jumps; + + /** + * 整体卡片的点击跳转事件,text_notice必填本字段 + * 跳转事件类型,1 代表跳转url,2 代表打开小程序。text_notice卡片模版中该字段取值范围为[1,2] + */ + private Integer cardActionType; + /** + * 跳转事件的url,card_action.type是1时必填 + */ + private String cardActionUrl; + + /** + * 跳转事件的小程序的appid,必须是与当前应用关联的小程序,card_action.type是2时必填 + */ + private String cardActionAppid; + + /** + * 跳转事件的小程序的pagepath,card_action.type是2时选填 + */ + private String cardActionPagepath; + + /** + * 按钮交互型卡片需指定。 + * 按钮列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过6 + */ + private List buttons; + + /** + * 投票选择型卡片需要指定 + * 选择题key值,用户提交选项后,会产生回调事件,回调事件会带上该key值表示该题,最长支持1024字节 + */ + private String checkboxQuestionKey; + + /** + * 选择题模式,单选:0,多选:1,不填默认0 + */ + private Integer checkboxMode; + + /** + * 选项list,选项个数不超过 20 个,最少1个 + */ + private List options; + + /** + * 提交按钮样式 + * 按钮文案,建议不超过10个字,不填默认为提交 + */ + private String submitButtonText; + /** + * 提交按钮的key,会产生回调事件将本参数作为EventKey返回,最长支持1024字节 + */ + private String submitButtonKey; + /** + * 下拉式的选择器列表,multiple_interaction类型的卡片该字段不可为空,一个消息最多支持 3 个选择器 + */ + private List selects; + + /** + * 引用文献样式 + */ + private QuoteArea quoteArea; + /** * To json string. * @@ -69,6 +220,9 @@ public class WxCpGroupRobotMessage implements Serializable { public String toJson() { JsonObject messageJson = new JsonObject(); messageJson.addProperty("msgtype", this.getMsgType()); + if (this.getAgentId() != null) { + messageJson.addProperty("agentid", this.getAgentId()); + } switch (this.getMsgType()) { case TEXT: { @@ -127,6 +281,176 @@ public String toJson() { messageJson.add("file", file); break; } + case TEMPLATE_CARD: { + JsonObject template = new JsonObject(); + template.addProperty("card_type", this.getCardType()); + + if (StringUtils.isNotBlank(this.getSourceIconUrl()) || StringUtils.isNotBlank(this.getSourceDesc())) { + JsonObject source = new JsonObject(); + if (StringUtils.isNotBlank(this.getSourceIconUrl())) { + source.addProperty("icon_url", this.getSourceIconUrl()); + } + if (StringUtils.isNotBlank(this.getSourceDesc())) { + source.addProperty("desc", this.getSourceDesc()); + } + source.addProperty("desc_color", this.getSourceDescColor()); + template.add("source", source); + } + + if (StringUtils.isNotBlank(this.getActionMenuDesc())) { + JsonObject action_menu = new JsonObject(); + action_menu.addProperty("desc", this.getActionMenuDesc()); + JsonArray actionList = new JsonArray(); + List actionMenuItemList = this.getActionMenuActionList(); + for (ActionMenuItem actionItemI : actionMenuItemList) { + actionList.add(actionItemI.toJson()); + } + action_menu.add("action_list", actionList); + template.add("action_menu", action_menu); + } + + if (StringUtils.isNotBlank(this.getMainTitleTitle()) || StringUtils.isNotBlank(this.getMainTitleDesc())) { + JsonObject mainTitle = new JsonObject(); + if (StringUtils.isNotBlank(this.getMainTitleTitle())) { + mainTitle.addProperty("title", this.getMainTitleTitle()); + } + if (StringUtils.isNotBlank(this.getMainTitleDesc())) { + mainTitle.addProperty("desc", this.getMainTitleDesc()); + } + template.add("main_title", mainTitle); + } + + if (StringUtils.isNotBlank(this.getCardImageUrl()) || this.getCardImageAspectRatio() != null) { + JsonObject cardImage = new JsonObject(); + if (StringUtils.isNotBlank(this.getCardImageUrl())) { + cardImage.addProperty("url", this.getCardImageUrl()); + } + if (null != this.getCardImageAspectRatio()) { + cardImage.addProperty("aspect_ratio", this.getCardImageAspectRatio()); + } + template.add("card_image", cardImage); + } + + if (StringUtils.isNotBlank(this.getEmphasisContentTitle()) || StringUtils.isNotBlank(this.getEmphasisContentDesc())) { + JsonObject emphasisContent = new JsonObject(); + if (StringUtils.isNotBlank(this.getEmphasisContentTitle())) { + emphasisContent.addProperty("title", this.getEmphasisContentTitle()); + } + if (StringUtils.isNotBlank(this.getEmphasisContentDesc())) { + emphasisContent.addProperty("desc", this.getEmphasisContentDesc()); + } + template.add("emphasis_content", emphasisContent); + } + + + if (StringUtils.isNotBlank(this.getSubTitleText())) { + template.addProperty("sub_title_text", this.getSubTitleText()); + } + + List verticalContents = this.getVerticalContents(); + if (null != verticalContents && !verticalContents.isEmpty()) { + JsonArray vContentJsonArray = new JsonArray(); + for (VerticalContent vContent : this.getVerticalContents()) { + JsonObject tempObject = vContent.toJson(); + vContentJsonArray.add(tempObject); + } + template.add("vertical_content_list", vContentJsonArray); + } + + List horizontalContents = this.getHorizontalContents(); + if (null != horizontalContents && !horizontalContents.isEmpty()) { + JsonArray hContentJsonArray = new JsonArray(); + for (HorizontalContent hContent : this.getHorizontalContents()) { + JsonObject tempObject = hContent.toJson(); + hContentJsonArray.add(tempObject); + } + template.add("horizontal_content_list", hContentJsonArray); + } + + List jumps = this.getJumps(); + if (null != jumps && !jumps.isEmpty()) { + JsonArray jumpJsonArray = new JsonArray(); + for (TemplateCardJump jump : this.getJumps()) { + JsonObject tempObject = jump.toJson(); + jumpJsonArray.add(tempObject); + } + template.add("jump_list", jumpJsonArray); + } + + if (null != this.getCardActionType()) { + JsonObject cardAction = new JsonObject(); + cardAction.addProperty("type", this.getCardActionType()); + if (StringUtils.isNotBlank(this.getCardActionUrl())) { + cardAction.addProperty("url", this.getCardActionUrl()); + } + if (StringUtils.isNotBlank(this.getCardActionAppid())) { + cardAction.addProperty("appid", this.getCardActionAppid()); + } + if (StringUtils.isNotBlank(this.getCardActionPagepath())) { + cardAction.addProperty("pagepath", this.getCardActionPagepath()); + } + template.add("card_action", cardAction); + } + + List buttons = this.getButtons(); + if (null != buttons && !buttons.isEmpty()) { + JsonArray btnJsonArray = new JsonArray(); + for (TemplateCardButton btn : this.getButtons()) { + JsonObject tempObject = btn.toJson(); + btnJsonArray.add(tempObject); + } + template.add("button_list", btnJsonArray); + } + + // checkbox + if (StringUtils.isNotBlank(this.getCheckboxQuestionKey())) { + JsonObject checkBox = new JsonObject(); + checkBox.addProperty("question_key", this.getCheckboxQuestionKey()); + if (null != this.getCheckboxMode()) { + checkBox.addProperty("mode", this.getCheckboxMode()); + } + JsonArray optionArray = new JsonArray(); + for (CheckboxOption option : this.getOptions()) { + JsonObject tempObject = option.toJson(); + optionArray.add(tempObject); + } + checkBox.add("option_list", optionArray); + + template.add("checkbox", checkBox); + } + + // submit_button + if (StringUtils.isNotBlank(this.getSubmitButtonText()) || StringUtils.isNotBlank(this.getSubmitButtonKey())) { + JsonObject submit_button = new JsonObject(); + if (StringUtils.isNotBlank(this.getSubmitButtonText())) { + submit_button.addProperty("text", this.getSubmitButtonText()); + } + if (StringUtils.isNotBlank(this.getSubmitButtonKey())) { + submit_button.addProperty("key", this.getSubmitButtonKey()); + } + template.add("submit_button", submit_button); + } + + // select_list + List selects = this.getSelects(); + if (null != selects && !selects.isEmpty()) { + JsonArray selectJsonArray = new JsonArray(); + for (MultipleSelect select : this.getSelects()) { + JsonObject tempObject = select.toJson(); + selectJsonArray.add(tempObject); + } + template.add("select_list", selectJsonArray); + } + + QuoteArea quoteArea = this.getQuoteArea(); + if (null != quoteArea) { + JsonObject quoteAreaJson = quoteArea.toJson(); + template.add("quote_area", quoteAreaJson); + } + + messageJson.add("template_card", template); + break; + } default: } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java index b3feb7e4fd..a097f64146 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java @@ -481,6 +481,11 @@ public static class GroupRobotMsgType { */ public static final String FILE = "file"; + /** + * 模版类型消息. + */ + public static final String TEMPLATE_CARD = "template_card"; + } /**