WeChat public account realizes reply to graphic messages

Description of the main parameters of the graphic message

Through WeChat's official message interface guide, you can see the parameter introduction of graphic messages, as shown in the following figure:

It can be seen from the above figure that:

1. The number of graphic messages is limited to 10, which is the value of ArticleCount in the graphic (the number of graphic messages is limited to 10)

2. For image and text messages, the image of the first image and text is displayed as a large image, and the images of other images and texts are displayed as small images.

3. The picture size of the first picture and text is recommended to be 640*320, and the pictures of other pictures and texts are recommended to be 80*80

Let's start implementing:

Base class for request messages:

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;
    }
}

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<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;
    }
}

The Articles class in the text message:

import com.thoughtworks.xstream.annotations.XStreamAlias;

import java.util.List;
@XStreamAlias("Articles")
public class Articles {
    private List<ArticlesItem> Articles;
}

The ArticlesItem class in the text 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:

Packaging method

 /**
     * 获取博客图文消息
     *
     * @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;
    }

Judging that if the number 1 is entered, return to the push text message

// 处理接收消息
        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));
                }
}

operation result:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324996805&siteId=291194637