May 20, 2021 WeChat Mini Program Development Document
Version requirements: wx-server-sdk sdk, 0.4.0, developer tools, 1.02.1904090 (RC version download)
Cloud calls are cloud-based functions that provide the ability to open interfaces using small programs, and currently cover the following usage scenarios:
Cloud calls need to be used through wx-server-sdk in the cloud function. C alling the service-side interface with a cloud call in a cloud function does not need to be exchanged for access_token, as long as the cloud call is initiated in a cloud function triggered from the small terminal is automatically identified by WeChat, and open interfaces such as sending template messages can be called directly after the permission is registered. Here's how to use it:
All service-side interfaces are listed in the list of service-side interfaces, and if the interface supports cloud calls, the label of cloud calls is next to the interface name. At the same time, in each service-side interface documentation, if the interface supports cloud calls, there are specific support instructions and corresponding usage documentation.
In the interface documentation that supports cloud calls, the documentation for HTTPS calls and the documentation for the cloud call are listed separately, and the cloud call document, like the HTTPS call document, contains request parameters, return values, and examples.
Next, you need to configure the cloud call permissions, each cloud function needs to declare the interface it will use, otherwise it cannot be called, the declared method is to add the name of the interface to be called in the permissions.openapi field of the config.json (if no new construction is required) profile in the cloud function directory, permissions.openapi is a string array field, the value must be the name of the service-side interface that needs to be called. E ach time you upload a cloud function using WeChat developer tools, you will update the permissions according to the configuration, which has a cache for 10 minutes, and if you are prompted after the update that you do not have permissions, wait 10 minutes before trying again. The following is an example that claims a profile that uses the send template message interface:
{
"permissions": {
"openapi": [
"templateMessage.send"
]
}
}
First of all, the cloud function needs to use a version number of at least 0.4.0 wx-server-sdk, it is recommended that wx-server-sdk always be up-to-date, to ensure that the cloud function directory package.json's wx-server-sdk field is latest, such as local installation dependency, please perform npm install--save wx-server-sdk@latest.
Next, you can use the cloud call API in the cloud function. T he cloud call API is mounted under the openapi object of the wx-server-sdk module, and each open interface category has a secondary namespace object under the openapi object (such as the template message interface's methods are under openapi.templateMessage), which mounts all open methods under that category (for example, the sending interface for template messages is openapi.templateMessage.send). T he category and method names of each interface dependent can be viewed by the interface name, which is named after the .lt;category..lt;method, such as the interface name that sends the template message is templateMessage.send. Here's an example of sending a template message to yourself:
For a direct-to-run example, create a project in the IDE for a cloud development quick start template with an example of a cloud call that sends a template message
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.templateMessage.send({
touser: cloud.getWXContext().OPENID, // 通过 getWXContext 获取 OPENID
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'
})
// result 结构
// { errCode: 0, errMsg: 'openapi.templateMessage.send:ok' }
return result
} catch (err) {
// 错误处理
// err.errCode !== 0
throw err
}
}
For the small terminal interface that returns sensitive open data, starting from the base library 2.7.0, if the small program has been opened for cloud development, the only cloudID corresponding to sensitive open data can be obtained in the return value of the open data interface, and the open data can be obtained directly through the cloud call, as can be seen in the cloud call.
Cloud development also supports receiving small program message pushes through cloud functions (such as triggering cloud functions when customer service messages are received), as seen in message push.