微信開發系列 — — 微信模板訊息
阿新 • • 發佈:2020-12-22
前言:
微信系列的開發,網上已經有一些很不錯的工具支援了,幫助我們快速開發微信應用。今天介紹一個藉助微信工具快速開發微信模板訊息。
首先我們需要引入一些jar包:
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>2.9.8.BETA</version> </dependency>
開始開發:
第一步:
準備一個配置檔案類。用來配置需要用到的APPID,TEMPLATEID等
package com.wen.demo.config; public interface WeChatConfig { /** * 測試號或公眾號ID */ String APPID=""; /** * 測試號或公眾號密碼 */ String SECRET=""; /** * 傳送到哪個使用者的ID */ String USERID=""; /** * 模板訊息ID */ String TEMPLATEID=""; }
以上加入自己的各種ID即可隨時使用
第二步:
準備一個bean,主要是例項化,配置相關的APPID,secret等。
package com.wen.demo.config; import me.chanjar.weixin.mp.api.WxMpConfigStorage; import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @Component public class WechatMpConfig { /** * 微信服務裝載 * @return */ @Bean public WxMpService wxMpService() { WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); return wxMpService; } /** * 配置溫馨的APPID和密碼 * @return */ @Bean public WxMpConfigStorage wxMpConfigStorage() { WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage(); wxMpConfigStorage.setAppId(WeChatConfig.APPID); wxMpConfigStorage.setSecret(WeChatConfig.SECRET); return wxMpConfigStorage; } }
第三步:
寫一個controller,來發送模板訊息
package com.wen.demo.controller; import com.wen.demo.config.WeChatConfig; import me.chanjar.weixin.common.exception.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.RestController; import java.util.Arrays; import java.util.List; @RestController public class SendMessageByWX { @Autowired private WxMpService wxMpService; /** * 傳送模板訊息控制器 * @return */ @GetMapping(value = "send") public String send() { //例項化模板物件 WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage(); //設定模板ID wxMpTemplateMessage.setTemplateId(WeChatConfig.TEMPLATEID); //設定傳送給哪個使用者 wxMpTemplateMessage.setToUser(WeChatConfig.USERID); //構建訊息格式 List<WxMpTemplateData> list= Arrays.asList( new WxMpTemplateData("first","wen"), new WxMpTemplateData("keyword1","wen"), new WxMpTemplateData("keyword2","wen"), new WxMpTemplateData("keyword3","wen"), new WxMpTemplateData("keyword4","wen"), new WxMpTemplateData("remark","wen") ); //放進模板物件。準備傳送 wxMpTemplateMessage.setData(list); try { //傳送模板 wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage); } catch (WxErrorException e) { e.printStackTrace(); } return "傳送成功"; } }
第四步:
就是開始測試啦,啟動伺服器。直接訪問
開啟你的測試號,基本就可以了。。不過注意一點是。測試號需要配置相關的東西,而且需要和你的傳送的欄位名一樣,否則會出錯。如果不好測試號,請看我的第一篇文章,網頁授權。
總結:
好好學習,不然怎麼泡妞是吧~!