微信公众号实现回复图文消息

图文消息的主要参数说明

通过微信官方的消息接口指南,可以看到对图文消息的参数介绍,如下图所示:

从上图可以了解到:

1、图文消息的个数限制为10,也就是图文中ArticleCount的值(图文消息的个数,限制在10条以内)

2、对于图文消息,第一条图文的图片显示为大图,其他图文的图片显示为小图。

3、第一条图文的图片大小建议为640*320,其他图文的图片建议为80*80

下面开始实现:

请求消息的基类:

 1 import com.thoughtworks.xstream.annotations.XStreamAlias;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * @author inchlifc
 7  */
 8 public class BaseMessage implements Serializable {
 9     @XStreamAlias("ToUserName")
10     @XStreamCDATA
11     private String ToUserName;
12 
13     @XStreamAlias("FromUserName")
14     @XStreamCDATA
15     private String FromUserName;
16 
17     @XStreamAlias("CreateTime")
18     private Long CreateTime;
19 
20     @XStreamAlias("MsgType")
21     @XStreamCDATA
22     private String MsgType;
23 
24     public BaseMessage() {
25         super();
26     }
27 
28     public BaseMessage(String fromUserName, String toUserName) {
29         super();
30         FromUserName = fromUserName;
31         ToUserName = toUserName;
32         CreateTime = System.currentTimeMillis();
33     }
34 
35     public String getToUserName() {
36         return ToUserName;
37     }
38 
39     public void setToUserName(String toUserName) {
40         ToUserName = toUserName;
41     }
42 
43     public String getFromUserName() {
44         return FromUserName;
45     }
46 
47     public void setFromUserName(String fromUserName) {
48         FromUserName = fromUserName;
49     }
50 
51     public Long getCreateTime() {
52         return CreateTime;
53     }
54 
55     public void setCreateTime(Long createTime) {
56         CreateTime = createTime;
57     }
58 
59     public String getMsgType() {
60         return MsgType;
61     }
62 
63     public void setMsgType(String msgType) {
64         MsgType = msgType;
65     }
66 }

图文消息类:

 1 import com.thoughtworks.xstream.annotations.XStreamAlias;
 2 
 3 import java.util.List;
 4 
 5 @XStreamAlias("xml")
 6 public class ArticlesMessage extends BaseMessage {
 7     @XStreamAlias("ArticleCount")
 8     private int ArticleCount;
 9 
10     @XStreamAlias("Articles")
11     private List<ArticlesItem> Articles;
12 
13     public int getArticleCount() {
14         return ArticleCount;
15     }
16 
17     public void setArticleCount(int articleCount) {
18         ArticleCount = articleCount;
19     }
20 
21     public List<ArticlesItem> getArticles() {
22         return Articles;
23     }
24 
25     public void setArticles(List<ArticlesItem> articles) {
26         Articles = articles;
27     }
28 }

图文消息中的Articles类:


1 import com.thoughtworks.xstream.annotations.XStreamAlias;
2 
3 import java.util.List;
4 @XStreamAlias("Articles")
5 public class Articles {
6     private List<ArticlesItem> Articles;
7 }

图文消息中的ArticlesItem类:

 1 import com.thoughtworks.xstream.annotations.XStreamAlias;
 2 
 3 import java.io.Serializable;
 4 
 5 @XStreamAlias("item")
 6 public class ArticlesItem implements Serializable {
 7     @XStreamAlias("Title")
 8     @XStreamCDATA
 9     private String Title;
10 
11     @XStreamAlias("Description")
12     @XStreamCDATA
13     private String Description;
14 
15     @XStreamAlias("PicUrl")
16     @XStreamCDATA
17     private String PicUrl;
18 
19     @XStreamAlias("Url")
20     @XStreamCDATA
21     private String Url;
22 
23     public String getTitle() {
24         return Title;
25     }
26 
27     public void setTitle(String title) {
28         Title = title;
29     }
30 
31     public String getDescription() {
32         return Description;
33     }
34 
35     public void setDescription(String description) {
36         Description = description;
37     }
38 
39     public String getPicUrl() {
40         return PicUrl;
41     }
42 
43     public void setPicUrl(String picUrl) {
44         PicUrl = picUrl;
45     }
46 
47     public String getUrl() {
48         return Url;
49     }
50 
51     public void setUrl(String url) {
52         Url = url;
53     }
54 }

 

service层实现方法:

封装方法

 1 /**
 2      * 获取博客图文消息
 3      *
 4      * @param custermName
 5      * @param serverName
 6      * @param createTime
 7      * @return
 8      */
 9     private ArticlesMessage getBlogMessage(String custermName, String serverName, Long createTime) {
10         ArticlesMessage outputMsg = new ArticlesMessage();
11         outputMsg.setFromUserName(serverName);
12         outputMsg.setToUserName(custermName);
13         outputMsg.setCreateTime(createTime);
14         outputMsg.setMsgType(MsgType.NEWS.getValue());
15 
16         List<ArticlesItem> articles = new ArrayList<>();
17 
18         ArticlesItem item1 = new ArticlesItem();
19         item1.setTitle("晚天吹凉风");
20         item1.setDescription("点击进入晚天吹凉风博客");
21         item1.setPicUrl(WechatConstant.BASE_SERVER + "resources/images/wechat/a.png");
22         item1.setUrl("https://my.oschina.net/inchlifc/blog");
23         articles.add(item1);
24 
25         outputMsg.setArticles(articles);
26         outputMsg.setArticleCount(articles.size());
27 
28         return outputMsg;
29     }

 

判断如果输入数字1,返回图文消息推送

 1 // 处理接收消息
 2         ServletInputStream in = request.getInputStream();
 3         // 将POST流转换为XStream对象
 4         XStream xs = new XStream();
 5         xs = SerializeXmlUtil.createXstream();
 6         XStream.setupDefaultSecurity(xs);
 7         xs.allowTypes(new Class[]{TextMessage.class, InputMessage.class, ArticlesMessage.class});
 8         xs.processAnnotations(InputMessage.class);
 9         xs.processAnnotations(ArticlesMessage.class);
10         xs.processAnnotations(ImageMessage.class);
11         // 将指定节点下的xml节点数据映射为对象
12         xs.alias("xml", InputMessage.class);
13         // 将流转换为字符串
14         StringBuilder xmlMsg = new StringBuilder();
15         byte[] b = new byte[4096];
16         for (int n; (n = in.read(b)) != -1; ) {
17             xmlMsg.append(new String(b, 0, n, "UTF-8"));
18         }
19         logger.info("收到消息====" + xmlMsg.toString());
20         // 将xml内容转换为InputMessage对象
21         InputMessage inputMsg = (InputMessage) xs.fromXML(xmlMsg.toString());
22 
23         // 服务端
24         String servername = inputMsg.getToUserName();
25         // 客户端
26         String custermname = inputMsg.getFromUserName();
27         // 接收时间
28         long createTime = inputMsg.getCreateTime();
29         // 返回时间
30         Long returnTime = Calendar.getInstance().getTimeInMillis() / 1000;
31         //接手文本内容
32         String content = inputMsg.getContent();
33         // 取得消息类型
34         String msgType = inputMsg.getMsgType();
35 
36 if (MsgType.TEXT.getValue().equals(msgType)) {
37                 //输入1 推送博客信息
38                 if ("1".equals(content)) {
39                     logger.info("收到文本1");
40                     ArticlesMessage outputMsg = getBlogMessage(custermname, servername, returnTime);
41                     logger.info("返回博客图文消息===" + xs.toXML(outputMsg));
42                     response.getWriter().write(xs.toXML(outputMsg));
43                 }
44 }

 

运行结果:

posted @ 2018-03-27 08:52  小小渔夫  阅读(6832)  评论(7编辑  收藏  举报
友情链接: 燕归来兮 李狗蛋Blog 萌力觉醒