WeChat Mini Program API Template Message
Based on WeChat’s notification channel, we provide developers with template messaging capabilities that can efficiently reach users, in order to achieve a closed-loop service and provide a better experience.
Template push location: Service notification
Template delivery conditions: Triggered after the user interacts with the page in the WeChat system. For details, please see the delivery condition description
Template Jump ability: Clicking to view details can only jump to each page of the account where the template is issued
Instructions
- Get the template id
Log in to https://mp.weixin.qq.com to obtain the template. If there is no suitable template, you can apply to add a new template. It can be used after passing the review. For details, please see the template review instructions
The
<form/>
component of the page, when the attributereport-submit
istrue
, can be declared as You need to send a template message. At this time, click the button to submit the form to getformId
, which is used to send the template message. Or when the user completes the payment behavior, he can obtainprepay_id
for sending template messages.Call the interface to deliver template messages (see interface description for details)
Interface description
1. Get access_token
access_token is a globally unique interface calling credential. Developers need to use access_token when calling each interface. Please keep it properly. At least 512 characters of space must be reserved for access_token storage. The validity period of access_token is currently 2 hours and needs to be refreshed regularly. Repeated acquisition will cause the last access_token to become invalid.
Instructions on the use and generation of access_token required for API calls on the public platform:
- In order to keep appsecrect secret, the third party needs a central control server to obtain and refresh the access_token. The access_token used by other business logic servers comes from the central control server and should not be refreshed individually, otherwise the access_token will be overwritten and affect the business;
- The current validity period of the access_token is conveyed through the returned expire_in. Currently It is the value within 7200 seconds. The central control server needs to refresh the new access_token in advance according to this valid time. During the refresh process, the central control server still outputs the old access_token. At this time, the public platform backend will ensure that both the old and new access_token are available within a short time of refresh, which ensures the smooth transition of third-party services;
- The validity time of access_token may be adjusted in the future, so the central control server not only needs to actively refresh internally at regular intervals, but also needs to provide an interface for passively refreshing access_token, so that the business server can trigger access_token when the API call learns that access_token has timed out. refresh process.
Developers can use AppID and AppSecret to call this interface to obtain access_token. AppID and AppSecret can be obtained by logging into the WeChat official website - Settings - Development Settings (you need to be bound to become a developer, and the account has no abnormal status). Please save the AppSecret yourself after it is generated, because every time it is generated and viewed on the public platform, the AppSecret will be reset. Note that the https protocol must be used when calling all WeChat interfaces. If the third party does not use the central control server, but chooses each business logic point to refresh the access_token separately, conflicts may occur, leading to service instability.
Interface address:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
HTTP request method:
GET
Parameter description:
Return parameter description:
Under normal circumstances, WeChat will return the following JSON data packet to the developer :
{"access_token":"ACCESS_TOKEN","expires_in":7200}
When an error occurs, WeChat will return error code and other information. The JSON data packet example is as follows (this example is an invalid AppID error):
{"errcode":40013,"errmsg":"invalid appid"}
2. Send template message
Interface address: (ACCESS_TOKEN needs to be replaced with the access_token obtained above)
https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
HTTP request method:
POST
POST parameter description:
##Example:
{ "touser": "OPENID", "template_id": "TEMPLATE_ID", "page": "index", "form_id": "FORMID", "data": { "keyword1": { "value": "339208499", "color": "#173177" }, "keyword2": { "value": "2015年01月05日 12:30", "color": "#173177" }, "keyword3": { "value": "粤海喜来登酒店", "color": "#173177" } , "keyword4": { "value": "广州市天河区天河路208号", "color": "#173177" } }, "emphasis_keyword": "keyword1.DATA" }
Return code description:
After calling the template message interface, a JSON data packet will be returned. Example of returning JSON data packet when normal:{ "errcode":0, "errmsg":"ok", }In case of error, error code information will be returned, the description is as follows:
Return code | Description |
---|---|
template_id is incorrect | |
form_id is incorrect or expired | |
form_id has been used | |
page is incorrect |