5, WeChat official account to reply to the text message

Keywords: Java xml

Description of main parameters of graphic message

Through the official wechat message interface guide, you can see the introduction to the parameters of graphic message, as shown in the following figure:

From the above figure, we can see:

1. Limit the number of text messages to 10, that is, the value of ArticleCount in the text (limit the number of text messages to 10)

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

3. The first picture size is 640 * 320, and other pictures are 80 * 80

Let's start with:

Base class for 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;
    }
}

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

Articles class in graphic message:

import com.thoughtworks.xstream.annotations.XStreamAlias;

import java.util.List;

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

ArticlesItem class in graphic 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;
    }
}

Implementation method of service layer:

Encapsulation method

 /**
     * Get blog image 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<ArticlesItem> 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 the number 1 is input, the image and text message push will be returned

// Processing received messages
        ServletInputStream in = request.getInputStream();
        // Convert POST stream to 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 an object
        xs.alias("xml", InputMessage.class);
        // Convert stream to 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("Receive message====" + xmlMsg.toString());
        // Converting xml content to InputMessage objects
        InputMessage inputMsg = (InputMessage) xs.fromXML(xmlMsg.toString());

        // Server side
        String servername = inputMsg.getToUserName();
        // Client
        String custermname = inputMsg.getFromUserName();
        // Receiving 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 information
                if ("1".equals(content)) {
                    logger.info("Received text1");
                    ArticlesMessage outputMsg = getBlogMessage(custermname, servername, returnTime);
                    logger.info("Return to blog image message===" + xs.toXML(outputMsg));
                    response.getWriter().write(xs.toXML(outputMsg));
                }
}

Operation result:

Posted by subrata on Fri, 10 Apr 2020 08:50:33 -0700