简单2步,无需服务器,小程序云开发自动客服回复
发布于 4 年前 作者 huangwei 1412 次浏览 来自 分享

云开发完全不需要自己搭建服务器也能使用小程序客服自动回复功能。这里的自动回复包括文字、图片、图文消息。(官方客服仅支持文字自动回复)

效果如下:

步骤一、打开小程序云开发控制台的全局设置

添加需要的拦截的消息类型,一般情况下可以全部拦截

步骤二、小程序自动回复云开发

这里支持文字回复、图片回复以及图文链接回复

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
//
let downLoad = async (event, context) => {
  const res = await cloud.downloadFile({
    fileID: 'cloud://bvread-test-hvcev.6276-bvread-test-hvcev-1302378864/640.png', // 图片的File ID,提前通过云开发控制台上传的图片fileId
  })
  const buffer = res.fileContent
  console.log(buffer)
  return buffer
}

//
let upload = async (Buffer) => {
  return await cloud.openapi.customerServiceMessage.uploadTempMedia({
    type: 'image',
    media: {
      contentType: 'image/png',
      value: Buffer
    }
  })
}

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  if (event.MsgType == 'miniprogrampage') {
    await cloud.openapi.customerServiceMessage.send({
      touser: wxContext.OPENID,
      msgtype: 'text',
      text: {
        content: '收到 MsgType=' + event.MsgType + ';content=' + event.Content,
      },
    })
  } else if (event.MsgType == 'image') {
    let Buffer = await downLoad()
    let meida = await upload(Buffer)
    await cloud.openapi.customerServiceMessage.send({
      "touser": wxContext.OPENID,
      "msgtype": "image",
      "image": {
        "media_id": meida.mediaId
      }
    })
  } else {
    await cloud.openapi.customerServiceMessage.send({
      'touser': wxContext.OPENID,
      'msgtype': 'link',
      'link':{
        'title': '标题1',
        'url': 'https://www.baidu.com',
        'description': '描述',
        'thumb_url': 'http://xx.cn/wp-content/uploads/2019/11/learn_downqr-150x150.png'
      }
    })
  }
  return 'success'
}

1 回复
回到顶部