赞
踩
微信小程序客服默认后台只支持回复文字。
如果你想自动回复二维码图片,或者一个H5页面的连接怎么办。之前要用自己服务器写接口,现在有了云函数简单多了。
直接上代码
- // 云函数入口文件
- 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'
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
记得添加EVENT事件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。