Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement WeChat Mini Programs API receiving messages and events

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge about how WeChat Mini Programs API receives messages and events. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Receive messages and events

Use the button to display the enter customer service session on the page.

When a user sends a message in a customer service session (or pushes an event caused by some specific user actions), the Wechat server will POST the packet of the message (or event) (in JSON or XML format) to the URL filled in by the developer. After receiving the request, developers can use the API to send customer service messages to reply asynchronously.

After the Wechat server sends the user's message to the developer server address of Mini Program (configured in the development settings), the Wechat server will be disconnected if it does not receive a response within five seconds, and the request will be reinitiated for a total of three times. If you find that the user cannot receive the response message during debugging, you can check whether the message processing timed out. For retry messages, it is recommended to use msgid for messages with msgid. FromUserName + CreateTime is recommended for event type messages.

The server must make the following reply to the request so that the Wechat server will not do anything about it and will not initiate a retry, otherwise, a serious error will appear. See the following instructions for details:

1. Reply success directly (recommended method) 2. Reply directly to an empty string (an empty string with a byte length of 0, rather than an empty content field in the structure)

In the event of the following situations, Wechat will send a systematic prompt to the user in the Mini Program session: "the Mini Program customer service is temporarily unavailable, please try again later":

1. The developer did not reply to anything within 5 seconds. 2. The developer replied to the abnormal data.

If developers want to enhance security, you can enable message encryption in the developer Center. In this way, messages sent by users to Mini Program and messages sent by Mini Program passively reply to users will continue to be encrypted. For more information, please see message encryption and decryption instructions.

The structure of push JSON and XML packets for each message type is as follows.

Text message

When a user sends a text message in a customer service session, the following packet is generated:

XML format

1482048670 1234567890123456

JSON format

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

Parameter description

Parameter description ToUserName Mini Program original IDFromUserName sender openidCreateTime message creation time (integer) MsgTypetextContent text message content MsgId message id,64 bit integer picture message

When a user sends a picture message in a customer service session, the following packet will be generated:

XML format

1482048670 1234567890123456

JSON format

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

Parameter description

Parameters indicate the creation time of the openidCreateTime message of the original IDFromUserName sender of ToUserName Mini Program (integer) MsgTypeimagePicUrl picture link (generated by the system) MediaId picture message media id. You can call API to obtain temporary material to pull data. MsgId message id,64 bit integer enter session event

Users will generate the following data packets when Mini Program "customer Service session Button" enters the customer service session:

XML format

1482048670

JSON format

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

Parameter description

The parameter describes the openidCreateTime event creation time (integer) of the original IDFromUserName sender of ToUserName Mini Program, the MsgTypeeventEvent event type, and the sessionFrom parameter set by the user_enter_tempsessionSessionFrom developer in the customer service session button

These are all the contents of the article "how to achieve WeChat Mini Programs API receiving messages and events". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report