This article uses the cloud development way to realize the small program customer service automatic reply message.

At present, there is only one trigger condition for small programs to send messages, that is, users actively send messages to customer service, and then the messages will be sent to developers, who use cloud functions and relevant APIS to automatically reply to users’ messages.

Cloud development documentation developers.weixin.qq.com/miniprogram…

<! -- Open the customer service dialogue page -->
<button open-type="contact">Contact customer service</button>
Copy the code

There are four message types, event.msgType. You need to configure the triggered message type and the received cloud function in the cloud development console. For example, if the message type is text, the cloud function “replyMessage” will be triggered after the user sends a message of the type text to the customer service. In this case, the cloud function can perform the processing after receiving the user’s message, such as replying the message. Multiple message type triggering conditions can also be configured for the same cloud function.

Add a notification push configuration

// Cloud function entry file
const cloud = require('wx-server-sdk')

// Download the cloud storage image and upload it to the wechat server for sending picture messages. You can manually upload the image to the cloud storage in advance to obtain the FileID.
let downLoad = async (event, context) => {
    const res = await cloud.downloadFile({
        fileID: 'cloud://xxx'.// File ID of the image, fileId of the image uploaded through the cloud development console in advance
    })
    const buffer = res.fileContent
    return buffer
}

// Before sending pictures, you need to upload media files to wechat server to obtain media-related data
let upload = async (Buffer) => {
    return await cloud.openapi.customerServiceMessage.uploadTempMedia({
        type: 'image'.media: {
            contentType: 'image/png'.value: Buffer
        }
    })
}

// Cloud function entry function
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()

    //event.MsgType Indicates the type of the message sent by the user

    if (event.MsgType == 'miniprogrampage') {
        // await cloud.openapi.customerServiceMessage.send({
        // touser: wxContext.OPENID,
        // msgtype: 'text',
        // text: {
        // content: 'Received MsgType=' + event.msgType + '; content=' + event.Content,
        / /},
        // })
    } else if (event.MsgType == 'text') {
        // Send the image
        if (event.Content == 'app') {
            let Buffer = await downLoad() // To download from cloud storage, you need to send images
            let meida = await upload(Buffer) // Upload the image to wechat server to get mediaId
            // We can use mediaId to send the image
            await cloud.openapi.customerServiceMessage.send({
                "touser": wxContext.OPENID, // User openID, available using wxContext.openid
                "msgtype": "image"."image": {
                    "media_id": meida.mediaId,// To send the image to mediaId}}}})else {
        // await cloud.openapi.customerServiceMessage.send({
        // 'touser': wxContext.OPENID,
        // 'msgtype': 'link',
        // 'link':{
        //     'title': '标题1',
        // 'url': 'https://www.baidu.com',
        // 'description': 'description',
        // 'thumb_url': 'http://vsource.cn/wp-content/uploads/2019/11/learn_downqr-150x150.png'
        / /}
        // })
    }
    return 'success'
}
Copy the code

Send the message of the service API developers.weixin.qq.com/miniprogram…

Special note: at present, the user action that can trigger the developer cloud function is only the user actively sending messages, and other actions, such as triggering the user to enter the customer service interface, are not supported.