微信公共平臺開發-(.net實現)4--發送圖文消息

  以前說了讓微信發送給關注咱們的粉絲普通的文本信息,下面咱們來看看如何發送圖文信息,須要注意的是這裏說的是,讓微信發給咱們,而不是咱們拍個圖片發給微信處理,上傳圖片在之後的再講.下面是發送圖文消息的函數,涉及title(標題),description(摘要),picurl(圖片),連接 (url)幾個關鍵的參數:html

格式以下:(此爲多圖文形式)微信

其實格式呢就和上篇http://www.cnblogs.com/QLJ1314/p/3855371.html格式同樣。curl

 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

寫了這麼多了,相信你也能照葫蘆畫瓢接着往下進行回覆語音消息、視頻、音樂消息嘍,或者是再深一點的,相信本身,必定能成功,能夠大膽嘗試一下。ide

相關文章
相關標籤/搜索