In our housekeeping service applet, users can add appointments. The general scenario is to remind the user to receive a notification when adding an appointment, and send a subscription message when the status changes. In this article, let's explain the development of the applet subscription message function.
1 Activate subscription message template
To send a subscription message, you first need to select a message template. Open your own mini program background, click on the subscription message menu
to switch to the public template, we enter the appointment in the search box, select the appointment result notification template,
select the user name, appointment time, and appointment result from the keywords, and click submit to
save successfully The template will appear in My Templates.
Click Details. What you need to pay attention to is the template ID and detailed content fields
2 Create a custom API
In the micro-build, if we need to send subscription messages, we need to develop the back-end logic ourselves. The code of the backend logic is written in the API, log in to the console, click New APIs
to select the custom code,
enter the name and logo,
then modify the method name and logo, and
paste the following code in the custom code
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
module.exports = async function (params, context) {
try {
const result = await cloud.openapi({
appid: context.env.currentAppId}).subscribeMessage.send({
"touser": context.env.currentOpenId,
"page": 'index',
"lang": 'zh_CN',
"data": {
"name1": {
"value": params.name1
},
"date2": {
"value": params.date2
},
"phrase3": {
"value": params.phrase3
}
},
"templateId": params.templateId,
"miniprogramState": 'trial'
})
return result
} catch (err) {
return err
}
};
Then create a new input parameter, the structure of the input parameter is as follows
Click the method test, enter the input parameter and
click the output parameter mapping
3 Prompt the user to subscribe and send a message
After the API is created, we can remind the user to subscribe. The timing of the subscription is to prompt the user to subscribe and send a message after clicking the submit button.
Open our new appointment page, select the button, and add an event
Select the javascript code
Select to add a new method
Enter the name of the method, the code is as follows
export default function({
event, data}) {
try{
wx.requestSubscribeMessage({
tmplIds: ['qwtYZFy7Y8a63nLm91nRq9_VtFpH_NUz3SJNh_jmm8c'],//模板id
success (res) {
console.log(res)
},
fail (res){
console.log(res)
}
})
} catch (e) {
console.log("错误代码",e.code,"错误信息",e.message)
}
}
Then select the form container, and we will send the user a successful appointment message after the data source is successfully added
export default async function({
event, data}) {
//const openId = app.auth.currentUser.openId
const currentdatte = app.utils.formatDate(Date.now(), 'yyyy年mm月dd日 HH时MM分ss秒');
try{
const result = await app.cloud.callConnector({
name: 'yycgtx_r9t1lv4',
methodName: 'sendYuyueMsg',
params: {
"templateId":"qwtYZFy7Y8a63nLm91nRq9_VtFpH_NUz3SJNh_jmm8c", //消息模板id
"name1":app.dataset.state.user.nc,
"date2":currentdatte,
"phrase3":"预约成功"
}, // 方法入参
});
console.log("result",result)
} catch (e) {
console.log("错误代码",e.code,"错误信息",e.message)
}
}
4 Mini Program release
Click the publish button on the top navigation bar to publish it as a trial version, and we will test it on the mobile phone