WeChat Open Platform

Mini Programs Documentation

Receive messages & events

The Enter Service Center Session button can be displayed by using <contact-button/> on the page.

When a user sends a message in a Service Center session (or when certain certain user actions trigger push events), the WeChat server will post a request for the message (or event) packet (JSON or XML format) to the url entered by the developer. After the developer receives the request they can use the Send Service Center Message interface to reply asynchronously.

After the WeChat server sends the user message to the Mini Program developer's server address (configured in Development Settings), it will cut off the connection if it does not receive a response within five seconds, and initiate the request again. It will retry a total of three times. If it is discovered during debugging that the user is unable to receive a response message, you can check whether message processing timed out. Using msgid to remove duplicate retry messages is recommended for messages with msgid. Using FromUserName + CreateTime to remove duplicates is recommended for event type messages.

After the server receives the request, it must reply as below. This is so that the WeChat server will not do any processing on this or initiate a retry, otherwise a serious error prompt will appear. Refer to the instructions below for further details:

1. Directly reply success (recommended method)
2. Directly reply with an empty string (refers to an empty string with a byte length of 0, not the content field in the structure being empty)

WeChat will send the user a system prompt in the Mini Program session, "This Mini Program Service Center is temporarily unable to provide services, please try again later", if the situations below are encountered:

1. The developer fails to reply to any content within five seconds
2. The developer replied with abnormal data

If the developer wishes to increase security, they can enable message encryption in the Developer Center. This means that messages the user sends to the Mini Program and the Mini Program's passive replies to user messages will both continue to be encrypted. Refer to Message Encryption and Decryption Instructions for more details.

The structures of the JSON and XML packets pushed for each message type are shown below.

Text messages

The packets below will be generated when a user sends a text message in a Service Center session:

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
}

Parameter descriptions

Parameter Description
ToUserName Original Mini Program ID
FromUserName Sender's openid
CreateTime Time message created (integer)
MsgType text
Content Text message content
MsgId Message id, 64-bit integer

Image messages

The packets below will be generated when a user sends an image message in a Service Center session:

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
}

Parameter descriptions

Parameter Description
ToUserName Original Mini Program ID
FromUserName Sender's openid
CreateTime Time message created (integer)
MsgType image
PicUrl Image link (generated by system)
MediaId Image message media id, the Get Temporary Materials interface can be called to pull data
MsgId Message id, 64-bit integer

Enter session event

The packets below will be generated when a user enters a Service Center session via the Mini Program Service Center Session button:

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"
}

Parameter descriptions

Parameter Description
ToUserName Original Mini Program ID
FromUserName Sender's openid
CreateTime Time event created (integer)
MsgType event
Event Event type, user_enter_tempsession
SessionFrom sessionFrom parameters set by developer for the Service Center Session button