Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发
在数字化营销与内容传播日益重要的今天,微信公众号已成为企业和个人进行信息发布与推广的重要平台。对于开发者而言,通过代码实现自动化的文章管理操作,如上传文章到草稿箱、群发文章等,能够极大提高工作效率。本文将详细介绍如何使用 Spring Boot 框架结合 WxJava 开发工具包,实现文章上传到微信公众号草稿箱以及群发功能。
JDK:建议使用 JDK 1.8 及以上版本,以确保与 Spring Boot 和 WxJava 的兼容性。
IDE:推荐使用 IntelliJ IDEA 或 Eclipse,两者都对 Spring Boot 项目有良好的支持。
Maven:用于项目依赖管理和构建,版本建议在 3.6 以上。
在开始开发前,需要提前准备好微信公众号的相关信息:
公众号 AppID:用于唯一标识你的公众号,在微信公众平台后台的 “开发 - 基本配置” 中获取。
公众号 AppSecret:与 AppID 配合使用,用于获取接口调用凭证(access_token),同样在 “开发 - 基本配置” 中查看。
IP 白名单设置:为保证接口调用的安全性,需要将服务器的 IP 地址添加到微信公众平台后台的 “开发 - 基本配置 - IP 白名单” 中。
可以通过 Spring Initializr(https://start.spring.io/)快速创建一个 Spring Boot 项目,在创建过程中选择以下依赖:
Spring Web:用于构建 Web 应用,方便后续编写接口进行功能测试。
Lombok:简化 Java 代码,通过注解自动生成 getter、setter、构造函数等。
在项目的pom.xml文件中添加 WxJava 相关依赖:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version> <!-- or latest version -->
</dependency>
WxJava 是一个优秀的微信开发 Java 工具包,weixin-java-mp模块专门用于微信公众号开发。然后这里发现还引入了jsoup,这肯定是大有用处,可接着看后续的内容。
在 Spring Boot 项目中,创建一个配置类用于加载微信公众号的 AppID 和 AppSecret 等信息。例如,创建WeChatConfig.java:
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WeChatConfig {
@Value("${wechat.mp.appId}")
private String appId;
@Value("${wechat.mp.appSecret}")
private String appSecret;
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public me.chanjar.weixin.mp.config.WxMpConfigStorage wxMpConfigStorage() {
me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl wxMpConfigStorage = new me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl();
wxMpConfigStorage.setAppId(appId);
wxMpConfigStorage.setSecret(appSecret);
return wxMpConfigStorage;
}
}
同时,在application.properties
或application.yml
文件中添加微信公众号的配置信息:
wechat.mp.appId=your_app_id
wechat.mp.appSecret=your_app_secret
将your_app_id和your_app_secret替换为实际获取到的公众号 AppID 和 AppSecret。
创建一个 Java 类用于表示微信公众号文章,例如WeChatArticle.java:
import lombok.Data;
@Data
public class WeChatArticle {
private String title;
private String content;
private String author;
// 其他文章属性,如封面图片URL等可根据需求添加
}
该类定义了文章的基本属性,如标题、内容和作者,可根据实际需求扩展更多属性。
创建一个服务类,如WeChatArticleService.java,用于实现上传文章到草稿箱的逻辑:
这里要特别注意,由于微信有些html标签不支持,所以需要将文章内容复制到专门的微信markdown编辑器中格式化后,在从编辑器复制出html出来,然后在通过代码上传。
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMpMassArticle;
import me.chanjar.weixin.mp.bean.material.WxMpMassArticleArticle;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUploadResult;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class WeChatArticleService {
@Resource
private WxMpService wxMpService;
public String addDraft(String title, String content, String thumbnailUrl) throws WxErrorException {
// 提取图片URL并替换
List<String> imageUrls = extractImageUrls(content);
for (String imgUrl : imageUrls) {
String wechatUrl = uploadImage(imgUrl);
content = content.replace(imgUrl, wechatUrl);
}
WxMpDraftArticles article = new WxMpDraftArticles();
article.setAuthor("拾壹");
article.setContent(content);
article.setTitle(title);
article.setThumbMediaId(mediaUploadInImage(thumbnailUrl));
// 可根据需求设置更多文章属性,如封面图片等
WxMpAddDraft news = new WxMpAddDraft();
news.setArticles(Collections.singletonList(article));
return wxMpService.getDraftService().addDraft(news);
}
/**
* 根据url上传永久素材
* @param url 图片url
* @return 微信素材ID
*/
public String mediaUploadInImage(String url) throws IOException, WxErrorException {
byte[] bytes = this.downloadImage(url);
File file = convert(bytes);
String mediaId = materialFileUpload("image",file);
FileUtil.del(file);
return mediaId;
}
/**
* 根据文件上传永久素材
* @param type 文件类型
* @param file 文件
* @return 微信素材ID
*/
public String materialFileUpload(String type,File file) throws IOException, WxErrorException {
WxMpMaterial wxMpMaterial = new WxMpMaterial();
wxMpMaterial.setFile(file);
WxMpMaterialUploadResult image = wxMpService.getMaterialService().materialFileUpload(type,wxMpMaterial);
return image.getMediaId();
}
/**
* 根据url上传图片
* @param url 图片url
* @return 微信可访问的url地址
*/
public String uploadImage(String url) throws IOException, WxErrorException {
byte[] bytes = this.downloadImage(url);
//bytes转成file
File file = convert(bytes);
WxMediaImgUploadResult wxMediaImgUploadResult = wxMpService.getMaterialService().mediaImgUpload(file);
FileUtil.del(file);
return wxMediaImgUploadResult.getUrl();
}
/**
* byte[] 转 File
*
* @param bytes
* @return
* @throws IOException
*/
public static File convert(byte[] bytes) throws IOException {
// 创建一个临时文件
File tempFile = File.createTempFile("temp-", ".png");
// 使用 FileOutputStream 将字节数组写入文件
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
fos.write(bytes);
}
return tempFile;
}
/**
* 下载图片
*
* @param url
* @return
*/
private byte[] downloadImage(String url) {
return new RestTemplate().getForObject(url, byte[].class);
}
/**
* 提取 HTML 中的图片 URL
*/
private List<String> extractImageUrls(String html) {
org.jsoup.nodes.Document doc = Jsoup.parse(html);
return doc.select("img").stream()
.map(img -> img.attr("src"))
.collect(Collectors.toList());
}
}
上述代码将WeChatArticle对象转换为 WxJava 中的文章对象格式,并调用WxMpService的接口方法将文章上传到草稿箱,返回文章的 mediaId,后续群发文章时会用到该 mediaId。
然后就是因为公众号不能用其他外链的图片,然后我们的图片基本都是已经上传好了的外链地址,所以需要先将图片上传到公众号,然后再替换成返回的公众号图片url地址。
在WeChatArticleService.java中继续添加文章群发的方法:
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMpMassArticle;
import me.chanjar.weixin.mp.bean.material.WxMpMassArticleArticle;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUploadResult;
import me.chanjar.weixin.mp.bean.message.WxMpMassMessage;
import me.chanjar.weixin.mp.bean.message.WxMpMassMessageResult;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class WeChatArticleService {
public void wechatSendAll(String mediaId) {
try {
WxMpDraftInfo draft = wxMpService.getDraftService().getDraft(mediaId);
if (draft == null || draft.getNewsItem() == null || draft.getNewsItem().isEmpty()) {
throw new ServiceException("无效的mediaId: " + mediaId);
}
WxMpMassTagMessage wxMpMassTagMessage = new WxMpMassTagMessage();
wxMpMassTagMessage.setMediaId(mediaId);
wxMpMassTagMessage.setMsgType("mpnews");
wxMpMassTagMessage.setSendAll(true);
wxMpMassTagMessage.setSendIgnoreReprint(true);
wxMpService.getMassMessageService().massGroupMessageSend(wxMpMassTagMessage);
} catch (WxErrorException e) {
log.error("微信群发失败", e);
throw new RuntimeException(e);
}
}
}
该方法通过传入文章的 mediaId
,构建群发消息对象,并调用WxMpService
的群发接口将文章发送给公众号的关注用户。
通过上述步骤,我们成功实现了使用 Spring Boot 和 WxJava 将文章上传到微信公众号草稿箱以及群发的功能。在实际开发过程中,还需要注意以下几点:
接口调用频率限制:微信公众号接口对调用频率有一定限制,需合理控制接口调用次数,避免因频率过高导致调用失败。
错误处理:代码中对WxErrorException进行了简单抛出,实际应用中应根据具体错误码进行更详细的错误处理和日志记录。
内容审核:群发的文章内容需符合微信公众平台的相关规定,否则可能导致文章发送失败或公众号受到处罚。
希望本文能帮助你快速掌握使用 Spring Boot 和 WxJava 进行微信公众号文章管理开发的技能,进一步拓展公众号的自动化运营能力。
上述文章涵盖了完整的实现流程与要点。你可以说说对文章篇幅、某些技术细节讲解的看法,若有特殊需求,我可进一步修改。