前提
小程序已經(jīng)開通了“云開發(fā)”功能
在微信開發(fā)者工具中打開“云開發(fā)”,點“設(shè)置”,點擊“其它設(shè)置”,點擊“添加消息推送”(添加消息類型為“image”和“event”兩種消息推送設(shè)置),點擊“確定”
目前微信小程序用戶使用客服功能,必須通過固定的按鈕進(jìn)行觸發(fā),在下文展示
按鈕觸發(fā)后小程序會主動發(fā)送客服圖片消息
實現(xiàn)
小程序index.wxml文件中實現(xiàn)
需要將 button 組件 open-type 的值設(shè)置為 contact,當(dāng)用戶點擊后就會進(jìn)入客服會話,如果用戶在會話中點擊了小程序消息,則會返回到小程序,開發(fā)者可以通過 bindcontact 事件回調(diào)獲取到用戶所點消息的頁面路徑 path 和對應(yīng)的參數(shù) query,此外,開發(fā)者可以通過設(shè)置 session-from 將會話來源透傳到客服。
<button open-type="contact" bindcontact="handleContact" session-from="sessionFrom">客服</button>
云函數(shù)config.json配置文件
{
"permissions": {
"openapi": [
"wxacode.get",
"customerServiceMessage.send",
"customerServiceMessage.uploadTempMedia"
]
}
}
小程序云函數(shù)入口文件index.js實現(xiàn)
const cloud = require('wx-server-sdk')
cloud.init()
// 下載云存儲圖片
// 講圖片上傳到小程序云開發(fā)的存儲中可以得到文件的fileID
let downLoad = async(event, context) => {
const res = await cloud.downloadFile({
fileID: 'cloud://example.png'
})
const buffer = res.fileContent
return buffer
}
// 把媒體文件上傳到微信服務(wù)器
let upload = async(Buffer) => {
return await cloud.openapi.customerServiceMessage.uploadTempMedia({
type: 'image',
media: {
contentType: 'image/png',
value: Buffer
}
})
}
// 云函數(shù)入口函數(shù)
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 的使用:
把媒體文件上傳到微信服務(wù)器。目前僅支持圖片。用于發(fā)送客服消息或被動回復(fù)用戶消息。
云調(diào)用是微信云開發(fā)提供的在云函數(shù)中調(diào)用微信開放接口的能力,需要在云函數(shù)中通過 wx-server-sdk 使用。
調(diào)用實例
// cloud = require('wx-server-sdk')
// ...
// 方法返回 Promise
cloud.openapi.customerServiceMessage.uploadTempMedia({
type: 'image',
media: {
contentType: 'image/png',
value: Buffer
}
})
cloud.downloadFile的使用文章來源:http://www.zghlxwxcb.cn/news/detail-495357.html
從云存儲空間下載文件 其中下載文件的返回類型為 ArrayBuffer
調(diào)用實例文章來源地址http://www.zghlxwxcb.cn/news/detail-495357.html
wx.cloud.downloadFile({
fileID: 'a7xzcb'
}).then(res => {
console.log(res.data)
}).catch(error => {
// handle error
})
到了這里,關(guān)于微信小程序?qū)崿F(xiàn)客服消息自動回復(fù)(回復(fù)圖片消息)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!