微信公眾號發(fā)送消息模板(java)

這段時(shí)間接觸公眾號開發(fā),寫下向用戶發(fā)送消息模板的接口調(diào)用

先上接口代碼

如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。

1? public static JSONObject sendModelMessage(ServletContext context,JSONObject jsonMsg) {

2? ? ? ? System.out.println("消息內(nèi)容:"+jsonMsg);

3? ? ? ? boolean result = false;

4? ? ? ? try {

5? ? ? ? ? ? getWX_AccessToken(context);

6? ? ? ? } catch (Exception e) {

7? ? ? ? ? ? // TODO Auto-generated catch block

8? ? ? ? ? ? e.printStackTrace();

9? ? ? ? }

10? ? ? ? // 拼接請求地址

11? ? ? ? String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";

12? ? ? ? requestUrl = requestUrl.replace("ACCESS_TOKEN", context.getAttribute(ContextTokenName).toString());

1314? ? ? ? // 發(fā)送客服消息

15? ? ? ? JSONObject jsonObject = getJsonByWX(requestUrl, context, "POST",jsonMsg, false);

16?

17? ? ? ? if (null != jsonObject) {

18? ? ? ? ? ? int errorCode = jsonObject.getInt("errcode");

19? ? ? ? ? ? String errorMsg = jsonObject.getString("errmsg");

20? ? ? ? ? ? if (0 == errorCode) {

21? ? ? ? ? ? ? ? result = true;

22? ? ? ? ? ? ? ? System.out.println("模板消息發(fā)送成功 errcode:{} "+errorCode+"----"+errorMsg);

23? ? ? ? ? ? } else {

24? ? ? ? ? ? ? ? System.out.println("模板消息發(fā)送失敗 errcode:{} "+errorCode+"----"+errorMsg);

25? ? ? ? ? ? }

26? ? ? ? }

27?

28? ? ? ? return null;

29? ? }

15行那段getJsonByWX是統(tǒng)一調(diào)用微信接口的方法,每個(gè)項(xiàng)目都有自己的調(diào)用方法,我這里就不貼了。接口調(diào)用鏈接:

https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

接下來就是建個(gè)bean類,里面寫入一下顏色及值

1    private String value;

2? ? private String color;

3? ?

4? ? public String getValue() {

5? ? ? ? return value;

6? ? }

7?

8? ? public void setValue(String value) {

9? ? ? ? this.value = value;

10? ? }

11?

12? ? public String getColor() {

13? ? ? ? return color;

14? ? }

15?

16? ? public void setColor(String color) {

17? ? ? ? this.color = color;

18? ? }

在公眾號里填寫模板消息的對應(yīng)格式


如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。

之后就是有個(gè)觸發(fā)點(diǎn),我選擇發(fā)貨后把發(fā)貨信息發(fā)送給用戶

    PageData wechatTemplate = new PageData();

? ? ? ? wechatTemplate.put("template_id", "填寫你的模板id");

? ? ? ? wechatTemplate.put("touser", userInfo.get("openid"));//獲取用戶的openid


? ? ? ? Map<String,TemplateMessageUtil> mapdata = new HashMap<>();


? ? ? ? TemplateMessageUtil first? = new TemplateMessageUtil();? ? ? ?

? ? ? ? first.setColor("#173177");

? ? ? ? first.setValue("發(fā)貨通知");

? ? ? ? mapdata.put("first", first);


? ? ? ? TemplateMessageUtil text1? = new TemplateMessageUtil();? ?

? ? ? ? text1.setColor("#173177");

? ? ? ? text1.setValue("您好,您所購買的商品已發(fā)貨。");

? ? ? ? mapdata.put("text1", text1);


? ? ? ? TemplateMessageUtil text2? = new TemplateMessageUtil();? ?

? ? ? ? text2.setColor("#173177");

? ? ? ? text2.setValue(expresser_name);

? ? ? ? mapdata.put("text2", text2);


? ? ? ? TemplateMessageUtil text3? = new TemplateMessageUtil();? ?

? ? ? ? text3.setColor("#173177");

? ? ? ? text3.setValue(expresser_phone);

? ? ? ? mapdata.put("text3", text3);


? ? ? ? TemplateMessageUtil remark = new TemplateMessageUtil();? ? ? ?

? ? ? ? remark.setColor("#173177");

? ? ? ? remark.setValue("請保持電話暢通>>");

? ? ? ? mapdata.put("remark", remark);

? ? ? ? JSONObject json = new JSONObject();?

? ? ? ? json.put("data",mapdata);

? ? ? ? json.putAll(wechatTemplate);//轉(zhuǎn)為json


? ? ? ? WXInterface.sendModelMessage(context,json);

之后手機(jī)就會收到信息了


整體思路是這樣,也是參照百度而來,因?yàn)槊總€(gè)人的項(xiàng)目里方法都不一樣,我就不詳細(xì)貼上,既然做到發(fā)送模板消息了,統(tǒng)一調(diào)用微信接口的方法應(yīng)每個(gè)人該也早寫在工具類里了,每個(gè)人都不同,當(dāng)應(yīng)該都有,調(diào)用這個(gè)方法,把微信模板消息連接的條件access_token寫進(jìn)去就請求了,剩下的就是傳入你要發(fā)送的消息,消息存入集合,集合轉(zhuǎn)json才行,JSONObject類相信也都有,我也不貼了,每個(gè)人項(xiàng)目都不一樣,沒必要照搬過去,就照著自己原先已有的類改進(jìn)。

如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容