Force.com is an internationally renowned cloud platform company. After successfully configuring Force.com as the server for WeChat public accounts, the next required task is to process the messages sent by users. When an ordinary WeChat user sends a message to a public account, the WeChat server will POST the XML data packet of the message to the URL filled in by the developer. Usually the message types include text information, picture information, voice information, video information, geographical location information and links. information. For the detailed structure of the XML packet for each message, see http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E6%99 %AE%E9%80%9A%E6%B6%88%E6%81%AF. The article introduces the processing of text messages. The text message XML data packet format and description are as follows:
Text message XML data packet example
1348831860 1234567890123456
Data packet structure description
Send data through Debug Logs monitoring usage
In Force.com, you can easily observe the XML data information sent by WeChat users. For this purpose, enter the Force.com background Setup page and click on the left Find Debug Logs under Logs in the side navigation bar. After clicking to enter, click the New button next to Monitored Users on the right, as shown below:
In the next screen You will be able to choose which user you want to monitor the interaction with the Force.com server. In the search screen, you can click the magnifying glass to find all users. Site.com uses an anonymous account similar to "Site Guest User...". After selecting The schematic interface is as follows:
At this time, if a WeChat user who follows this WeChat public account sends any text to this account, refresh the Debug Logs monitoring page and you can click below See that the corresponding log has been added:
Click in and you can see the XML data packet with the text we sent in the log:
This kind of log can also help with subsequent debugging.
Create user message processing method
Next we open the WeChatRestController class created before and add the following method in it to respond to user data packets
@HttpPost global static void doPost(){ //存储XML各节点数据变量 String toUserName = ''; String fromUserName = ''; String msgType = ''; String content = ''; //获取XML数据包 RestRequest req = RestContext.request; RestResponse res = RestContext.response; string strMsg = req.requestBody.toString(); XmlStreamReader reader = new XmlStreamReader(strMsg); //解析XML数据包,Force.com这块的处理能力并不强,简单的结构也需要较多代码来解析 while(reader.hasNext()){ if(reader.getLocalName() == 'ToUserName'){ reader.next(); if(String.isNotBlank(reader.getText())){ toUserName = reader.getText(); } } else if(reader.getLocalName() == 'FromUserName'){ reader.next(); if(String.isNotBlank(reader.getText())){ fromUserName = reader.getText(); } } else if(reader.getLocalName() == 'MsgType'){ reader.next(); if(String.isNotBlank(reader.getText())){ msgType = reader.getText(); } } else if(reader.getLocalName() == 'Content'){ reader.next(); if(String.isNotBlank(reader.getText())){ content = reader.getText(); } } reader.next(); } }
The above code completes the analysis of the data XML package sent by the user. Next, we follow http://mp.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81 %E8%A2%AB%E5%8A%A8%E5%93%8D%E5%BA%94%E6%B6%88%E6%81%AF Description, organize the text message data replied to the user, here The reply data is also an XML structure, which is basically the same as the XML data structure of the sent text message. For details, please refer to the detailed introduction of Tencent in the link. The following code will automatically send the current date and time to the user. Add the following code after the While loop of the previous code:
Datetime dt = System.now(); String returnDT = dt.format('EEEE, MMMM d, yyyy'); String replyMSG = '<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>'; String[] arguments = new String[]{fromUserName, toUserName, rtnMsg}; String formattedReplyMSG = String.format(replyMSG, arguments); RestContext.response.addHeader('Content-Type', 'text/plain'); RestContext.response.responseBody = Blob.valueOf(rtnMsg);
What needs to be slightly reminded of the previous code is the ToUserName parameter and FromUserName parameter in line 3 and the normal The message XML data packet sent by WeChat users is the other way around.
Save the code. At this time, ordinary users who send any message to this public account can quickly receive the current time information.
For more articles related to Force.com WeChat development series user message processing, please pay attention to the PHP Chinese website!