【微信公众号开发】【6】图文消息加强
前言:
1,圖文消息參數(shù)介紹
2,注意區(qū)分圖片消息和圖文消息,圖片消息是不能帶鏈接的
3,一定要給圖文消息的Url屬性賦值。(柳峰老師說如果Url為空,用戶點擊后將會打開一個空白頁面,待驗證,我以前直接不賦值圖片是無法點擊的)
4,只有單圖文的描述才會顯示,多圖文的描述不會被顯示。
5,圖文消息的標題、描述中可以使用QQ表情和符號表情。
6,圖文消息的標題、描述中可以使用換行符。(\n)
6,圖文消息的標題、描述中不支持超文本鏈接(html的<a>標簽)。
7,圖文消息的鏈接、圖片鏈接可以使用外部域名下的資源。
8,使用指定大小的圖片。第一條圖文的圖片大小建議為640*320,其他圖文的圖片大小建議為80*80。
9,每條圖文消息的圖文建議控制在1-4條。
正文:
以下內(nèi)容完全參照柳峰老師
package org.liufeng.course.service; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.liufeng.course.message.resp.Article; import org.liufeng.course.message.resp.NewsMessage; import org.liufeng.course.message.resp.TextMessage; import org.liufeng.course.util.MessageUtil; /** * 核心服務(wù)類 * * @author liufeng * @date 2013-07-25 */ public class CoreService { /** * 處理微信發(fā)來的請求 * * @param request * @return */ public static String processRequest(HttpServletRequest request) { String respMessage = null; try { // xml請求解析 Map<String, String> requestMap = MessageUtil.parseXml(request); // 發(fā)送方帳號(open_id) String fromUserName = requestMap.get("FromUserName"); // 公眾帳號 String toUserName = requestMap.get("ToUserName"); // 消息類型 String msgType = requestMap.get("MsgType"); // 默認回復(fù)此文本消息 TextMessage textMessage = new TextMessage(); textMessage.setToUserName(fromUserName); textMessage.setFromUserName(toUserName); textMessage.setCreateTime(new Date().getTime()); textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT); textMessage.setFuncFlag(0); // 由于href屬性值必須用雙引號引起,這與字符串本身的雙引號沖突,所以要轉(zhuǎn)義 textMessage.setContent("歡迎訪問<a href=\"http://blog.csdn.net/lyq8479\">柳峰的博客</a>!"); // 將文本消息對象轉(zhuǎn)換成xml字符串 respMessage = MessageUtil.textMessageToXml(textMessage); // 文本消息 if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) { // 接收用戶發(fā)送的文本消息內(nèi)容 String content = requestMap.get("Content"); // 創(chuàng)建圖文消息 NewsMessage newsMessage = new NewsMessage(); newsMessage.setToUserName(fromUserName); newsMessage.setFromUserName(toUserName); newsMessage.setCreateTime(new Date().getTime()); newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS); newsMessage.setFuncFlag(0); List<Article> articleList = new ArrayList<Article>(); // 單圖文消息 if ("1".equals(content)) { Article article = new Article(); article.setTitle("微信公眾帳號開發(fā)教程Java版"); article.setDescription("柳峰,80后,微信公眾帳號開發(fā)經(jīng)驗4個月。為幫助初學(xué)者入門,特推出此系列教程,也希望借此機會認識更多同行!"); article.setPicUrl("http://0.xiaoqrobot.duapp.com/images/avatar_liufeng.jpg"); article.setUrl("http://blog.csdn.net/lyq8479"); articleList.add(article); // 設(shè)置圖文消息個數(shù) newsMessage.setArticleCount(articleList.size()); // 設(shè)置圖文消息包含的圖文集合 newsMessage.setArticles(articleList); // 將圖文消息對象轉(zhuǎn)換成xml字符串 respMessage = MessageUtil.newsMessageToXml(newsMessage); } // 單圖文消息---不含圖片 else if ("2".equals(content)) { Article article = new Article(); article.setTitle("微信公眾帳號開發(fā)教程Java版"); // 圖文消息中可以使用QQ表情、符號表情 article.setDescription("柳峰,80后," + emoji(0x1F6B9) + ",微信公眾帳號開發(fā)經(jīng)驗4個月。為幫助初學(xué)者入門,特推出此系列連載教程,也希望借此機會認識更多同行!\n\n目前已推出教程共12篇,包括接口配置、消息封裝、框架搭建、QQ表情發(fā)送、符號表情發(fā)送等。\n\n后期還計劃推出一些實用功能的開發(fā)講解,例如:天氣預(yù)報、周邊搜索、聊天功能等。"); // 將圖片置為空 article.setPicUrl(""); article.setUrl("http://blog.csdn.net/lyq8479"); articleList.add(article); newsMessage.setArticleCount(articleList.size()); newsMessage.setArticles(articleList); respMessage = MessageUtil.newsMessageToXml(newsMessage); } // 多圖文消息 else if ("3".equals(content)) { Article article1 = new Article(); article1.setTitle("微信公眾帳號開發(fā)教程\n引言"); article1.setDescription(""); article1.setPicUrl("http://0.xiaoqrobot.duapp.com/images/avatar_liufeng.jpg"); article1.setUrl("http://blog.csdn.net/lyq8479/article/details/8937622"); Article article2 = new Article(); article2.setTitle("第2篇\n微信公眾帳號的類型"); article2.setDescription(""); article2.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg"); article2.setUrl("http://blog.csdn.net/lyq8479/article/details/8941577"); Article article3 = new Article(); article3.setTitle("第3篇\n開發(fā)模式啟用及接口配置"); article3.setDescription(""); article3.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg"); article3.setUrl("http://blog.csdn.net/lyq8479/article/details/8944988"); articleList.add(article1); articleList.add(article2); articleList.add(article3); newsMessage.setArticleCount(articleList.size()); newsMessage.setArticles(articleList); respMessage = MessageUtil.newsMessageToXml(newsMessage); } // 多圖文消息---首條消息不含圖片 else if ("4".equals(content)) { Article article1 = new Article(); article1.setTitle("微信公眾帳號開發(fā)教程Java版"); article1.setDescription(""); // 將圖片置為空 article1.setPicUrl(""); article1.setUrl("http://blog.csdn.net/lyq8479"); Article article2 = new Article(); article2.setTitle("第4篇\n消息及消息處理工具的封裝"); article2.setDescription(""); article2.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg"); article2.setUrl("http://blog.csdn.net/lyq8479/article/details/8949088"); Article article3 = new Article(); article3.setTitle("第5篇\n各種消息的接收與響應(yīng)"); article3.setDescription(""); article3.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg"); article3.setUrl("http://blog.csdn.net/lyq8479/article/details/8952173"); Article article4 = new Article(); article4.setTitle("第6篇\n文本消息的內(nèi)容長度限制揭秘"); article4.setDescription(""); article4.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg"); article4.setUrl("http://blog.csdn.net/lyq8479/article/details/8967824"); articleList.add(article1); articleList.add(article2); articleList.add(article3); articleList.add(article4); newsMessage.setArticleCount(articleList.size()); newsMessage.setArticles(articleList); respMessage = MessageUtil.newsMessageToXml(newsMessage); } // 多圖文消息---最后一條消息不含圖片 else if ("5".equals(content)) { Article article1 = new Article(); article1.setTitle("第7篇\n文本消息中換行符的使用"); article1.setDescription(""); article1.setPicUrl("http://0.xiaoqrobot.duapp.com/images/avatar_liufeng.jpg"); article1.setUrl("http://blog.csdn.net/lyq8479/article/details/9141467"); Article article2 = new Article(); article2.setTitle("第8篇\n文本消息中使用網(wǎng)頁超鏈接"); article2.setDescription(""); article2.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg"); article2.setUrl("http://blog.csdn.net/lyq8479/article/details/9157455"); Article article3 = new Article(); article3.setTitle("如果覺得文章對你有所幫助,請通過博客留言或關(guān)注微信公眾帳號xiaoqrobot來支持柳峰!"); article3.setDescription(""); // 將圖片置為空 article3.setPicUrl(""); article3.setUrl("http://blog.csdn.net/lyq8479"); articleList.add(article1); articleList.add(article2); articleList.add(article3); newsMessage.setArticleCount(articleList.size()); newsMessage.setArticles(articleList); respMessage = MessageUtil.newsMessageToXml(newsMessage); } } } catch (Exception e) { e.printStackTrace(); } return respMessage; } /** * emoji表情轉(zhuǎn)換(hex -> utf-16) * * @param hexEmoji * @return */ public static String emoji(int hexEmoji) { return String.valueOf(Character.toChars(hexEmoji)); } }a)用戶發(fā)送1,回復(fù)單圖文消息。參考代碼68~81行,運行效果如下:
?
b)用戶發(fā)送2,回復(fù)單圖文消息---不含圖片。參考代碼82~96行,運行效果如下:
說明:圖文消息的標題、描述是可以包含QQ表情、符號表情的。
?
c)用戶發(fā)送3,回復(fù)多圖文消息。參考代碼97~123行,運行效果如下:
說明:對于多圖文消息,描述不會被顯示,可以在標題使用換行符,使得顯示更加美觀。
?
d)用戶發(fā)送4,回復(fù)多圖文消息---首條消息不含圖片。參考代碼124~158行,運行效果如下:
?
e)用戶發(fā)送5,回復(fù)多圖文消息---最后一條消息不含圖片。參考代碼159~186行,運行效果如下:
轉(zhuǎn)載于:https://www.cnblogs.com/huashengweilong/p/7716931.html
總結(jié)
以上是生活随笔為你收集整理的【微信公众号开发】【6】图文消息加强的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 发票、司法、扶贫,政务应用已成区块链率先
- 下一篇: 实习小结