微信小程序API 接收消息和事件

2020-09-15 17:12 更新

接收消息和事件


在頁(yè)面中使用 <button open-type="contact" /> 可以顯示進(jìn)入客服會(huì)話按鈕。

當(dāng)用戶在客服會(huì)話發(fā)送消息、或由某些特定的用戶操作引發(fā)事件推送時(shí),微信服務(wù)器會(huì)將消息或事件的數(shù)據(jù)包發(fā)送到開發(fā)者填寫的 URL,如果使用的是云開發(fā),則可以推送到指定的云函數(shù)(詳情請(qǐng)參考消息推送)。開發(fā)者收到請(qǐng)求后可以使用 發(fā)送客服消息 接口進(jìn)行異步回復(fù)。

各消息類型的推送JSON、XML數(shù)據(jù)包結(jié)構(gòu)如下。

文本消息

用戶在客服會(huì)話中發(fā)送文本消息時(shí)將產(chǎn)生如下數(shù)據(jù)包:

XML 格式

<xml>
   <ToUserName><![CDATA[toUser]]></ToUserName>
   <FromUserName><![CDATA[fromUser]]></FromUserName>
   <CreateTime>1482048670</CreateTime>
   <MsgType><![CDATA[text]]></MsgType>
   <Content><![CDATA[this is a test]]></Content>
   <MsgId>1234567890123456</MsgId>
</xml>

JSON 格式

{
  "ToUserName": "toUser",
  "FromUserName": "fromUser",
  "CreateTime": 1482048670,
  "MsgType": "text",
  "Content": "this is a test",
  "MsgId": 1234567890123456
}

參數(shù)說(shuō)明

參數(shù)說(shuō)明
ToUserName小程序的原始ID
FromUserName發(fā)送者的openid
CreateTime消息創(chuàng)建時(shí)間(整型)
MsgTypetext
Content文本消息內(nèi)容
MsgId消息id,64位整型

圖片消息

用戶在客服會(huì)話中發(fā)送圖片消息時(shí)將產(chǎn)生如下數(shù)據(jù)包:

XML 格式

<xml>
      <ToUserName><![CDATA[toUser]]></ToUserName>
      <FromUserName><![CDATA[fromUser]]></FromUserName>
      <CreateTime>1482048670</CreateTime>
      <MsgType><![CDATA[image]]></MsgType>
      <PicUrl><![CDATA[this is a url]]></PicUrl>
      <MediaId><![CDATA[media_id]]></MediaId>
      <MsgId>1234567890123456</MsgId>
</xml>

JSON 格式

{
  "ToUserName": "toUser",
  "FromUserName": "fromUser",
  "CreateTime": 1482048670,
  "MsgType": "image",
  "PicUrl": "this is a url",
  "MediaId": "media_id",
  "MsgId": 1234567890123456
}

參數(shù)說(shuō)明

參數(shù)說(shuō)明
ToUserName小程序的原始ID
FromUserName發(fā)送者的openid
CreateTime消息創(chuàng)建時(shí)間(整型)
MsgTypeimage
PicUrl圖片鏈接(由系統(tǒng)生成)
MediaId圖片消息媒體id,可以調(diào)用[獲取臨時(shí)素材]((getTempMedia)接口拉取數(shù)據(jù)。
MsgId消息id,64位整型

小程序卡片消息

用戶在客服會(huì)話中發(fā)送小程序卡片消息時(shí)將產(chǎn)生如下數(shù)據(jù)包:

XML 格式

<xml>
  <ToUserName><![CDATA[toUser]]></ToUserName>
  <FromUserName><![CDATA[fromUser]]></FromUserName>
  <CreateTime>1482048670</CreateTime>
  <MsgType><![CDATA[miniprogrampage]]></MsgType>
  <MsgId>1234567890123456</MsgId>
  <Title><![CDATA[Title]]></Title>
  <AppId><![CDATA[AppId]]></AppId>
  <PagePath><![CDATA[PagePath]]></PagePath>
  <ThumbUrl><![CDATA[ThumbUrl]]></ThumbUrl>
  <ThumbMediaId><![CDATA[ThumbMediaId]]></ThumbMediaId>
</xml>

JSON 格式

{
  "ToUserName": "toUser",
  "FromUserName": "fromUser",
  "CreateTime": 1482048670,
  "MsgType": "miniprogrampage",
  "MsgId": 1234567890123456,
  "Title":"title",
  "AppId":"appid",
  "PagePath":"path",
  "ThumbUrl":"",
  "ThumbMediaId":""
}

參數(shù)說(shuō)明

參數(shù)說(shuō)明
ToUserName小程序的原始ID
FromUserName發(fā)送者的openid
CreateTime消息創(chuàng)建時(shí)間(整型)
MsgTypeminiprogrampage
MsgId消息id,64位整型
Title標(biāo)題
AppId小程序appid
PagePath小程序頁(yè)面路徑
ThumbUrl封面圖片的臨時(shí)cdn鏈接
ThumbMediaId封面圖片的臨時(shí)素材id

進(jìn)入會(huì)話事件

用戶在小程序“客服會(huì)話按鈕”進(jìn)入客服會(huì)話時(shí)將產(chǎn)生如下數(shù)據(jù)包:

XML 格式

<xml>
    <ToUserName><![CDATA[toUser]]></ToUserName>
    <FromUserName><![CDATA[fromUser]]></FromUserName>
    <CreateTime>1482048670</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[user_enter_tempsession]]></Event>
    <SessionFrom><![CDATA[sessionFrom]]></SessionFrom>
</xml>

JSON 格式

{
  "ToUserName": "toUser",
  "FromUserName": "fromUser",
  "CreateTime": 1482048670,
  "MsgType": "event",
  "Event": "user_enter_tempsession",
  "SessionFrom": "sessionFrom"
}

參數(shù)說(shuō)明

參數(shù)說(shuō)明
ToUserName小程序的原始ID
FromUserName發(fā)送者的openid
CreateTime事件創(chuàng)建時(shí)間(整型)
MsgTypeevent
Event事件類型,user_enter_tempsession
SessionFrom開發(fā)者在客服會(huì)話按鈕設(shè)置的 session-from 屬性


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)