微信公众号发送模板消息
生活随笔
收集整理的這篇文章主要介紹了
微信公众号发送模板消息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 1 開發中遇到的問題匯總
- 2 模板消息創建
- 3 調試接口
- 3.1 微信公眾號消息模板
- 3.1.1 基本信息
- 3.2 請求參數
- 3.2.1 Query參數及說明
- 3.2.2 body參數及說明
- 4 測試結果
- 5 工具類封裝
- 6 常見問題
1 開發中遇到的問題匯總
首先是在測試中,遇到最多的就是41003->appid錯誤,然后就是40165,說什么page和pagepath,其實就是線上和體驗的問題,反正不管怎么改發出去就行,跳轉到正確的頁面即可,但是大問題是:公眾號的文檔然后里面發送模板消息是小程序的,然后一直就是appid錯誤,然后突然發現還有個文檔,修改body后測試成功.按照我的方式是肯定成功的,因為有的博客都是從官方文檔抄一遍測都不測.
2 模板消息創建
3 調試接口
3.1 微信公眾號消息模板
3.1.1 基本信息
- 接口狀態: 開發中
- 接口URL: https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=49_pWR_MJBhGBi97IMi0pzXqkxphglh9YzKPs8Wu962QjtVeVL1MbnTLHTL11mUMPFrhJMjPTB6OGvmzcs9w1MJG8xQVbhFalmh5K7WtOHLR7pQDhupwLCKkQGrqsb8sy-OIAkDr2CzWtQVlBOIRTNdAGAEGA
- 請求方式: POST
- Content-Type: application/json
3.2 請求參數
3.2.1 Query參數及說明
| access_token | 49_pWR_MJBhGBi97IMi0pzXqkxphglh9YzKPs8Wu962QjtVeVL1MbnTLHTL11mUMPFrhJMjPTB6OGvmzcs9w1MJG8xQVbhFalmh5K7WtOHLR7pQDhupwLCKkQGrqsb8sy-OIAkDr2CzWtQVlBOIRTNdAGAEGA | 是 | 暫無描述 |
3.2.2 body參數及說明
{"touser": "oUtXT6JTkW8oeQbRb-wBe9CMucyU","template_id": "Eeh7tc4SiJ9W49QSyjTeuCgscRTjBq_DEmf1BNmq_VU","url": "http://weixin.qq.com/download","topcolor": "#FF0000","data": {"first": {"value": "恭喜你購買成功!","color": "#173177"},"keyword1": {"value": "巧克力","color": "#173177"},"keyword2": {"value": "39.8元","color": "#173177"},"keyword3": {"value": "2014年9月22日","color": "#173177"},"remark": {"value": "歡迎再次購買!","color": "#173177"}} }4 測試結果
其實開發過程中并不是很復雜,只是文檔有一些問題,容易歧義
5 工具類封裝
// 獲取tokenString token = saveAndFlushAccessTokenUtil.getToken();String postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;JSONObject jsonObject = new JSONObject();jsonObject.put("touser", "發送到用戶的openid"); // openidjsonObject.put("template_id", "你的模板id");jsonObject.put("url", "http://www.baidu.com");JSONObject data = new JSONObject();JSONObject first = new JSONObject();first.put("value", "hello");first.put("color", "#173177");JSONObject keyword1 = new JSONObject();keyword1.put("value", "hello");keyword1.put("color", "#173177");JSONObject keyword2 = new JSONObject();keyword2.put("value", "hello");keyword2.put("color", "#173177");JSONObject keyword3 = new JSONObject();keyword3.put("value", "hello");keyword3.put("color", "#173177");JSONObject remark = new JSONObject();remark.put("value", "hello");remark.put("color", "#173177");data.put("first",first);data.put("keyword1",keyword1);data.put("keyword2",keyword2);data.put("keyword3",keyword3);data.put("remark",remark);jsonObject.put("data", data);String string = HttpClientUtils.sendPostJsonStr(postUrl, jsonObject.toJSONString());JSONObject result = JSON.parseObject(string);int errcode = result.getIntValue("errcode");if(errcode == 0){// 發送成功System.out.println("發送成功");} else {// 發送失敗System.out.println("發送失敗");}6 常見問題
有時候你會發現你的jsonobject中進行轉字符串會被轉義,然后發出去的模板消息讀不到內容,可能是因為你放入的Json字符串,之后又進行了一次jsonObject.toJSONString(),之后就被轉義了,建議就是把所有的Json放入到一個jsonObject,最后合并時在進行jsonObject.toJSONString().
以下為測試代碼:最終的話被轉義的內容是空白!
@ApiOperation(value = "測試發送微信公眾模板消息", notes = "測試發送微信公眾模板消息")@GetMapping("/sendMsg")public ResponseEntity sendMsg() throws ApiException {com.alibaba.fastjson.JSONObject jsonObject=new com.alibaba.fastjson.JSONObject();com.alibaba.fastjson.JSONObject jsonObjectValue = new com.alibaba.fastjson.JSONObject();jsonObjectValue.put("value","oldlu");jsonObjectValue.put("color","#173177");com.alibaba.fastjson.JSONObject firstValue = new com.alibaba.fastjson.JSONObject();firstValue.put("value","oldlu");firstValue.put("color","#173177");jsonObject.put("first",firstValue.toJSONString());com.alibaba.fastjson.JSONObject keyword1Value = new com.alibaba.fastjson.JSONObject();keyword1Value.put("value","oldlu");keyword1Value.put("color","#173177");jsonObject.put("keyword1",keyword1Value.toString());com.alibaba.fastjson.JSONObject keyword2Value = new com.alibaba.fastjson.JSONObject();keyword2Value.put("value","oldlu");keyword2Value.put("color","#173177");jsonObject.put("keyword2",keyword2Value);com.alibaba.fastjson.JSONObject keyword3Value = new com.alibaba.fastjson.JSONObject();keyword3Value.put("value","oldlu");keyword3Value.put("color","#173177");jsonObject.put("keyword3",keyword3Value);com.alibaba.fastjson.JSONObject remarkValue = new com.alibaba.fastjson.JSONObject();remarkValue.put("value","oldlu");remarkValue.put("color","#173177");jsonObject.put("remark",remarkValue);JSONObject jsonResponse= WeChatMpUtil.sendTemplate("oUtXT6DbS3tA_eeMaUVZvN66ezTs", "Eeh7tc4SiJ9W49QSyjTeuCgscRTjBq_DEmf1BNmq_VU", jsonObject, "http://www.baidu.com/");return ResponseEntity.ok(jsonResponse);}toString和toJsonString其實是一樣(源碼):
總結
以上是生活随笔為你收集整理的微信公众号发送模板消息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3.4学习笔记(九) Pyt
- 下一篇: 现在是2016-09-23,查询2个月后