Skip to content

Commit

Permalink
🎨 【企业微信】添加AttachmentBuilder,可以更方便的创建新客户欢迎语的附件信息
Browse files Browse the repository at this point in the history
  • Loading branch information
Coco-king authored Mar 29, 2023
1 parent 722c2cf commit 6e96d7c
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,53 @@ public class Attachment implements Serializable {
*
* @param image the image
*/
public void setImage(Image image) {
public Attachment setImage(Image image) {
this.image = image;
this.msgType = WxCpConsts.WelcomeMsgType.IMAGE;
return this;
}

/**
* Sets link.
*
* @param link the link
*/
public void setLink(Link link) {
public Attachment setLink(Link link) {
this.link = link;
this.msgType = WxCpConsts.WelcomeMsgType.LINK;
return this;
}

/**
* Sets mini program.
*
* @param miniProgram the mini program
*/
public void setMiniProgram(MiniProgram miniProgram) {
public Attachment setMiniProgram(MiniProgram miniProgram) {
this.miniProgram = miniProgram;
this.msgType = WxCpConsts.WelcomeMsgType.MINIPROGRAM;
return this;
}

/**
* Sets video.
*
* @param video the video
*/
public void setVideo(Video video) {
public Attachment setVideo(Video video) {
this.video = video;
this.msgType = WxCpConsts.WelcomeMsgType.VIDEO;
return this;
}

/**
* Sets file.
*
* @param file the file
*/
public void setFile(File file) {
public Attachment setFile(File file) {
this.file = file;
this.msgType = WxCpConsts.WelcomeMsgType.FILE;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.chanjar.weixin.cp.bean.external.msg;

import lombok.Builder;

/**
* @author codecrab
*/
public class AttachmentBuilder {

@Builder(builderClassName = "ImageBuilder", builderMethodName = "imageBuilder")
private static Attachment image(String mediaId, String picUrl) {
Image image = new Image().setMediaId(mediaId).setPicUrl(picUrl);
return new Attachment().setImage(image);
}

@Builder(builderClassName = "VideoBuilder", builderMethodName = "videoBuilder")
private static Attachment video(String mediaId) {
Video video = new Video().setMediaId(mediaId);
return new Attachment().setVideo(video);
}

@Builder(builderClassName = "FileBuilder", builderMethodName = "fileBuilder")
private static Attachment file(String mediaId) {
File file = new File().setMediaId(mediaId);
return new Attachment().setFile(file);
}

@Builder(builderClassName = "LinkBuilder", builderMethodName = "linkBuilder")
private static Attachment link(String title, String url, String picUrl, String desc) {
Link link = new Link().setTitle(title).setPicUrl(picUrl).setUrl(url).setDesc(desc);
return new Attachment().setLink(link);
}

@Builder(builderClassName = "MiniProgramBuilder", builderMethodName = "miniProgramBuilder")
private static Attachment miniProgram(String title, String picMediaId, String appId, String page) {
MiniProgram miniProgram = new MiniProgram().setTitle(title).setPicMediaId(picMediaId).setAppid(appId).setPage(page);
return new Attachment().setMiniProgram(miniProgram);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

Expand All @@ -11,6 +12,7 @@
* @author <a href="https://github.com/binarywang">Binary Wang</a> created on 2020-08-16
*/
@Data
@Accessors(chain = true)
public class Link implements Serializable {
private static final long serialVersionUID = -8041816740881163875L;
private String title;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package me.chanjar.weixin.cp.bean.external.msg;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* 地理位置
*
* @author leiin created on 2021-10-29
*/
@Data
@Accessors(chain = true)
public class Location {
private String latitude;
private String longitude;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

Expand All @@ -11,6 +12,7 @@
* @author pg created on 2021-6-21
*/
@Data
@Accessors(chain = true)
public class Video implements Serializable {
private static final long serialVersionUID = -6048642921382867138L;
@SerializedName("media_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactBatchInfo;
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.AttachmentBuilder;
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;
import org.testng.collections.CollectionUtils;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static org.testng.Assert.assertNotNull;

Expand Down Expand Up @@ -419,6 +423,7 @@ public void testOnjobTransferGroupChat() throws WxErrorException {
System.out.println(result);
assertNotNull(result);
}

/**
* Test get user behavior statistic.
*/
Expand Down Expand Up @@ -466,6 +471,33 @@ public void testSendWelcomeMsg() throws WxErrorException {
.build());
}

/**
* Test send welcome msg. use AttachmentBuilder
*
* @throws WxErrorException the wx error exception
*/
@Test
public void testSendWelcomeMsg2() throws WxErrorException {

Attachment imageAttachment = AttachmentBuilder.imageBuilder().mediaId("123123").build();
Attachment videoAttachment = AttachmentBuilder.videoBuilder().mediaId("video_media_id").build();
Attachment miniProgramAttachment = AttachmentBuilder.miniProgramBuilder()
.title("title")
.picMediaId("123123123")
.appId("wxcxxxxxxxxxxx")
.page("https://")
.build();

List<Attachment> attachments = new ArrayList<>();
attachments.add(imageAttachment);
attachments.add(videoAttachment);
attachments.add(miniProgramAttachment);
this.wxCpService.getExternalContactService().sendWelcomeMsg(WxCpWelcomeMsg.builder()
.welcomeCode("abc")
.attachments(attachments)
.build());
}

/**
* Test update remark.
*
Expand Down

0 comments on commit 6e96d7c

Please sign in to comment.