Update: Template Message for WeChat Mini-Program
In the past, I wrote an article about how to send template message for WeChat Mini-Program:
Unfortunately, Tencent announced that they deprecate this way of sending a template message on January 10th, 2020.
What has changed?
- Explicit permission pop-up
2. No more Form ID or Prepay ID is needed
Which means a message is not bound to a button or payment anymore.
3. One permission for one message except for the following industry:
Governmental, Medical, Transportation, Finance, Education
How to implement it?
- Setup Template in Mini-Program dashboard
- Add
wx.requestSubscribeMessage
to mini-program code - Use this endpoint to send message
POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
Add Template in Mini-Program Dashboard
- Click “subscription message” under “function” in the left sidebar
- Click on the green “Add” button
- Select the template you want to use
4. Select the keyword you wish to include in the template
5. Return to “My template” in the subscription message
6. Open the detail
7. Copy template id and take note of the keyword
8. Take note of the keywords
Note: Unlike the previous template message method, the variable key is not always “keyword” and number, but something like “name1” or “time2”, etc.
Mini-Program Code
wx.requestSubscribeMessage({
tmplIds: [''],
success (res) { }
})
So no more formId is needed, you only need to pass in the template id which you need permission for.
Note: You can pass multiple template IDs. The users can check or uncheck the message they want to receive.
For the response, you will receive either “accept”, “reject”, “ban” for each of the template ids.
Backend Code
Here’s an example in NodeJS
// Send messageconst message = { "touser": "OPEN_ID_OF_RECEIVING_USER", "template_id": "TEMPLATE_ID", "page": "/pages/index/index", "data": { "name1": { "value": "Class Name" }, "time2": { "value": "2015年01月05日" }, "thing3": { "value": "TIT创意园" } , "thing4": { "value": "comment" } }};const response = await axios.post(`https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${access_token}`, message);
Result
If you’re not in one of those special industry categories mentioned above, then for one permission, you will be able to send one message.
Links
Official Doc: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html
MP Code Example(Frontend):
https://github.com/davidyu37/wechat-mp-template-message-frontend
Backend Example:
Userland
You can control which message you wish to receive by going into the setting of the Mini-Program.
Conclusion
If you’re new to WeChat related development, here’s a free glossary for you to get up to speed.