微信公眾號開發之回復圖文消息(十一)


有時候我們希望用戶點擊我們的菜單時候,微信公眾號給他回復我們自定義的圖文消息,先看開發文檔接口

回復圖文消息

<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>1</ArticleCount> <Articles> <item> <Title><![CDATA[title1]]></Title> <Description><![CDATA[description1]]></Description> <PicUrl><![CDATA[picurl]]></PicUrl> <Url><![CDATA[url]]></Url> </item> </Articles> </xml> 
參數 是否必須 說明
ToUserName 接收方帳號(收到的OpenID)
FromUserName 開發者微信號
CreateTime 消息創建時間 (整型)
MsgType 消息類型,圖文為news
ArticleCount 圖文消息個數;當用戶發送文本、圖片、視頻、圖文、地理位置這五種消息時,開發者只能回復1條圖文消息;其余場景最多可回復8條圖文消息
Articles 圖文消息信息,注意,如果圖文數超過限制,則將只發限制內的條數
Title 圖文消息標題
Description 圖文消息描述
PicUrl 圖片鏈接,支持JPG、PNG格式,較好的效果為大圖360*200,小圖200*200
Url 點擊圖文消息跳轉鏈接

先定義我們的發送體POJO,一個是Article,一個是NewsMessage

Article.java 
package com.xu.wemall.pojo.message; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; /**  *   * @Description: 圖文model  * @Parameters:   * @Return:   * @Create Date:   * @Version: V1.00  * @author: 來日可期  */ @Data @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = false) public class Article { //圖文消息名稱 private String Title; //圖文消息描述 private String Description; //圖片鏈接,支持JPG、PNG格式,較好的效果為大圖640像素*320像素,小圖80像素*80像素 private String PicUrl; //點擊圖文消息跳轉鏈接 private String Url; } 
NewsMessage.java
package com.xu.wemall.pojo.message; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import java.util.List; /** * * @Description: 圖文消息 * @Parameters: * @Return: * @Create Date: * @Version: V1.00 * @author: 來日可期 */ @Data @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = false) public class NewsMessage extends BaseMessage { //圖文消息個數,限制為10條以內 private int ArticleCount; //多條圖文消息信息,默認第一個item為大圖 private List<Article> Articles; } 

定義一個方法回復圖文消息

/** * 回復文本圖片 * @param toUserName * @param fromUserName * @param articles * @return */ public String replyForArticles(String toUserName, String fromUserName, List<Article> articles) throws Exception{ log.info("這是圖文消息回復!"); NewsMessage newsMessage = new NewsMessage(); //必填 newsMessage.setFromUserName(toUserName); //必填 newsMessage.setToUserName(fromUserName); //必填 newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS); //必填 newsMessage.setCreateTime( LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()); //當用戶發送文本、圖片、視頻、圖文、地理位置這五種消息時,開發者只能回復1條圖文消息 newsMessage.setArticleCount(1); if (!articles.isEmpty()) { newsMessage.setArticles(articles); String xmlString = MessageUtil.newsMessageToXml(newsMessage); log.info(xmlString); return xmlString; } return null; }

這里有個MessageUtil.newsMessageToXml(newsMessage)方法

/** * @param newsMessage * @return xml * @Description: 圖文消息對象轉換成xml * @date 2016-12-01 */ public static String newsMessageToXml(NewsMessage newsMessage) { xstream.alias("xml", newsMessage.getClass()); xstream.alias("item", new Article().getClass()); return xstream.toXML(newsMessage); }

現在測試一下我們的代碼,我們需要在我們的菜單里觸發我們的方法,我們這里采用

如果需要重新生成菜單,請重新生成一次,然后我們在微信接入的核心controller中寫我們的響應代碼

package com.xu.wemall.controller.weixin; import com.alibaba.fastjson.JSONObject; import com.xu.wemall.commons.utils.CheckUtil; import com.xu.wemall.components.weixin.MessageUtil; import com.xu.wemall.components.weixin.WeiXinUserUtil; import com.xu.wemall.pojo.message.Article; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * 類名稱: LoginController * 類描述: 與微信對接登陸驗證 * * @author RonnieXu * 創建時間:2017年12月5日上午10:52:13 */ @Slf4j @RestController @Api(tags = "接入驗證接口") @RequestMapping(value = "/weChart") public class WeiXinController { @Autowired private WeiXinUserUtil weiXinUserUtil; @Autowired private MessageUtil messageUtil; @RequestMapping(value = "/connect", method = RequestMethod.GET) public String connect(@RequestParam(value = "signature") String signature, @RequestParam(value = "timestamp") String timestamp, @RequestParam(value = "nonce") String nonce, @RequestParam(value = "echostr") String echostr) { log.info("-----開始校驗簽名-----"); PrintWriter out = null; if (CheckUtil.checkSignature(signature, timestamp, nonce)) { log.info("-----簽名校驗通過-----"); return echostr; } else { log.info("-----校驗簽名失敗-----"); return null; } } @RequestMapping(value = "connect", method = RequestMethod.POST) public String dopost(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setCharacterEncoding("utf-8"); //將微信請求xml轉為map格式,獲取所需的參數 Map<String, String> map = MessageUtil.parseXml(request); String ToUserName = map.get("ToUserName"); String FromUserName = map.get("FromUserName"); String MsgType = map.get("MsgType"); String Content = map.get("Content"); String Event = map.get("Event"); String EventKey = map.get("EventKey"); if(MessageUtil.REQ_MESSAGE_TYPE_EVENT.equals(MsgType)){ if(MessageUtil.EVENT_TYPE_SUBSCRIBE.equals(Event)){ String xmlString = messageUtil.subscribeForText(ToUserName,FromUserName); //關注了公眾號,調用接口獲得用戶的詳細信息並保存到后台 JSONObject jsonObject = weiXinUserUtil.handdleWeixinUserInfo(FromUserName); log.info("獲取用戶的詳細信息:{}",jsonObject.toJSONString()); return xmlString; }else if(MessageUtil.EVENT_TYPE_UNSUBSCRIBE.equals(Event)){ String xmlString = messageUtil.unsubscribeForText(ToUserName,FromUserName); return xmlString; }else if(MessageUtil.EVENT_TYPE_SCAN.equals(Event)){ JSONObject jsonObject = weiXinUserUtil.handdleWeixinUserInfo(FromUserName); log.info("獲取用戶的詳細信息:{}",jsonObject.toJSONString()); } } //處理文本類型,實現輸入1,回復相應的封裝的內容 if (MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(MsgType)) { String xmlString = messageUtil.replyForText(ToUserName,FromUserName,"你發送的是:" + Content); log.info(xmlString); return xmlString; } if (MessageUtil.REQ_MESSAGE_TYPE_IMAGE.equals(MsgType)) { String filePath = "C:\\Users\\RonnieXu\\Pictures\\2.jpg"; String xmlString = messageUtil.replyForImage(ToUserName,FromUserName,filePath); return xmlString; } if ("1".equals(EventKey)) { List<Article> articles = new ArrayList<>(); Article article = new Article(); article.setTitle("Hello, Ronnie"); article.setDescription("這是一條描述,這是一條描述"); article.setPicUrl("https://www.baidu.com/img/superlogo_c4d7df0a003d3db9b65e9ef0fe6da1ec.png?where=super"); article.setUrl("https://www.baidu.com/"); articles.add(article); String xmlString = messageUtil.replyForArticles(ToUserName,FromUserName,articles); return xmlString; } return null; } } 

點擊我們的微信公眾號子菜單,觸發我們的方法,可以看到我們收到了一個titile是“Hello,Ronnie”的圖文消息

點擊這個圖文消息,我們就進入我們設置的URL頁面,這里我們設置是百度首頁,呵呵呵(捂臉中)

今天的內容到此為止,謝謝觀看,下回再見!

 

如果您覺得此文有幫助,可以小小打賞一下,持續更新更有動力喲!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM