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 use java to classify Wechat to receive messages and create entities

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use java to achieve Wechat classification to receive messages and create entities. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

(1) basic classes of message entities

Package com.cuiyongzhi.wechat.message.req;/** * ClassName: BaseMessage * @ Description: Wechat request message basic class * @ author dapengniao * @ date 3:03:59 on March 7, 2016 * / public class BaseMessage {/ / developer Wechat account private String ToUserName; / / sender account (an OpenID) private String FromUserName; / / message creation time (integer) private long CreateTime / / message type (text/image/location/link/video/shortvideo) private String MsgType; / / message id,64 bit integer private long MsgId; 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 long getMsgId () {return MsgId;} public void setMsgId (long msgId) {MsgId = msgId;}}

(2) pojo entity of ordinary message

① picture message

Package com.cuiyongzhi.wechat.message.req; / * * ClassName: ImageMessage * @ Description: picture message * @ author dapengniao * @ date 3:04:52 * / public class ImageMessage extends BaseMessage {/ / picture link private String PicUrl; public String getPicUrl () {return PicUrl;} public void setPicUrl (String picUrl) {PicUrl = picUrl;}}

② connection message

Package com.cuiyongzhi.wechat.message.req; / * * ClassName: LinkMessage * @ Description: connection message * @ author dapengniao * @ date 3:05:48 * / public class LinkMessage extends BaseMessage {/ / message title private String Title; / / message description private String Description; / / message link private String Url; public String getTitle () {return Title;} public void setTitle (String title) {Title = title } public String getDescription () {return Description;} public void setDescription (String description) {Description = description;} public String getUrl () {return Url;} public void setUrl (String url) {Url = url;}}

③ geolocation message

Package com.cuiyongzhi.wechat.message.req; / * * ClassName: LocationMessage * @ Description: geolocation message * @ author dapengniao * @ date 3:06:10 * / public class LocationMessage extends BaseMessage {/ / Geographic location Dimension private String Location_X; / / Geographic location Longitude private String Location_Y; / / Map Zoom size private String Scale; / / Geographic location Information private String Label Public String getLocation_X () {return Location_X;} public void setLocation_X (String location_X) {Location_X = location_X;} public String getLocation_Y () {return Location_Y;} public void setLocation_Y (String location_Y) {Location_Y = location_Y;} public String getScale () {return Scale;} public void setScale (String scale) {Scale = scale;} public String getLabel () {return Label } public void setLabel (String label) {Label = label;}}

④ text message

Package com.cuiyongzhi.wechat.message.req; / * * ClassName: TextMessage * @ Description: text message * @ author dapengniao * @ date 3:06:40 * / public class TextMessage extends BaseMessage {/ / message content private String Content; public String getContent () {return Content;} public void setContent (String content) {Content = content;}}

⑤ Video / Mini Video messages

Package com.cuiyongzhi.wechat.message.req; / * * ClassName: VideoMessage * @ Description: video / small video message * @ author dapengniao * @ date 3:12:51 on March 7, 2016 * / public class VideoMessage extends BaseMessage {private String MediaId; / / video message media id, you can call the multimedia file download API to pull data private String ThumbMediaId / / Media id of video message thumbnails. You can call the multimedia file download API to pull data public String getMediaId () {return MediaId;} public void setMediaId (String mediaId) {MediaId = mediaId;} public String getThumbMediaId () {return ThumbMediaId;} public void setThumbMediaId (String thumbMediaId) {ThumbMediaId = thumbMediaId;}}.

⑥ voice messa

Package com.cuiyongzhi.wechat.message.req;/** * ClassName: VoiceMessage * @ Description: voice message * @ author dapengniao * @ date 3:07:10 * / public class VoiceMessage extends BaseMessage {/ / Media ID private String MediaId; / / Voice format private String Format; public String getMediaId () {return MediaId;} public void setMediaId (String mediaId) {MediaId = mediaId;} public String getFormat () {return Format } public void setFormat (String format) {Format = format;}}

(3) message classification and processing

According to the above received message categories to do different distribution processing, here we set up our own business distributor (EventDispatcher, MsgDispatcher), respectively do ordinary message processing and event message processing!

① MsgDispatcher.java-- is used for business distribution processing of ordinary messages

Package com.cuiyongzhi.wechat.dispatcher;import java.util.Map;import com.cuiyongzhi.wechat.util.MessageUtil / * ClassName: MsgDispatcher * @ Description: message business processing distributor * @ author dapengniao * @ date 4:04:21 * / public class MsgDispatcher {public static String processMessage (Map map) {if (map.get ("MsgType") .equals (MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {/ / text message System.out.println ("= this is a text message!") ;} if (map.get ("MsgType") .equals (MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {/ / picture message System.out.println ("= this is a picture message!") ;} if (map.get ("MsgType") .equals (MessageUtil.REQ_MESSAGE_TYPE_LINK)) {/ / Link message System.out.println ("= this is a link message!") ;} if (map.get ("MsgType") .equals (MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {/ / location message System.out.println ("= this is a location message!") ;} if (map.get ("MsgType") .equals (MessageUtil.REQ_MESSAGE_TYPE_VIDEO)) {/ / Video message System.out.println ("= this is a video message!") ;} if (map.get ("MsgType") .equals (MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {/ / voice message System.out.println ("= this is a voice message!") ;} return null;}}

Business Distribution processing of ② EventDispatcher.java-- event messages

Package com.cuiyongzhi.wechat.dispatcher; import java.util.Map; import com.cuiyongzhi.wechat.util.MessageUtil / * ClassName: EventDispatcher * @ Description: event message Service Distributor * @ author dapengniao * @ date 4:04:41 * / public class EventDispatcher {public static String processEvent (Map map) {if (map.get ("Event") .equals (MessageUtil.EVENT_TYPE_SUBSCRIBE)) {/ / follow event System.out.println ("= this is a follow event!") ;} if (map.get ("Event") .equals (MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {/ / unfollow the event System.out.println ("= this is the unfollowed event!") ;} if (map.get ("Event") .equals (MessageUtil.EVENT_TYPE_SCAN)) {/ / scan the QR code event System.out.println ("= this is the scan QR code event!") ;} if (map.get ("Event") .equals (MessageUtil.EVENT_TYPE_LOCATION)) {/ / location escalation event System.out.println ("= this is a location escalation event!") ;} if (map.get ("Event") .equals (MessageUtil.EVENT_TYPE_CLICK)) {/ / Custom menu click event System.out.println ("= this is a custom menu click event!") ;} if (map.get ("Event") .equals (MessageUtil.EVENT_TYPE_VIEW)) {/ / Custom menu View event System.out.println ("= this is a custom menu View event!") ;} return null;}}

At this point, we need to make some changes to the post method in our message entry [WechatSecurity.java]. The final result is as follows:

Package com.cuiyongzhi.wechat.controller; import java.io.PrintWriter;import java.util.Map; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam; import com.cuiyongzhi.wechat.dispatcher.EventDispatcher;import com.cuiyongzhi.wechat.dispatcher.MsgDispatcher;import com.cuiyongzhi.wechat.util.MessageUtil Import com.cuiyongzhi.wechat.util.SignUtil; @ Controller@RequestMapping ("/ wechat") public class WechatSecurity {private static Logger logger = Logger.getLogger (WechatSecurity.class) / * @ Description: used to receive get parameters Return the verification parameter * @ param @ param request * @ param @ param response * @ param @ param signature * @ param * @ param * @ param @ param echostr * @ author dapengniao * @ date 6:20:00 * / @ RequestMapping (value = "security", method = RequestMethod.GET) public void doGet (HttpServletRequest request, HttpServletResponse response, @ RequestParam (value = "signature", required = true) String signature @ RequestParam (value = "timestamp", required = true) String timestamp, @ RequestParam (value = "nonce", required = true) String nonce, @ RequestParam (value = "echostr", required = true) String echostr) {try {if (SignUtil.checkSignature (signature, timestamp, nonce) {PrintWriter out = response.getWriter () Out.print (echostr); out.close ();} else {logger.info ("there is an illegal request!") ;} catch (Exception e) {logger.error (e, e) }} / * * @ Description: receive Wechat message processing and distribute it * @ param @ param request * @ param @ param response * @ author dapengniao * @ date 4:06:47 * / @ RequestMapping (value = "security", method = RequestMethod.POST) public void DoPost (HttpServletRequest request,HttpServletResponse response) {try {Map map=MessageUtil.parseXml (request); String msgtype=map.get ("MsgType") If (MessageUtil.REQ_MESSAGE_TYPE_EVENT.equals (msgtype)) {EventDispatcher.processEvent (map); / / enter event processing} else {MsgDispatcher.processMessage (map); / / enter message processing}} catch (Exception e) {logger.error (e);}

Finally, after we run the project successfully, we can verify the correctness of our message classification by sending different types of messages, as shown in the following figure:

With so many new files, let's take a look at the directory structure of our entire project:

Thank you for reading! This is the end of the article on "how to use java to classify Wechat to receive messages and create entities". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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