WeChat Mini Program API receives messages and events
Receive messages and events
Use <contact-button/>
on the page to display the button to enter the customer service session.
When a user sends a message in a customer service session (or performs event push caused by certain specific user operations), the WeChat server will POST request development of the message (or event) data package (JSON or XML format) URL filled in by the user. After receiving the request, the developer can use the customer service message interface to respond asynchronously.
After the WeChat server sends the user's message to the mini program's developer server address (configured in the development settings), if the WeChat server does not receive a response within five seconds, it will disconnect and reinitiate the request. A total of three retries are made. If during debugging, it is found that the user cannot receive the response message, you can check whether the message processing times out. Regarding retry message deduplication, it is recommended to use msgid to deduplicate messages with msgid. It is recommended to use FromUserName + CreateTime for event type messages to eliminate duplication.
If the server receives the request and #does not need to be forwarded to the customer service tool#, it must make the following reply, so that the WeChat server will not do anything with it and will not initiate a retry. Otherwise, a message will appear Serious error message. See the instructions below for details:
1、直接回复success(推荐方式) 2、直接回复空串(指字节长度为0的空字符串,而不是结构体中content字段的内容为空)
Once the following situation occurs, WeChat will issue a system prompt to the user in the mini program session "This mini program customer service is temporarily unable to provide services, please try again later":
1、开发者在5秒内未回复任何内容 2、开发者回复了异常数据
If developers want to forward messages or events to customer service tools, please refer to Forwarding Messages to Customer Service Tools. For customer service tool access, please refer to the customer service tool instructions
If developers want to enhance security, they can turn on message encryption in the developer center. In this way, the messages sent by users to the mini program and the mini program passively reply to user messages Encryption will continue. For details, see message encryption and decryption instructions.
The push JSON and XML data packet structures of each message type are as follows.
Text message
When a user sends a text message in a customer service session, the following data packet will be 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 }
Parameter description
Picture message
When the user sends a picture message in the customer service session, the following data packet will be 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 }
Parameter description
Enter session event
User in the mini program "Customer service session button" will generate the following data packet when entering 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" }
Parameter description
Forward message to customer service tool
If If the developer wants the message to be forwarded to the customer service tool for processing, please return a response packet according to the following structure. The data format (XML or JSON) of the response packet needs to be consistent with the set data format of the received message.
XML format
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1482048670</CreateTime> <MsgType><![CDATA[transfer_customer_service]]></MsgType> </xml>
JSON format
{ "ToUserName": "toUser", "FromUserName": "fromUser", "CreateTime": 1482048670, "MsgType": "transfer_customer_service", }