亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序?qū)崿F(xiàn)自動回復(fù)圖片消息

 更新時(shí)間:2023年01月17日 15:00:39   作者:六六的小帥  
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)客服消息自動回復(fù)圖片消息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

前提

小程序已經(jīng)開通了“云開發(fā)”功能

在微信開發(fā)者工具中打開“云開發(fā)”,點(diǎn)“設(shè)置”,點(diǎn)擊“其它設(shè)置”,點(diǎn)擊“添加消息推送”(添加消息類型為“image”和“event”兩種消息推送設(shè)置),點(diǎn)擊“確定”

目前微信小程序用戶使用客服功能,必須通過固定的按鈕進(jìn)行觸發(fā),在下文展示

按鈕觸發(fā)后小程序會主動發(fā)送客服圖片消息

實(shí)現(xiàn)

小程序index.wxml文件中實(shí)現(xiàn)

需要將 button 組件 open-type 的值設(shè)置為 contact,當(dāng)用戶點(diǎn)擊后就會進(jìn)入客服會話,如果用戶在會話中點(diǎn)擊了小程序消息,則會返回到小程序,開發(fā)者可以通過 bindcontact 事件回調(diào)獲取到用戶所點(diǎn)消息的頁面路徑 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實(shí)現(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)用實(shí)例

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

cloud.downloadFile的使用

從云存儲空間下載文件 其中下載文件的返回類型為 ArrayBuffer

調(diào)用實(shí)例

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

到此這篇關(guān)于微信小程序?qū)崿F(xiàn)自動回復(fù)圖片消息的文章就介紹到這了,更多相關(guān)小程序自動回復(fù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論