被動響應(yīng)多圖文消息官方文檔:
news消息
參數(shù) | 說明 |
---|---|
ToUserName | 員工UserID |
FromUserName | 企業(yè)號CorpID |
CreateTime | 消息創(chuàng)建時間(整型) |
MsgType | 消息類型,此時固定為:news |
ArticleCount | 圖文條數(shù),默認(rèn)第一條為大圖。圖文數(shù)不能超過10,否則將會無響應(yīng) |
Title | 圖文消息標(biāo)題 |
Description | 圖文消息描述 |
PicUrl | 圖片鏈接,支持JPG、PNG格式,較好的效果為大圖360*200,小圖200*200 |
Url | 點擊圖文消息跳轉(zhuǎn)鏈接 |
?
我們在項目中新增一個News類
代碼如下:
/// <summary> /// 圖文消息 /// </summary> public class ResponseNews : ResponseBase { /// <summary> /// 圖文條數(shù),默認(rèn)第一條為大圖。圖文數(shù)不能超過10,否則將會無響應(yīng) /// </summary> public int ArticleCount { get { return ArticleCollection.Count; } } public string Articles { get; set; } /// <summary> /// 圖文內(nèi)容,集合 /// </summary> public List<ArticleItem> ArticleCollection { get; set; } /// <summary> /// 構(gòu)造函數(shù),自動生成消息創(chuàng)建時間(整型),參考:APITools.GetCreateTime() /// </summary> /// <param name="toUser">員工UserID </param> /// <param name="fromUser">企業(yè)號CorpID </param> /// <param name="content">文本消息內(nèi)容 </param> public ResponseNews(string toUser, string fromUser) : base(RequestMessageType.news) { this.ToUserName = toUser; this.FromUserName = fromUser; this.CreateTime = CorpTools.GetCreateTime(); } /// <summary> /// 獲得XML文本 /// </summary> /// <returns></returns> public override string GenerateXML() { /* const string xmlformat = @" <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>2</ArticleCount> <Articles> <item> <Title><![CDATA[title1]]></Title> <Description><![CDATA[description1]]></Description> <PicUrl><![CDATA[picurl]]></PicUrl> <Url><![CDATA[url]]></Url> </item> <item> <Title><![CDATA[title]]></Title> <Description><![CDATA[description]]></Description> <PicUrl><![CDATA[picurl]]></PicUrl> <Url><![CDATA[url]]></Url> </item> </Articles> </xml>"; */ //獲得文章列表xml string xmlArticles = ""; foreach (ArticleItem item in ArticleCollection) { xmlArticles += item.GenerateXML(); } string XML = " <xml>" + String.Format("<ToUserName><![CDATA[{0}]]></ToUserName>", ToUserName) + String.Format("<FromUserName><![CDATA[{0}]]></FromUserName>", FromUserName) + String.Format("<CreateTime>{0}</CreateTime>", CreateTime) + String.Format("<MsgType><![CDATA[{0}]]></MsgType>", MsgType) + String.Format("<ArticleCount>{0}</ArticleCount>", ArticleCollection.Count) + String.Format("<Articles>{0}</Articles>", xmlArticles) + "</xml>"; return String.Format(xmlformat, ToUserName, FromUserName, CreateTime, MsgType, ""); } } /// <summary> /// 圖文消息,單圖文類 /// </summary> public class ArticleItem { /// <summary> /// 文章標(biāo)題 /// </summary> public string Title { get; set; } /// <summary> /// 文章描述 /// </summary> public string Description { get; set; } /// <summary> /// 圖片鏈接,支持JPG、PNG格式,較好的效果為大圖360*200,小圖200*200 /// </summary> public string PicUrl { get; set; } /// <summary> /// 點擊圖文消息跳轉(zhuǎn)鏈接 /// </summary> public string Url { get; set; } public string GenerateXML() { string xml = "<item>" + String.Format("<Title><![CDATA[{0}]]></Title>", Title) + String.Format("<Description><![CDATA[{0}]]></Description>", Description) + String.Format("<PicUrl><![CDATA[{0}]]></PicUrl>", PicUrl) + String.Format("<Url><![CDATA[{0}]]></Url>", Url) + "</item>"; return xml; } public string GenerateJSON() { throw new NotImplementedException(); } }
查詢返回
慎于行,敏于思!GGGGGG
本文摘自 :https://blog.51cto.com/u