WeChat public account receives messages and processes ordinary messages
There are two situations when WeChat users interact with public accounts: one is when WeChat users send ordinary messages to public accounts; the other is when some operations of WeChat users cause the WeChat server to notify the URL filled in by the developer through event push. This article introduces the processing of public accounts receiving ordinary messages.
The following passage is excerpted from WeChat official development documentation:
1. Regarding retrying message deduplication, it is recommended to use msgid deduplication.
2. If the WeChat server does not receive a response within five seconds, it will disconnect and re-initiate the request, retrying a total of three times. If the server cannot guarantee processing and replying within five seconds, it can directly reply with an empty string. The WeChat server will not do anything about it and will not retry. For details, please see "Send Message-Passive Reply Message".
3. In order to ensure higher security, developers can set up message encryption in the Developer Center of the official website of the public platform. After encryption is turned on, messages sent by users will be encrypted, and messages that the public account passively replies to users also need to be encrypted (but developers sending messages to users through API calls such as customer service interfaces will not be affected). For detailed instructions on message encryption and decryption, please see "Message Encryption and Decryption Instructions" .
Ordinary messages are divided into seven categories: text messages, picture messages, voice messages, video messages, short video messages, location messages and link messages.
When a WeChat user sends a message to a public account, the WeChat server will identify the type of message and send the corresponding message XML data to the developer. Taking a text message as an example, its format is as follows:
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[Content]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
The meaning of each field is as follows
ToUserNameDeveloper WeChat
IDFromUserNameSender account (an OpenID)
CreateTimeMessage creation time (integer) MsgTypeMessage
typeContentText message contentMsgIdMessage
id , 64-bit integer
After the WeChat server sends the XML data to the developer, the developer determines the type of the message based on the MsgType field. The values of MsgType for different messages are as follows:
Text message text
picture message image
voice message voice
video message video
short video message short video
location message location
link message link
For the XML data format of each message, the following five fields are available for each message, where the value of MsgType varies according to the message type.
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text/image/voice/video/shortvideo/location/link]]></MsgType>
<MsgId>1234567890123456</MsgId>
Let's look at the different fields.
Text Messages
<Content><![CDATA[Content]]></Content> //Text message content
Picture Message
<PicUrl><![CDATA[this is a url]]></PicUrl> //Picture link
<MediaId><![CDATA[media_id]]></MediaId> //Picture message media id, you can call the multimedia file download interface to pull data.
Voice Message
<MediaId><![CDATA[media_id]]></MediaId> //Voice message media id, you can call the multimedia file download interface to pull data.
<Format><![CDATA[Format]]></Format> //Voice format such as amr, speex, etc.
In addition to the above two fields, voice messages also have another field - voice recognition - which needs to be enabled in the official account before it can be used.
<Recognition><![CDATA[]]></Recognition> //Close voice recognition, the content is empty
// Open
<Recognition><![CDATA[Tencent WeChat Team]]></Recognition> //Open voice recognition, the content is the content of the recognized voice
Video Message
<MediaId><![CDATA[media_id]]></MediaId> //The media id of the video message. You can call the multimedia file download interface to pull data.
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> //The media id of the video message thumbnail. You can call the multimedia file download interface to pull data.
Small video message
<MediaId><![CDATA[media_id]]></MediaId> //The media id of the video message. You can call the multimedia file download interface to pull data.
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> //The media id of the video message thumbnail. You can call the multimedia file download interface to pull data.
Location Message
<Location_X>23.134521</Location_X> //Geographic location latitude
<Location_Y>113.358803</Location_Y> //Geographic location longitude
<Scale>20</Scale> //Map zoom size
<Label><![CDATA[Location information]]></Label> //Geographic location information
Link Message
<Title><![CDATA[Ji Yi Blog]]></Title> //Message title
<Description><![CDATA[Ji Yi Blog - A professional technical blog]]></Description> //Message content
<Url><![CDATA[http://www.onmpw.com]]></Url> //Message link
The formats of the various messages are those we showed above.
Next, let's take a look at how developers receive these messages.
function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents("/tmp/wx.txt", $postStr); //Write the message content to the file for our own observation
//extract post data
if (!empty($postStr)){
//Write the message processing code here
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$msgType = $postObj->MsgType;
//Write different processing according to different message types
}else {
echo "";
exit;
}
}
responseMsg() //Calling this function is equivalent to opening this interface for WeChat server to call and push messages
The processing of receiving messages on WeChat public accounts is actually very simple. What we are talking about here is the processing of plain text information, as well as the safe mode and compatible mode. We will talk about it later, so here we will introduce so much.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Detailed explanation of the implementation methods of SSO single sign-on in three
Publish Date:2025/03/18 Views:190 Category:NETWORK
-
Single Sign On (SSO) is not unfamiliar to us. For large systems, using SSO can reduce a lot of trouble for users. Take Baidu for example. Baidu has many subsystems - Baidu Experience, Baidu Knows, Baidu Library, etc. If we need to enter a u
What multipart/form-data does in post Upload upload files
Publish Date:2025/03/18 Views:63 Category:NETWORK
-
Everyone has used the attribute enctype="multipart/form-data" when uploading files using a form. What is the role of multipart/form-data? Let's talk about this topic. First, let's look at a case Look at the first code form action= "handl
About application/x-www-form-urlencoded
Publish Date:2025/03/18 Views:147 Category:NETWORK
-
As a data format of form, application/x-www-form-urlencoded has its own characteristics form action= "handle.php" method= "post" input type= "text" name= " uname" class= " uname" /br / input type= "text" name= "email" class=
My understanding of webservice is this
Publish Date:2025/03/18 Views:147 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
WSDL looks like this
Publish Date:2025/03/18 Views:190 Category:NETWORK
-
When I first started learning Webservice, I found that there were quite a lot of knowledge points involved, and each point could be a school of its own. Especially when I saw WSDL, I looked up information for a long time, but I was still a
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:61 Category:NETWORK
-
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio
Implementing a group chat room using socket.io
Publish Date:2025/03/18 Views:65 Category:NETWORK
-
This article will share with you an example of using socket.io to realize the function of group chat. If you want to use socket.io, you must use nodejs to implement the server, so we need to install socket.io in nodejs Install socket.io How
First contact with CGI
Publish Date:2025/03/18 Views:51 Category:NETWORK
-
Since I am a PHP programmer, I often have to build a PHP operating environment. The popular nginx+php environment is very popular, and the mode it adopts is the FastCGI method, so I spent some time to learn about FastCGI. CGI (Common Gatewa
Getting started with FastCGI
Publish Date:2025/03/18 Views:164 Category:NETWORK
-
In "First Contact with CGI", we mentioned the operating mechanisms of CGI and Server APIs, as well as their respective advantages and disadvantages. In this chapter, we will learn about FastCGI, which combines the advantages of CGI and Serv