const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.subscribeMessage.send({
touser: event.openid,
page: event.pageurl,
data: event.data,
templateId: event.templateId
})
return result
} catch (err) {
return err
}
}
订阅功能在云端是无法调试的,必须由小程序或后端服务发起发送订阅信息,云端调用调试会出现错误。
调用可使用:
wx.cloud.callFunction({
name: 'sendmsg',
data: {
pageurl: pageurl,
data: cloud_data,
templateId: templateId,
openid: openid
},
success: function (res) {
console.log(res);
callback(res.data);
},
fail: function (err) {
errFun(err);
}
})
可多次调用并发送给多个接收订阅者,但是云调用发送速度较慢,人数过多时,要考虑运行时间。