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 develop the reply picture and text message function of Wechat official account

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

Share

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

This article mainly introduces how to use java to develop the Wechat official account reply picture and text message function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The details are as follows

Description of the main parameters of picture and text messages

Through Wechat's official message API guide, you can see the introduction of the parameters of picture and text messages, as shown in the following figure:

As can be seen from the above picture:

1. The number of picture and text messages is limited to 10, that is, the value of ArticleCount in the picture and text (the number of picture and text messages is limited to 10)

2. For the picture and text message, the picture of the first picture and text is displayed as a large picture, and the picture of the other picture and text is displayed as a small picture.

3. The picture size of the first picture and text is suggested to be 640-320, and that for other pictures and texts is 80-80.

The following begins to be implemented:

Base class of the request message:

Import com.thoughtworks.xstream.annotations.XStreamAlias;import java.io.Serializable;/** * @ author inchlifc * / public class BaseMessage implements Serializable {@ XStreamAlias ("ToUserName") @ XStreamCDATA private String ToUserName; @ XStreamAlias ("FromUserName") @ XStreamCDATA private String FromUserName; @ XStreamAlias ("CreateTime") private Long CreateTime; @ XStreamAlias ("MsgType") @ XStreamCDATA private String MsgType; public BaseMessage () {super ();} public BaseMessage (String fromUserName, String toUserName) {super (); FromUserName = fromUserName ToUserName = toUserName; CreateTime = System.currentTimeMillis ();} 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;}}

Picture and text message class:

Import com.thoughtworks.xstream.annotations.XStreamAlias;import java.util.List;@XStreamAlias ("xml") public class ArticlesMessage extends BaseMessage {@ XStreamAlias ("ArticleCount") private int ArticleCount; @ XStreamAlias ("Articles") private List Articles; public int getArticleCount () {return ArticleCount;} public void setArticleCount (int articleCount) {ArticleCount = articleCount;} public List getArticles () {return Articles;} public void setArticles (List articles) {Articles = articles;}}

The Articles class in the Teletext message:

Import com.thoughtworks.xstream.annotations.XStreamAlias;import java.util.List;@XStreamAlias ("Articles") public class Articles {private List Articles;}

The ArticlesItem class in the Teletext message:

Import com.thoughtworks.xstream.annotations.XStreamAlias;import java.io.Serializable;@XStreamAlias ("item") public class ArticlesItem implements Serializable {@ XStreamAlias ("Title") @ XStreamCDATA private String Title; @ XStreamAlias ("Description") @ XStreamCDATA private String Description; @ XStreamAlias ("PicUrl") @ XStreamCDATA private String PicUrl; @ XStreamAlias ("Url") @ XStreamCDATA 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 getPicUrl () {return PicUrl;} public void setPicUrl (String picUrl) {PicUrl = picUrl;} public String getUrl () {return Url;} public void setUrl (String url) {Url = url;}}

Service layer implementation method:

Encapsulation method

/ * * get blog message * * @ param custermName * @ param serverName * @ param createTime * @ return * / private ArticlesMessage getBlogMessage (String custermName, String serverName, Long createTime) {ArticlesMessage outputMsg = new ArticlesMessage (); outputMsg.setFromUserName (serverName); outputMsg.setToUserName (custermName); outputMsg.setCreateTime (createTime); outputMsg.setMsgType (MsgType.NEWS.getValue ()); List articles = new ArrayList () ArticlesItem item1 = new ArticlesItem (); item1.setTitle ("cool breeze in the evening"); item1.setDescription ("Click to enter the cool breeze blog"); item1.setPicUrl (WechatConstant.BASE_SERVER + "resources/images/wechat/a.png"); item1.setUrl ("https://my.oschina.net/inchlifc/blog"); articles.add (item1); outputMsg.setArticles (articles); outputMsg.setArticleCount (articles.size ()); return outputMsg;}"

Judge that if you enter the number 1, return the picture and text message push

/ / process the received message ServletInputStream in = request.getInputStream (); / / convert the POST stream to the XStream object XStream xs = new XStream (); xs = SerializeXmlUtil.createXstream (); XStream.setupDefaultSecurity (xs); xs.allowTypes (new Class [] {TextMessage.class, InputMessage.class, ArticlesMessage.class}); xs.processAnnotations (InputMessage.class); xs.processAnnotations (ArticlesMessage.class); xs.processAnnotations (ImageMessage.class) / / Map the xml node data under the specified node to the object xs.alias ("xml", InputMessage.class); / / convert the stream to the string StringBuilder xmlMsg = new StringBuilder (); byte [] b = new byte [4096]; for (int n; (n = in.read (b))! =-1;) {xmlMsg.append (new String (b, 0, n, "UTF-8")) } logger.info ("received message = =" + xmlMsg.toString ()); / / convert xml content to InputMessage object InputMessage inputMsg = (InputMessage) xs.fromXML (xmlMsg.toString ()); / / server String servername = inputMsg.getToUserName (); / / client String custermname = inputMsg.getFromUserName (); / / receive time long createTime = inputMsg.getCreateTime () / / return time Long returnTime = Calendar.getInstance () .getTimeInMillis () / 1000; / / take over text content String content = inputMsg.getContent (); / / get message type String msgType = inputMsg.getMsgType () If (MsgType.TEXT.getValue (). Equals (msgType)) {/ / enter 1 push blog message if ("1" .equals (content)) {logger.info ("receive text 1"); ArticlesMessage outputMsg = getBlogMessage (custermname, servername, returnTime); logger.info ("return blog message = =" + xs.toXML (outputMsg)) Response.getWriter () .write (xs.toXML (outputMsg));}

Running result:

Thank you for reading this article carefully. I hope the article "how to use java to develop the reply picture and text message function of Wechat official account" shared by the editor is helpful to everyone. At the same time, I also hope you can support me and follow the industry information channel. More related knowledge is waiting for you to learn!

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