2. Automatically reply to and automatically reply to messages
The previous article has been connected to the public platform. Now let's reply to the message.
Differences between accessing the public platform and message processing:
Access public platforms: method = RequestMethod. GET;
Message Processing: method = RequestMethod. POST
The following are examples of text messages, other messages view the document: https://mp.weixin.qq.com/wiki? T = resource/res_main & id = mp1421140453
By default, we already know the XML structure for common users to send messages to public accounts.
1. XmlMessageEntity entity class
package com.wenxi.Entity;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "xml")@XmlAccessorType(XmlAccessType.FIELD)public class XmlMessageEntity {private String ToUserName;private String FromUserName;private Long CreateTime;private String MsgType;private String Content;private String Event;private Long MsgId;public String getEvent() {return Event;}public void setEvent(String event) {Event = event;}public String getToUserName() {return ToUserName;}public void setToUserName(String toUserName) {ToUserName = toUserName;}public String getFromUserName() {return FromUserName;}public void setFromUserName(String fromUserName) {FromUserName = fromUserName;}public Long getCreateTime() {return CreateTime;}public void setCreateTime(Long createTime) {CreateTime = createTime;}public String getMsgType() {return MsgType;}public void setMsgType(String msgType) {MsgType = msgType;}public String getContent() {return Content;}public void setContent(String content) {Content = content;}public Long getMsgId() {return MsgId;}public void setMsgId(Long msgId) {MsgId = msgId;}@Overridepublic String toString() {return "XmlMessageEntity [ToUserName=" + ToUserName + ", FromUserName="+ FromUserName + ", Event=" + Event + ", MsgType=" + MsgType+ ", Content=" + Content + ", MsgId=" + MsgId + "]";}}
2. process the message and return it
Package com. wenxi. controller; import com. wenxi. entity. xmlMessageEntity; import com. wenxi. utils. securityUtil; import com. wenxi. utils. weixinUtil; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestBody; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import org. springframework. web. Bind. annotation. responseBody; import java. util. arrays; import java. util. date; @ Controllerpublic class ValidateController {@ RequestMapping (value = "/weixinValidate", method = RequestMethod. GET) @ ResponseBody public String weixinValidate (String signature, String timestamp, String nonce, String echostr) {// 1) sort the token, timestamp, and nonce parameters alphabetically by String [] arrs = {WeixinUtil. TOKEN, timestamp, nonce}; // Weixi NUtil. TOKEN: The extracted TOKEN to reuse Arrays. sort (arrs); // 2) Concatenates the three parameter strings into one String for sha1 encryption String str = arrs [0] + arrs [1] + arrs [2]; // 3) the encrypted string obtained by the developer can be compared with signature to identify that the request comes from if (signature. equals (SecurityUtil. SHA1 (str) {// confirm that the GET request comes from the server. If the echostr parameter content is returned, the access takes effect and becomes a developer. Otherwise, the access fails. return echostr ;} else return null;}/*** auto reply message */@ RequestMapping (value = "weixinValidate", method = RequestMethod. POST) @ ResponseBody publ Ic XmlMessageEntity weixinValidate (@ RequestBody XmlMessageEntity entity) {System. out. println (entity); XmlMessageEntity returnMessageEntity = new XmlMessageEntity (); returnMessageEntity. setFromUserName (entity. getToUserName (); returnMessageEntity. setToUserName (entity. getFromUserName (); returnMessageEntity. setContent ("Thank you for your attention! "); ReturnMessageEntity. setCreateTime (new Date (). getTime (); returnMessageEntity. setMsgType (" text "); return returnMessageEntity ;}}
Below is the XML structure of the reply, other message view document: https://mp.weixin.qq.com/wiki? T = resource/res_main & id = mp1421140543
3. Test
1. Follow the public account: On your test account page, scan the QR code in the figure and click follow
Console printing:
XmlMessageEntity [ToUserName = xxxxxa436, FromUserName = xxxxxxxxxlJdq9AY2E, Event = subscribe, MsgType = event, Content = null, MsgId = null]
Here, ToUserName (developer ID) can be viewed at the top of the test page, as shown in figure
FromUserName (User OpenID) can be found in the user list in the test code QR code
Display:
2. Cancel the public account
Console printing:
XmlMessageEntity [ToUserName = xxxxxa436, FromUserName = xxxxxxxxxlJdq9AY2E, Event = unsubscribe, MsgType = event, Content = null, MsgId = null]
Difference between the two: Event
Unsubscribe: cancels the public account
Subscribe: Follow the public account
3. Send a message on the Public Account
Because the program hasn't processed MsgType by category, all of them are replies. Thank you for your attention)
Console printing:
XmlMessageEntity [ToUserName = xxxxxa436, FromUserName = xxxxxxxxxlJdq9AY2E, Event = null, MsgType = text, Content = test, MsgId = 6479560144755671793]
Difference between follow (cancel) and normal message sending:
Event: follow (cancel) is subscribe or unsubscribe, while normal message sending is null;
MsgType: the focus (cancel) is the event, and the normal message is sent as text;
Content: The followed (canceled) parameter is null, while the normal message is sent as a specific message;
MsgId: the focus (cancel) is null, while the normal message is sent as the specific ID;
1. msgid is recommended for message deduplication of retries.
2. If the server fails to receive the response within five seconds, the connection will be disconnected and a request will be initiated again. A total of three retries will be made. If the server cannot process and reply within five seconds, it can directly reply to an empty string. The server will not process the request and will not initiate a retry. For details, see "Send message-passive reply message ".