Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program send


May 19, 2021 WeChat Mini Program Development Document


Table of contents


uniformMessage.send

This interface should be called on the server side, as detailed in the Service Side API.
This interface supports cloud calls. Developer tool version required . . . 1.02.1904090 (latest stable download)
wx-server-sdk >= 0.4.0

Send a unified service message with a small program and a public number

How to call:

  • HTTPS call
  • Cloud calls

HTTPS call

The request address

POST https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN

Request parameters

Property Type The default Required Description
access_token string Is The interface calls the credentials
touser string Is User openid, can be the openid of a small program, or mp_template_msg the public number of the user.appid
weapp_template_msg Object Whether Small program template message related information, you can refer to the small program template message interface; With this node, a small program template message is sent first
mp_template_msg Object Is Information related to the public number template message can refer to the public number template message interface, and when there is this node and there is no weapp_template_msg node, the public number template message is sent

weapp_template_msg structure of the system

Property Type The default Required Description
template_id string Is Small program template ID
page string Is The path to the small program page
form_id string Is The small program template messageformid
data string Is Small program template data
emphasis_keyword string Is Small program templates zoom in on keywords

mp_template_msg structure of the system

Property Type The default Required Description
appid string Is Public appid, which requires binding to a small program and the same subject
template_id string Yes Public template ID
url string Yes Public Template Message URL to be jumped
miniprogram string Is The small program to jump the public number template message, the small program must be bound to the public number
data string Is Data for the public number template message

Returns a value

Object

The returned JSON packet

Property Type Description
errcode number Error code
errmsg string The error message

Error

Error code The error message Description
40037 The template id is incorrect, weapp_template_msg.template_id or mp_template_msg.template_id
41028 weapp_template_msg.form_id is out of date or incorrect
41029 weapp_template_msg.form_id is already in use
41030 weapp_template_msg.page is incorrect
45009 The interface call exceeds the limit
40003 Touser is not the right openid
40013 The appid is incorrect or does not meet the binding relationship requirements

An example of requesting data

{
    "touser":"OPENID",
    "weapp_template_msg":{
        "template_id":"TEMPLATE_ID",
        "page":"page/page/index",
        "form_id":"FORMID",
        "data":{
            "keyword1":{
                "value":"339208499"
            },
            "keyword2":{
                "value":"2015年01月05日 12:30"
            },
            "keyword3":{
                "value":"腾讯微信总部"
            },
            "keyword4":{
                "value":"广州市海珠区新港中路397号"
            }
        },
        "emphasis_keyword":"keyword1.DATA"
    },
    "mp_template_msg":{
        "appid":"APPID ",
        "template_id":"TEMPLATE_ID",
        "url":"http://weixin.qq.com/download",
        "miniprogram":{
            "appid":"xiaochengxuappid12345",
            "pagepath":"index?foo=bar"
        },
        "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"
            }
        }
    }
}

Return an example of the data

{
 "errcode": 0,
 "errmsg": "ok"
}

Cloud calls

Cloud call is the ability provided by the small program Cloud Development to call WeChat open interfaces in cloud functions and need to be used in cloud functions via wx-server-sdk.

The interface method

openapi.uniformMessage.send
Permissions for the uniformMessage.send API need to be configured in config.json, details

Request parameters

Property Type The default Required Description
touser string Is User openid, can be the openid of a small program, or mp_template_msg the public number of the user.appid
weappTemplateMsg Object Whether Small program template message related information, you can refer to the small program template message interface; With this node, a small program template message is sent first
mpTemplateMsg Object Is Information related to the public number template message can refer to the public number template message interface, and when there is this node and there is no weapp_template_msg node, the public number template message is sent

The structure of weappTemplate Msg

Property Type The default Required Description
templateId string Is Small program template ID
page string Is The path to the small program page
formId string Is The small program template messageformid
data string Is Small program template data
emphasisKeyword string Is Small program templates zoom in on keywords

The structure of mpTemplate Msg

Property Type The default Required Description
appid string Is Public appid, which requires binding to a small program and the same subject
templateId string Is Public number template id
Url string Is The url to jump from the public number template message
miniprogram string Is The small program to jump the public number template message, the small program must be bound to the public number
data string Is Data for the public number template message

Returns a value

Object

The returned JSON packet

Property Type Description
errCode number Error code
errMsg string The error message

Abnormal

Object

The exception thrown

Property Type Description
errCode number Error code
errMsg string The error message

The legal value of errCode

Value Description The lowest version

Error

Error code The error message Description
40037 The template id is incorrect, weapp_template_msg.template_id or mp_template_msg.template_id
41028 weapp_template_msg.form_id is out of date or incorrect
41029 weapp_template_msg.form_id is already in use
41030 weapp_template_msg.page is incorrect
45009 The interface call exceeds the limit
40003 Touser is not the right openid
40013 The appid is incorrect or does not meet the binding relationship requirements

An example of requesting data

const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.uniformMessage.send({
        touser: 'OPENID',
        weappTemplateMsg: {
          page: 'page/page/index',
          data: {
            keyword1: {
              value: '339208499'
            },
            keyword2: {
              value: '2015年01月05日 12:30'
            },
            keyword3: {
              value: '腾讯微信总部'
            },
            keyword4: {
              value: '广州市海珠区新港中路397号'
            }
          },
          templateId: 'TEMPLATE_ID',
          formId: 'FORMID',
          emphasisKeyword: 'keyword1.DATA'
        },
        mpTemplateMsg: {
          appid: 'APPID ',
          url: 'http://weixin.qq.com/download',
          miniprogram: {
            appid: 'xiaochengxuappid12345',
            pagepath: 'index?foo=bar'
          },
          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'
            }
          },
          templateId: 'TEMPLATE_ID'
        }
      })
    return result
  } catch (err) {
    return err
  }
}

Return an example of the data

{
  "errCode": 0,
  "errMsg": "openapi.uniformMessage.send:ok"
}