Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat program API receives messages and events


May 19, 2021 WeChat Mini Program Development Document


Table of contents


Receive messages and events


On the page, you can display the go-to-customer service session button by using the button open-type-"contact" />

When a user sends a message in a customer service session, or when an event push is raised by certain user actions, the WeChat server sends packets of the message or event to the URL filled out by the developer, and if the cloud development is used, it can be pushed to the specified cloud function (see Message Push for details). When a developer receives a request, they can use the Send customer service message interface to make an asynchronous reply.

The push JSON and XML packet structures for each message type are as follows.

The text message

When a user sends a text message in a customer service session, the following packets are generated:

XML format

<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 format

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

Description of the parameters

Parameters Description
ToUserName The original ID of the applet
FromUserName The sender's openid
CreateTime Message creation time (integer)
MsgType text
Content The content of the text message
MsgId Message id, 64-bit integer

Picture message

When a user sends a picture message in a customer service session, the following packets are generated:

XML format

<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 format

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

Description of the parameters

Parameters Description
ToUserName The original ID of the applet
FromUserName The sender's openid
CreateTime Message creation time (integer)
MsgType image
PicUrl Photo link (generated by the system)
MediaId Picture message media id, you can call the getTempMedia interface to pull the data.
MsgId Message id, 64-bit integer

Small program card message

When a user sends a small program card message in a customer service session, the following packets are generated:

XML format

<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 format

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

Description of the parameters

Parameters Description
ToUserName The original ID of the applet
FromUserName The sender's openid
CreateTime Message creation time (integer)
MsgType miniprogrampage
MsgId Message id, 64-bit integer
Title Title
AppId Small program appid
PagePath The path to the small program page
ThumbUrl Temporary cdn link to cover picture
ThumbMediaId Temporary footage id for the cover picture

Enter the session event

The user will generate the following packets when the small program "Customer Service Session Button" enters the customer service session:

XML format

<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 format

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

Description of the parameters

Parameters Description
ToUserName The original ID of the applet
FromUserName The sender's openid
CreateTime Event creation time (integer)
MsgType event
Event The type of event, user_enter_tempsession
SessionFrom The session-from property set by the developer in the customer service session button