微信公众号开发之回复图文消息java代码
作者:小小渔夫 时间:2022-11-24 00:56:19
微信公众号开发之回复图文消息,供大家参考,具体内容如下
图文消息的主要参数说明
通过微信官方的消息接口指南,可以看到对图文消息的参数介绍,如下图所示:
从上图可以了解到:
1、图文消息的个数限制为10,也就是图文中ArticleCount的值(图文消息的个数,限制在10条以内)
2、对于图文消息,第一条图文的图片显示为大图,其他图文的图片显示为小图。
3、第一条图文的图片大小建议为640*320,其他图文的图片建议为80*80
下面开始实现:
请求消息的基类:
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;
}
}
图文消息类:
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<ArticlesItem> Articles;
public int getArticleCount() {
return ArticleCount;
}
public void setArticleCount(int articleCount) {
ArticleCount = articleCount;
}
public List<ArticlesItem> getArticles() {
return Articles;
}
public void setArticles(List<ArticlesItem> articles) {
Articles = articles;
}
}
图文消息中的Articles类:
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.util.List;
@XStreamAlias("Articles")
public class Articles {
private List<ArticlesItem> Articles;
}
图文消息中的ArticlesItem类:
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层实现方法:
封装方法
/**
* 获取博客图文消息
*
* @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<ArticlesItem> articles = new ArrayList<>();
ArticlesItem item1 = new ArticlesItem();
item1.setTitle("晚天吹凉风");
item1.setDescription("点击进入晚天吹凉风博客");
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;
}
判断如果输入数字1,返回图文消息推送
// 处理接收消息
ServletInputStream in = request.getInputStream();
// 将POST流转换为XStream对象
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);
// 将指定节点下的xml节点数据映射为对象
xs.alias("xml", InputMessage.class);
// 将流转换为字符串
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("收到消息====" + xmlMsg.toString());
// 将xml内容转换为InputMessage对象
InputMessage inputMsg = (InputMessage) xs.fromXML(xmlMsg.toString());
// 服务端
String servername = inputMsg.getToUserName();
// 客户端
String custermname = inputMsg.getFromUserName();
// 接收时间
long createTime = inputMsg.getCreateTime();
// 返回时间
Long returnTime = Calendar.getInstance().getTimeInMillis() / 1000;
//接手文本内容
String content = inputMsg.getContent();
// 取得消息类型
String msgType = inputMsg.getMsgType();
if (MsgType.TEXT.getValue().equals(msgType)) {
//输入1 推送博客信息
if ("1".equals(content)) {
logger.info("收到文本1");
ArticlesMessage outputMsg = getBlogMessage(custermname, servername, returnTime);
logger.info("返回博客图文消息===" + xs.toXML(outputMsg));
response.getWriter().write(xs.toXML(outputMsg));
}
}
运行结果:
来源:https://www.cnblogs.com/PreachChen/archive/2018/03/27/8655394.html
标签:java,微信,图文消息
0
投稿
猜你喜欢
Java实现定时任务的方法详解
2022-12-30 04:14:00
Android自动编辑文本框(AutoCompleteTextView)使用方法详解
2023-02-05 18:17:53
ASM的tree api对匿名线程的hook操作详解
2022-07-16 15:03:30
Linux环境g++编译GDAL动态库操作方法
2022-02-23 09:41:45
C#/VB.NET中从 PDF 文档中提取所有表格
2023-04-07 09:11:03
java高并发写入用户信息到数据库的几种方法
2023-07-09 08:45:49
Java 的 FileFilter文件过滤与readline读行操作实例代码
2022-04-09 07:22:53
Android 手写热修复dex实例详解
2022-11-06 08:01:40
springboot中使用FastJson解决long类型在js中失去精度的问题
2022-04-05 05:15:27
Android 登录Web 时对cookie 处理
2022-05-08 20:16:11
C++实现LeetCode(169.求大多数)
2023-09-04 08:03:47
C#操作CSV文件类实例
2023-04-29 13:49:44
JavaWeb使用POI操作Excel文件实例
2022-06-12 18:19:30
Android 自定义返回按钮的实例详解
2023-07-04 01:13:43
Android 文件操作详解及简单实例
2021-10-29 06:23:01
C#7.0中新特性汇总
2023-06-24 18:29:13
java8 stream 如何打印数据元素
2022-08-20 18:40:02
Android中Bitmap用法实例分析
2023-03-21 11:06:31
kettle中使用js调用java类的方法
2022-05-09 00:06:31
SpringBoot如何访问html和js等静态资源配置
2023-08-17 08:07:43