微信小程序客服自动回复二维码或者H5图文连接

微信小程序客服默认后台只支持回复文字。

如果你想自动回复二维码图片,或者一个H5页面的连接怎么办。之前要用自己服务器写接口,现在有了云函数简单多了。

第一步,添加一个云函数msgPush

直接上代码

// 云函数入口文件
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': 'url'
      }
    })
  }
  return 'success'
}

第二步,打开云开发,添加云函数消息推送

 记得添加EVENT事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值