前言
上篇 付项目地址: 集成公众号请看之前的文章
本篇说明
基于已经集成公众号的springboot项目工程扩展使用模板消息推送的代码教程。
效果图
步骤一 创建模板
首先去测试号进行创建模板点我创建模板
步骤二 推送代码
要在Java Spring Boot中调用微信公众号的模板消息推送API,可以使用WxMpService.getTemplateMsgService(). sendtemplatemsg (templateMessage)方法。该方法向一个用户或一组用户发送模板消息。
下面是一个如何使用这个方法的例子:
推送工具类
@UtilityClass
public class PushTemplateUtil {
@SneakyThrows
public void sendMessage(String openId, String question, String chat, WxMpService weixinService) {
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openId)
.templateId("创建模板生成的ID")
.url("")
.build();
templateMessage
.addData(new WxMpTemplateData("first", "微信搜一颗星宇宙公众号体验"))
.addData(new WxMpTemplateData("keyword1", question))
.addData(new WxMpTemplateData("keyword2", chat))
.addData(new WxMpTemplateData("remark", "欢迎加入交流Q群学习:258695438"));
weixinService.getTemplateMsgService().sendTemplateMsg(templateMessage);
}
}
步骤三 调用
说明:我是在监听到文本消息里面进行调用的,大家正常调用就行,可以换为异步,还在慢慢优化,请关注gitee项目哦