日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微信公众号发送模板消息

發(fā)布時間:2023/12/9 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信公众号发送模板消息 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

  • 1 開發(fā)中遇到的問題匯總
  • 2 模板消息創(chuàng)建
  • 3 調(diào)試接口
    • 3.1 微信公眾號消息模板
      • 3.1.1 基本信息
    • 3.2 請求參數(shù)
      • 3.2.1 Query參數(shù)及說明
      • 3.2.2 body參數(shù)及說明
  • 4 測試結(jié)果
  • 5 工具類封裝
  • 6 常見問題


1 開發(fā)中遇到的問題匯總

首先是在測試中,遇到最多的就是41003->appid錯誤,然后就是40165,說什么page和pagepath,其實就是線上和體驗的問題,反正不管怎么改發(fā)出去就行,跳轉(zhuǎn)到正確的頁面即可,但是大問題是:公眾號的文檔然后里面發(fā)送模板消息是小程序的,然后一直就是appid錯誤,然后突然發(fā)現(xiàn)還有個文檔,修改body后測試成功.按照我的方式是肯定成功的,因為有的博客都是從官方文檔抄一遍測都不測.


2 模板消息創(chuàng)建

3 調(diào)試接口

3.1 微信公眾號消息模板

3.1.1 基本信息

  • 接口狀態(tài): 開發(fā)中
  • 接口URL: https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=49_pWR_MJBhGBi97IMi0pzXqkxphglh9YzKPs8Wu962QjtVeVL1MbnTLHTL11mUMPFrhJMjPTB6OGvmzcs9w1MJG8xQVbhFalmh5K7WtOHLR7pQDhupwLCKkQGrqsb8sy-OIAkDr2CzWtQVlBOIRTNdAGAEGA
  • 請求方式: POST
  • Content-Type: application/json

3.2 請求參數(shù)

3.2.1 Query參數(shù)及說明

參數(shù)名示例值參數(shù)類型是否必填參數(shù)描述
access_token49_pWR_MJBhGBi97IMi0pzXqkxphglh9YzKPs8Wu962QjtVeVL1MbnTLHTL11mUMPFrhJMjPTB6OGvmzcs9w1MJG8xQVbhFalmh5K7WtOHLR7pQDhupwLCKkQGrqsb8sy-OIAkDr2CzWtQVlBOIRTNdAGAEGA暫無描述

3.2.2 body參數(shù)及說明

{"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 測試結(jié)果


其實開發(fā)過程中并不是很復(fù)雜,只是文檔有一些問題,容易歧義

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", "發(fā)送到用戶的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){// 發(fā)送成功System.out.println("發(fā)送成功");} else {// 發(fā)送失敗System.out.println("發(fā)送失敗");}

6 常見問題

有時候你會發(fā)現(xiàn)你的jsonobject中進(jìn)行轉(zhuǎn)字符串會被轉(zhuǎn)義,然后發(fā)出去的模板消息讀不到內(nèi)容,可能是因為你放入的Json字符串,之后又進(jìn)行了一次jsonObject.toJSONString(),之后就被轉(zhuǎn)義了,建議就是把所有的Json放入到一個jsonObject,最后合并時在進(jìn)行jsonObject.toJSONString().

以下為測試代碼:最終的話被轉(zhuǎn)義的內(nèi)容是空白!

@ApiOperation(value = "測試發(fā)送微信公眾模板消息", notes = "測試發(fā)送微信公眾模板消息")@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其實是一樣(源碼):

總結(jié)

以上是生活随笔為你收集整理的微信公众号发送模板消息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。