Skip to content

Commit 6e96d7c

Browse files
authored
🎨 【企业微信】添加AttachmentBuilder,可以更方便的创建新客户欢迎语的附件信息
1 parent 722c2cf commit 6e96d7c

File tree

9 files changed

+95
-6
lines changed

9 files changed

+95
-6
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Attachment.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,48 +34,53 @@ public class Attachment implements Serializable {
3434
*
3535
* @param image the image
3636
*/
37-
public void setImage(Image image) {
37+
public Attachment setImage(Image image) {
3838
this.image = image;
3939
this.msgType = WxCpConsts.WelcomeMsgType.IMAGE;
40+
return this;
4041
}
4142

4243
/**
4344
* Sets link.
4445
*
4546
* @param link the link
4647
*/
47-
public void setLink(Link link) {
48+
public Attachment setLink(Link link) {
4849
this.link = link;
4950
this.msgType = WxCpConsts.WelcomeMsgType.LINK;
51+
return this;
5052
}
5153

5254
/**
5355
* Sets mini program.
5456
*
5557
* @param miniProgram the mini program
5658
*/
57-
public void setMiniProgram(MiniProgram miniProgram) {
59+
public Attachment setMiniProgram(MiniProgram miniProgram) {
5860
this.miniProgram = miniProgram;
5961
this.msgType = WxCpConsts.WelcomeMsgType.MINIPROGRAM;
62+
return this;
6063
}
6164

6265
/**
6366
* Sets video.
6467
*
6568
* @param video the video
6669
*/
67-
public void setVideo(Video video) {
70+
public Attachment setVideo(Video video) {
6871
this.video = video;
6972
this.msgType = WxCpConsts.WelcomeMsgType.VIDEO;
73+
return this;
7074
}
7175

7276
/**
7377
* Sets file.
7478
*
7579
* @param file the file
7680
*/
77-
public void setFile(File file) {
81+
public Attachment setFile(File file) {
7882
this.file = file;
7983
this.msgType = WxCpConsts.WelcomeMsgType.FILE;
84+
return this;
8085
}
8186
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package me.chanjar.weixin.cp.bean.external.msg;
2+
3+
import lombok.Builder;
4+
5+
/**
6+
* @author codecrab
7+
*/
8+
public class AttachmentBuilder {
9+
10+
@Builder(builderClassName = "ImageBuilder", builderMethodName = "imageBuilder")
11+
private static Attachment image(String mediaId, String picUrl) {
12+
Image image = new Image().setMediaId(mediaId).setPicUrl(picUrl);
13+
return new Attachment().setImage(image);
14+
}
15+
16+
@Builder(builderClassName = "VideoBuilder", builderMethodName = "videoBuilder")
17+
private static Attachment video(String mediaId) {
18+
Video video = new Video().setMediaId(mediaId);
19+
return new Attachment().setVideo(video);
20+
}
21+
22+
@Builder(builderClassName = "FileBuilder", builderMethodName = "fileBuilder")
23+
private static Attachment file(String mediaId) {
24+
File file = new File().setMediaId(mediaId);
25+
return new Attachment().setFile(file);
26+
}
27+
28+
@Builder(builderClassName = "LinkBuilder", builderMethodName = "linkBuilder")
29+
private static Attachment link(String title, String url, String picUrl, String desc) {
30+
Link link = new Link().setTitle(title).setPicUrl(picUrl).setUrl(url).setDesc(desc);
31+
return new Attachment().setLink(link);
32+
}
33+
34+
@Builder(builderClassName = "MiniProgramBuilder", builderMethodName = "miniProgramBuilder")
35+
private static Attachment miniProgram(String title, String picMediaId, String appId, String page) {
36+
MiniProgram miniProgram = new MiniProgram().setTitle(title).setPicMediaId(picMediaId).setAppid(appId).setPage(page);
37+
return new Attachment().setMiniProgram(miniProgram);
38+
}
39+
40+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/File.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.annotations.SerializedName;
44
import lombok.Data;
5+
import lombok.experimental.Accessors;
56

67
import java.io.Serializable;
78

@@ -11,6 +12,7 @@
1112
* @author <a href="https://github.com/binarywang">Binary Wang</a> created on 2021-08-23
1213
*/
1314
@Data
15+
@Accessors(chain = true)
1416
public class File implements Serializable {
1517
private static final long serialVersionUID = 2794189478198329090L;
1618

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Image.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.annotations.SerializedName;
44
import lombok.Data;
5+
import lombok.experimental.Accessors;
56

67
import java.io.Serializable;
78

@@ -11,6 +12,7 @@
1112
* @author <a href="https://github.com/binarywang">Binary Wang</a> created on 2020-08-16
1213
*/
1314
@Data
15+
@Accessors(chain = true)
1416
public class Image implements Serializable {
1517
private static final long serialVersionUID = -606286372867787121L;
1618

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Link.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.annotations.SerializedName;
44
import lombok.Data;
5+
import lombok.experimental.Accessors;
56

67
import java.io.Serializable;
78

@@ -11,6 +12,7 @@
1112
* @author <a href="https://github.com/binarywang">Binary Wang</a> created on 2020-08-16
1213
*/
1314
@Data
15+
@Accessors(chain = true)
1416
public class Link implements Serializable {
1517
private static final long serialVersionUID = -8041816740881163875L;
1618
private String title;

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Location.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package me.chanjar.weixin.cp.bean.external.msg;
22

33
import lombok.Data;
4+
import lombok.experimental.Accessors;
45

56
/**
67
* 地理位置
78
*
89
* @author leiin created on 2021-10-29
910
*/
1011
@Data
12+
@Accessors(chain = true)
1113
public class Location {
1214
private String latitude;
1315
private String longitude;

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/MiniProgram.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.annotations.SerializedName;
44
import lombok.Data;
5+
import lombok.experimental.Accessors;
56

67
import java.io.Serializable;
78

@@ -11,6 +12,7 @@
1112
* @author <a href="https://github.com/binarywang">Binary Wang</a> created on 2020-08-16
1213
*/
1314
@Data
15+
@Accessors(chain = true)
1416
public class MiniProgram implements Serializable {
1517
private static final long serialVersionUID = 4242074162638170679L;
1618

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/msg/Video.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.annotations.SerializedName;
44
import lombok.Data;
5+
import lombok.experimental.Accessors;
56

67
import java.io.Serializable;
78

@@ -11,6 +12,7 @@
1112
* @author pg created on 2021-6-21
1213
*/
1314
@Data
15+
@Accessors(chain = true)
1416
public class Video implements Serializable {
1517
private static final long serialVersionUID = -6048642921382867138L;
1618
@SerializedName("media_id")

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactBatchInfo;
1111
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
1212
import me.chanjar.weixin.cp.bean.external.msg.Attachment;
13+
import me.chanjar.weixin.cp.bean.external.msg.AttachmentBuilder;
1314
import me.chanjar.weixin.cp.bean.external.msg.Image;
1415
import me.chanjar.weixin.cp.bean.external.msg.Video;
1516
import org.apache.commons.lang3.time.DateFormatUtils;
1617
import org.testng.annotations.Guice;
1718
import org.testng.annotations.Test;
1819
import org.testng.collections.CollectionUtils;
1920

20-
import java.util.*;
21+
import java.util.ArrayList;
22+
import java.util.Collections;
23+
import java.util.Date;
24+
import java.util.List;
2125

2226
import static org.testng.Assert.assertNotNull;
2327

@@ -419,6 +423,7 @@ public void testOnjobTransferGroupChat() throws WxErrorException {
419423
System.out.println(result);
420424
assertNotNull(result);
421425
}
426+
422427
/**
423428
* Test get user behavior statistic.
424429
*/
@@ -466,6 +471,33 @@ public void testSendWelcomeMsg() throws WxErrorException {
466471
.build());
467472
}
468473

474+
/**
475+
* Test send welcome msg. use AttachmentBuilder
476+
*
477+
* @throws WxErrorException the wx error exception
478+
*/
479+
@Test
480+
public void testSendWelcomeMsg2() throws WxErrorException {
481+
482+
Attachment imageAttachment = AttachmentBuilder.imageBuilder().mediaId("123123").build();
483+
Attachment videoAttachment = AttachmentBuilder.videoBuilder().mediaId("video_media_id").build();
484+
Attachment miniProgramAttachment = AttachmentBuilder.miniProgramBuilder()
485+
.title("title")
486+
.picMediaId("123123123")
487+
.appId("wxcxxxxxxxxxxx")
488+
.page("https://")
489+
.build();
490+
491+
List<Attachment> attachments = new ArrayList<>();
492+
attachments.add(imageAttachment);
493+
attachments.add(videoAttachment);
494+
attachments.add(miniProgramAttachment);
495+
this.wxCpService.getExternalContactService().sendWelcomeMsg(WxCpWelcomeMsg.builder()
496+
.welcomeCode("abc")
497+
.attachments(attachments)
498+
.build());
499+
}
500+
469501
/**
470502
* Test update remark.
471503
*

0 commit comments

Comments
 (0)