200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 【微信公众号】8 SpringBoot整合WxJava发送模板消息

【微信公众号】8 SpringBoot整合WxJava发送模板消息

时间:2020-05-01 11:21:46

相关推荐

【微信公众号】8 SpringBoot整合WxJava发送模板消息

模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等。不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息。

1、主动发送消息

1、在微信公众号中我们多数都是被动回复用户消息,在文章:【微信开发】4、SpringBoot整合WxJava处理微信消息、事件推送 中我们可以收到用户的消息、事件推送,从而被动回复用户信息。2、微信公众号中能主动给用户发送消息的方式有以下几种:

1、模板消息,每个账号的模板消息的日调用上限为10万次

2、群发信息,服务号提供每月(自然月)4条的群发权限。而对于某些具备开发能力的公众号运营者,可以通过高级群发接口,实现更灵活的群发能力

3、客服信息,在用户点击自定义菜单、关注公众号、扫描二维码等三个场景中,开发者在收到事件通知后1分钟内可调用客服消息接口向用户下发3条客服消息

各有各的好处和限制,本次我们使用模板消息主动给用户发送消息

2、创建消息模板

1、正式号

在网页最下端找到:

点击进入到功能,添加【模板消息】功能,如图:

我已经添加了,点击进入:

可以到模板库中添加你需要的模板,没找到合适的需要提交新增申请

2、测试号

在模板消息接口处,可以直接新增消息模板,这里的消息模板只能用于测试号,正式号是无法使用的:

消息模板格式参考:

参数需以{{开头,以.DATA}}结尾,实际使用中,我们替换对应的参数即可

配置成功后,我们需要拿到模板ID,我们发送模板消息就需要模板ID

3、整合WxJava发送模板消息

import me.mon.error.WxErrorException;import me.chanjar.weixin.mp.api.WxMpService;import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** 微信公众号模板消息*/@RestController@RequestMapping("wx/mp/template")public class WxMpTemplateController {@Autowiredprivate WxMpService wxMpService;/*** 发送模板消息** @return* @throws WxErrorException*/@GetMapping("sendMsg")public String sendMsg() throws WxErrorException {WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();// 模板IDwxMpTemplateMessage.setTemplateId("qnV7iJ0Lr4lnhkPI0loUfSh0LoBOr9la0BST12XSG74");// 接收用户openIdwxMpTemplateMessage.setToUser("o_Zxu6Zh8UigOVTZ3pqx9C3PUWXo");// 可配置跳转指链接或小程序,不配置默认不跳转wxMpTemplateMessage.setUrl("");// 消息内容WxMpTemplateData wxMpTemplateData = new WxMpTemplateData("first", "下单成功提醒");wxMpTemplateMessage.addData(wxMpTemplateData);wxMpTemplateData = new WxMpTemplateData("keyword1", "iphone 13pro max 远峰蓝 256GB X 1");wxMpTemplateMessage.addData(wxMpTemplateData);// 可以设置字体颜色wxMpTemplateData = new WxMpTemplateData("keyword2", "¥8799.00", "#ff0000");wxMpTemplateMessage.addData(wxMpTemplateData);wxMpTemplateData = new WxMpTemplateData("keyword3", "-03-23 13:49:00");wxMpTemplateMessage.addData(wxMpTemplateData);wxMpTemplateData = new WxMpTemplateData("keyword4", "京东快递");wxMpTemplateMessage.addData(wxMpTemplateData);wxMpTemplateData = new WxMpTemplateData("keyword5", "贵阳市观山湖区金融城1号楼");wxMpTemplateMessage.addData(wxMpTemplateData);wxMpTemplateData = new WxMpTemplateData("remark", "");wxMpTemplateMessage.addData(wxMpTemplateData);// 发送模板消息String msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);return "模板消息发送成功,msgId:" + msgId;}}

1、核心思想就是使用你需要发送的内容替换消息模板中的{{}}内的内容,2、还可以单独设置字体的颜色3、可以使模板点击跳转到响应的网页

4、测试发送模板消息

1、访问接口

localhost:8080/wx/mp/template/sendMsg

2、打开微信

如图,我们收到了刚刚发送的模板消息,点击后,跳转到【京东】网页:

如您在阅读中发现不足,欢迎留言!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。