失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 微信小程序实现客服消息自动回复(回复图片消息)

微信小程序实现客服消息自动回复(回复图片消息)

时间:2022-02-01 17:25:10

相关推荐

微信小程序实现客服消息自动回复(回复图片消息)

前提

小程序已经开通了“云开发”功能

在微信开发者工具中打开“云开发”,点“设置”,点击“其它设置”,点击“添加消息推送”(添加消息类型为“image”和“event”两种消息推送设置),点击“确定”

目前微信小程序用户使用客服功能,必须通过固定的按钮进行触发,在下文展示

按钮触发后小程序会主动发送客服图片消息

实现

小程序index.wxml文件中实现

需要将 button 组件 open-type 的值设置为 contact,当用户点击后就会进入客服会话,如果用户在会话中点击了小程序消息,则会返回到小程序,开发者可以通过 bindcontact 事件回调获取到用户所点消息的页面路径 path 和对应的参数 query,此外,开发者可以通过设置 session-from 将会话来源透传到客服。

<button open-type="contact" bindcontact="handleContact" session-from="sessionFrom">客服</button>

云函数config.json配置文件

{"permissions": {"openapi": ["wxacode.get","customerServiceMessage.send","customerServiceMessage.uploadTempMedia"]}}

小程序云函数入口文件index.js实现

const cloud = require('wx-server-sdk')cloud.init()// 下载云存储图片// 讲图片上传到小程序云开发的存储中可以得到文件的fileIDlet downLoad = async(event, context) => {const res = await cloud.downloadFile({fileID: 'cloud://example.png' })const buffer = res.fileContentreturn buffer}// 把媒体文件上传到微信服务器let upload = async(Buffer) => {return await cloud.openapi.customerServiceMessage.uploadTempMedia({type: 'image',media: {contentType: 'image/png',value: Buffer}})}// 云函数入口函数exports.main = async (event, context) => {// 客服消息if (event.MsgType == 'event') {const wxContext = cloud.getWXContext()let Buffer = await downLoad()let meida = await upload(Buffer)await cloud.openapi.customerServiceMessage.send({"touser": wxContext.OPENID,"msgtype": "image","image": {"media_id": meida.mediaId}})return "success"}};

备注

customerServiceMessage.uploadTempMedia 的使用:

把媒体文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息。

云调用是微信云开发提供的在云函数中调用微信开放接口的能力,需要在云函数中通过 wx-server-sdk 使用。

调用实例

// cloud = require('wx-server-sdk')// ...// 方法返回 Promisecloud.openapi.customerServiceMessage.uploadTempMedia({type: 'image',media: {contentType: 'image/png',value: Buffer}})

cloud.downloadFile的使用

从云存储空间下载文件 其中下载文件的返回类型为 ArrayBuffer

调用实例

wx.cloud.downloadFile({fileID: 'a7xzcb'}).then(res => {console.log(res.data)}).catch(error => {// handle error})

如果觉得《微信小程序实现客服消息自动回复(回复图片消息)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。