Small program realizes message subscription and sending

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
insert image description here
to switch to the public template, we enter the appointment in the search box, select the appointment result notification template,
insert image description here
select the user name, appointment time, and appointment result from the keywords, and click submit to
insert image description here
save successfully The template will appear in My Templates.
insert image description here
Click Details. What you need to pay attention to is the template ID and detailed content fields
insert image description here

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
insert image description here
to select the custom code,
insert image description here
enter the name and logo,
insert image description here
then modify the method name and logo, and
insert image description here
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
insert image description here
Click the method test, enter the input parameter and
insert image description here
click the output parameter mapping
insert image description here

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
insert image description here
Select the javascript code
insert image description here
Select to add a new method
insert image description here
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
insert image description here

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
insert image description here

Guess you like

Origin blog.csdn.net/u012877217/article/details/130122672