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

Force.com Wechat Development Series values how to handle user messages

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

Share

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

This article mainly introduces Force.com Wechat development series values how to deal with user messages, the article is very detailed, has a certain reference value, interested friends must read it!

After successfully configuring Force.com as the server of the Wechat Public account, the next task is to process the messages sent by the user. When an ordinary Wechat user sends a message to a public account, the Wechat server packets the XML of the POST message to the URL filled in by the developer. The message types usually include text information, picture information, voice information, video information, geographic location information and link information. For a detailed structure of the XML packets 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. This paper introduces the processing of text message, the format of XML packet of text message and the explanation as follows:

Text message XML packet exampl

1348831860 1234567890123456

Packet structure description

Send data through Debug Logs monitoring usage

The XML data information sent by Wechat users can be easily observed in Force.com. After entering the Force.com backend Setup page, find Debug Logs under Logs in the left navigation bar. Click enter, and 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, where Site.com uses something similar to "Site Guest User..." For similar anonymous accounts, the selected interface is as follows:

At this time, if Wechat users who follow this Wechat public account send any text to this account, refresh the Debug Logs monitoring page and see the corresponding logs added below:

Click in to see the XML packet with the text we sent in the log:

This kind of log can also help with subsequent debugging.

Create a user message processing method

Next, let's open the WeChatRestController class we created earlier and add the following method to respond to the user packet

@ HttpPost global static void doPost () {/ / Storage XML node data variable String toUserName ='; String fromUserName =''; String msgType =''; String content =''; / / get XML packet RestRequest req = RestContext.request; RestResponse res = RestContext.response; string strMsg = req.requestBody.toString (); XmlStreamReader reader = new XmlStreamReader (strMsg) / / parsing XML packets. The processing power of Force.com is not strong, and a simple structure requires more code to parse 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 parsing of the XML packet of the data sent by the user. Next, we organize the text message data returned to the user according to the instructions of 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. The reply data here is also XML structure. It is basically the same as the XML data structure of the text message sent. For more information, please see the detailed introduction of Tencent in the link. The following code automatically sends the current date and time to the user, adding 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 =' 12345678 political 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)

The previous code needs a slight reminder that the ToUserName and FromUserName parameters on line 3 and the message XML packets sent by ordinary Wechat users are the reverse.

Save the code so that ordinary users can quickly receive the current time information if they send any message to this public account.

The above is all the content of the article "Force.com Wechat development series values how to handle user messages". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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