微信公共平台开发-(.net实现)4--发送图文消息

  之前说了让微信发送给关注我们的粉丝普通的文本信息,下面我们来看看如何发送图文信息,需要注意的是这里说的是,让微信发给我们,而不是我们拍个图片发给微信处理,上传图片在以后的再讲.下面是发送图文消息的函数,涉及title(标题),description(摘要),picurl(图片),链接 (url)几个关键的参数:

格式如下:(此为多图文形式)

其实格式呢就和上篇http://www.cnblogs.com/QLJ1314/p/3855371.html格式一样。

 1 protected string sendPicTextMessage(Msg _mode,string title,string description,string picurl,string url)
 2     {
 3        
 4         string res = string.Format(@"<xml>
 5                                             <ToUserName><![CDATA[{0}]]></ToUserName>
 6                                             <FromUserName><![CDATA[{1}]]></FromUserName>
 7                                             <CreateTime>{2}</CreateTime>//时间,可以转成int类型,类似与时间戳
 8                                             <MsgType><![CDATA[news]]></MsgType>
 9                                             <ArticleCount>1</ArticleCount>//数量
10                                             <Articles>
11                                             <item>
12                                             <Title><![CDATA[{3}]]></Title> //图文标题
13                                             <Description><![CDATA[{4}]]></Description>//图文描述
14                                             <PicUrl><![CDATA[{5}]]></PicUrl>//图片路径
15                                             <Url><![CDATA[{6}]]></Url>//链接地址
16                                             </item>
17                                             </Articles>
18                                    </xml> ",
19            _mode.FromUserName, _mode.ToUserName, DateTime.Now,title, description, picurl, url);
20 
21         return res;
22 
23      }
View Code
 1 protected void Page_Load(object sender, EventArgs e)  
 2      {  
 3           
 4          MyMenu();  
 5          wxmessage wx = GetWxMessage();   //获取微信所需信息
 6          string res = "";  
 7   
 8          if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")   //关注事件
 9          {  
10              string content = "";  
11              content = "你好,感谢你关注QLJ1314博客";  
12              res = sendTextMessage(wx, content);  
13          }  
14          else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")  
15          {  
16              if(wx.EventKey=="Hello")  
17                  res = sendTextMessage(wx, "请说中文!");  
18              if(wx.EventKey=="P1")  
19                  res = sendTextMessage(wx, "你好,点击了我的博文1");              
20          }  
21          else  
22          {  
23              if (wx.MsgType == "text" && wx.Content == "你好")  
24              {  
25                  res = sendTextMessage(wx, "你好,感谢你关注QLJ1314博客");  
26              }  
27              if (wx.MsgType == "text" && wx.Content == "图文")   //此为图文回复
28              {  
29                  res = sendPicTextMessage(wx,"这里是一个标题","这里是摘要","http://mp.weixin.qq.com/wiki/skins/common/images/weixin_wiki_logo.png","http://www.4ugood.net");  
30              }  
31              else if (wx.MsgType == "voice")   //此为语音回复,稍后讲或者直接看开发者文档自己试试
32              {  
33                  res = sendTextMessage(wx, wx.Recognition);  
34              }  
35              else  
36              {  
37                  res = sendTextMessage(wx, "你好,未能识别消息!");  
38              }  
39          }  
40   
41          Response.Write(res);  
42      }  
View Code
 1      private wxmessage GetWxMessage()  
 2      {  
 3          wxmessage wx = new wxmessage();   //实例化微信类
 4          StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);  
 5          XmlDocument xml = new XmlDocument();  //实例化微信的xml类
 6          xml.Load(str);   //读取数据流
 7 //取所需的节点值
 8          wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;  
 9          wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;  
10          wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;  
11          if (wx.MsgType.Trim() == "text")   //文本类型
12          {  
13              wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;  
14          }  
15          if (wx.MsgType.Trim() == "event")  //事件,关注
16          {  
17              wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;  
18              wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;  
19          }  
20          if (wx.MsgType.Trim() == "voice")  //语音
21          {  
22              wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;  
23          }  
24            
25          return wx;  
26      }  
View Code

写了这么多了,相信你也能照葫芦画瓢接着往下进行回复语音消息、视频、音乐消息喽,或者是再深一点的,相信自己,一定能成功,可以大胆尝试一下。

posted @ 2014-08-04 10:49  妍珊  阅读(4403)  评论(0编辑  收藏  举报