首页 > 代码库 > 《微信开发日志》之被动响应多图文消息

《微信开发日志》之被动响应多图文消息

被动响应多图文消息官方文档:

news消息

<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>
参数说明
ToUserName员工UserID
FromUserName企业号CorpID
CreateTime消息创建时间(整型)
MsgType消息类型,此时固定为:news
ArticleCount图文条数,默认第一条为大图。图文数不能超过10,否则将会无响应
Title图文消息标题
Description图文消息描述
PicUrl图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
Url点击图文消息跳转链接


 

我们在项目中新增一个News类

image

代码如下:

    /// <summary>    /// 图文消息    /// </summary>    public class ResponseNews : ResponseBase    {        /// <summary>        /// 图文条数,默认第一条为大图。图文数不能超过10,否则将会无响应        /// </summary>        public int ArticleCount        {            get            {                return ArticleCollection.Count;            }        }        public string Articles { get; set; }        /// <summary>        /// 图文内容,集合        /// </summary>        public List<ArticleItem> ArticleCollection { get; set; }        /// <summary>        /// 构造函数,自动生成消息创建时间(整型),参考:APITools.GetCreateTime()        /// </summary>        /// <param name="toUser">员工UserID </param>        /// <param name="fromUser">企业号CorpID </param>        /// <param name="content">文本消息内容  </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>        /// 文章标题        /// </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>        /// 点击图文消息跳转链接        /// </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();        }    }

查询返回

《微信开发日志》之被动响应多图文消息