微信公共平台開發-(.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

寫了這么多了,相信你也能照葫蘆畫瓢接着往下進行回復語音消息、視頻、音樂消息嘍,或者是再深一點的,相信自己,一定能成功,可以大膽嘗試一下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM