diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpWelcomeMsg.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpWelcomeMsg.java index ce744b9f24..ade49684ce 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpWelcomeMsg.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpWelcomeMsg.java @@ -2,13 +2,11 @@ import com.google.gson.annotations.SerializedName; import lombok.*; -import me.chanjar.weixin.cp.bean.external.msg.Image; -import me.chanjar.weixin.cp.bean.external.msg.Link; -import me.chanjar.weixin.cp.bean.external.msg.MiniProgram; -import me.chanjar.weixin.cp.bean.external.msg.Text; +import me.chanjar.weixin.cp.bean.external.msg.*; import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; import java.io.Serializable; +import java.util.List; /** * 新客户欢迎语. @@ -28,11 +26,7 @@ public class WxCpWelcomeMsg implements Serializable { private Text text; - private Image image; - - private Link link; - - private MiniProgram miniprogram; + private List attachments; public String toJson() { return WxCpGsonBuilder.create().toJson(this); diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Attachment.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Attachment.java new file mode 100644 index 0000000000..0c64b9bf63 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Attachment.java @@ -0,0 +1,76 @@ +package me.chanjar.weixin.cp.bean.external.msg; + +import com.google.gson.annotations.SerializedName; +import me.chanjar.weixin.cp.constant.WxCpConsts; + +import java.io.Serializable; + +public class Attachment implements Serializable { + private static final long serialVersionUID = -8078748379570640198L; + + @SerializedName("msgtype") + private String msgType; + + private Image image; + + private Link link; + + private MiniProgram miniprogram; + + private Video video; + + @Override + public String toString() { + return "Attachment{" + + "msgType='" + msgType + '\'' + + ", image=" + image + + ", link=" + link + + ", miniprogram=" + miniprogram + + ", video=" + video + + '}'; + } + + private String getMsgType() { + return msgType; + } + + private void setMsgType(String msgType) { + this.msgType = msgType; + } + + public Image getImage() { + return image; + } + + public void setImage(Image image) { + this.image = image; + this.msgType = WxCpConsts.WelcomeMsgType.IMAGE; + } + + public Link getLink() { + return link; + } + + public void setLink(Link link) { + this.link = link; + this.msgType = WxCpConsts.WelcomeMsgType.LINK; + } + + public MiniProgram getMiniprogram() { + return miniprogram; + } + + public void setMiniprogram(MiniProgram miniprogram) { + this.miniprogram = miniprogram; + this.msgType = WxCpConsts.WelcomeMsgType.MINIPROGRAM; + } + + public Video getVideo() { + return video; + } + + public void setVideo(Video video) { + this.video = video; + this.msgType = WxCpConsts.WelcomeMsgType.VIDEO; + } +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Video.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Video.java new file mode 100644 index 0000000000..237fb75cfe --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Video.java @@ -0,0 +1,19 @@ +package me.chanjar.weixin.cp.bean.external.msg; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +import java.io.Serializable; + +/** + * 视频消息 + * + * @author pg + * @date 2021-6-21 + */ +@Data +public class Video implements Serializable { + private static final long serialVersionUID = -6048642921382867138L; + @SerializedName("media_id") + private String mediaId; +} 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 4a41fa8f71..69db78efbe 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 @@ -334,4 +334,24 @@ public static class WorkBenchType { * */ public static final String WEBVIEW = "webview"; } + + @UtilityClass + public static class WelcomeMsgType { + /** + * 图片消息. + */ + public static final String IMAGE = "image"; + /** + * 图文消息. + */ + public static final String LINK = "link"; + /** + * 视频消息. + */ + public static final String VIDEO = "video"; + /** + * 小程序消息. + */ + public static final String MINIPROGRAM = "miniprogram"; + } } diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java index 29089d478d..4b6221d175 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java @@ -8,6 +8,9 @@ import me.chanjar.weixin.cp.bean.WxCpBaseResp; import me.chanjar.weixin.cp.bean.external.*; import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo; +import me.chanjar.weixin.cp.bean.external.msg.Attachment; +import me.chanjar.weixin.cp.bean.external.msg.Image; +import me.chanjar.weixin.cp.bean.external.msg.Video; import org.apache.commons.lang3.time.DateFormatUtils; import org.testng.annotations.Guice; import org.testng.annotations.Test; @@ -214,8 +217,22 @@ public void testAddMsgTemplate() { @Test public void testSendWelcomeMsg() throws WxErrorException { + Image image = new Image(); + image.setMediaId("123123"); + Attachment attachment = new Attachment(); + attachment.setImage(image); + + Video video = new Video(); + video.setMediaId("video_media_id"); + Attachment attachment2 = new Attachment(); + attachment2.setVideo(video); + + List attachments = new ArrayList<>(); + attachments.add(attachment); + attachments.add(attachment2); this.wxCpService.getExternalContactService().sendWelcomeMsg(WxCpWelcomeMsg.builder() .welcomeCode("abc") + .attachments(attachments) .build()); }