WeChat Mini Program pushes Official Account template messages.

Published: 2024-07-15

Description:

Because the Mini Program is passively triggered to push messages, it needs to be made to push to that user's WeChat Official Account.

Code pushing template messages is done through the user's openId, but the openId of the Mini Program and the WeChat Official Account are different, so we can only find another way.

According to WeChat's official website, if they are bound to the same WeChat Open Platform, a unionId will be generated. This unionId is the same on the Mini Program side and the WeChat Official Account side, so we use this to implement the function of pushing template messages to the WeChat Official Account.


Preparations:

WeChat Official Account: AppId and APPSecret (must be verified)

WeChat Mini Program: AppId and APPSecret (must be verified)

WeChat Open Platform (the Mini Program and Official Account must be bound to the same Open Platform and must be verified)

For the verification method, refer to WeChat documentation or Baidu.


Get all user information of the WeChat Official Account:

1. Through the WeChat Official Account's AppId and AppSecret (the current IP address must be in the WeChat Official Account's whitelist)

Obtain the WeChat Official Account's access_token (valid for 7200 seconds)

URL: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET


Return result:

{
 "access_token": "ACCESS_TOKEN",
 "expires_in": 7200}


(Replace APPID and APPSECRET with your own WeChat Official Account's appId and APPSecret)


2. Use the access_token just obtained to get the openId of all users

URL: https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID


Return result:

{"count": 2,"data": {
  "openid": [
    "OPENID1",
    "OPENID2"
  ]},"next_openid": "NEXT_OPENID","total": 2}

(If there are more than 10000, take 10001 and continue requesting as NEXT_OPENID; this will be included in the JSON string returned after each request)


3. Put all openIds into a new array, then use a for loop; the number of iterations is the length of the openId array, and request each user's basic information in a loop

URL: https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN


Return result:

{
"subscribe": 1,
"openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
"nickname": "Band",
"sex": 1,
"language": "zh_CN",
"city": "Guangzhou",
"province": "Guangdong",
"country": "China",
"headimgurl": "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
"subscribe_time": 1382694957,
"unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL",
"remark": "",
"groupid": 0,
"tagid_list": [
  128,
  2
],
"subscribe_scene": "ADD_SCENE_QR_CODE",
"qr_scene": 98765,
"qr_scene_str": ""
}

(The unionid is only available if the Open Platform is bound)


4. Store all user information in your own database


Get Mini Program user information:

1. The Mini Program side will obtain a user login credential code by calling wx.login() (valid for five minutes)

2. Pass the code to the backend; through appId, APPSecret, and code, the backend can obtain this user's basic WeChat information


URL: https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code


Return result:

{
 "sex": 1,
 "photoUrl": "https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83eofr4W1SptUJ9M1PUiaqA4XBZiapCN98sPYOjNDXZSrUp1JcicibeLiaHZ56qlQnibQEUSFGLnEatu1TyiaA/132",
 "openId": "oDl465JGG54ezszjG3K2spJV1TNM",
 "uniodId": "otjNb03MD_IipC8XRdL68GOKLVgs"}

(The unionid is only available if the Open Platform is bound. If there is still no unionId after binding, then this Mini Program user has not followed a WeChat Official Account under the same entity)


3. Save the user information in your own database


WeChat Official Account push:

1. The local database stores the Mini Program user table and the WeChat Official Account table; below is how to push WeChat Official Account information to a certain Mini Program user

2. Take any user A's information from the Mini Program user table, including user A's openId and unionId; use the unionId to search the Official Account table for the corresponding WeChat Official Account openId of user A

3. Select a template message on the WeChat Official Account, edit the content to be sent, and then request the API for sending template messages


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

Request parameters:

{
  "touser": "OPENID",
  "template_id": "ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
  "url": "http://weixin.qq.com/download",
  "miniprogram": {
    "appid": "xiaochengxuappid12345",
    "pagepath": "index?foo=bar"
  },
  "data": {
    "first": {
      "value": "Congratulations on your successful purchase!",
      "color": "#173177"
    },
    "keyword1": {
      "value": "Chocolate",
      "color": "#173177"
    },
    "keyword2": {
      "value": "39.8 yuan",
      "color": "#173177"
    },
    "keyword3": {
      "value": "September 22, 2014",
      "color": "#173177"
    },
    "remark": {
      "value": "Welcome to purchase again!",
      "color": "#173177"
    }
  }}


Parameter
RequiredDescription
touserYesReceiver openid
template_idYesTemplate ID
urlNoTemplate redirect link
miniprogramNoData required to jump to the Mini Program; if you do not need to jump to the Mini Program, you do not need to pass this data
appidYesappid of the Mini Program to jump to (this Mini Program appid must be bound and associated with the Official Account sending the template message; mini games are not supported for now)
pagepathNoSpecific page path to jump to in the Mini Program, supports parameters (example index?foo=bar); mini games are not supported for now
dataYesTemplate data
colorNoFont color of the template content; if not filled, the default is black

The above are the steps for pushing WeChat Official Account template messages to a WeChat Mini Program.

*Note: For cases where the WeChat Official Account cannot push, or a push error occurs, the Mini Program corresponding to the appid under the pushed miniprogram must have been reviewed and published before it can push.