微信公共服务平台开发(.Net 的实现)7-------发送图文消息



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

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  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>  
  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.      }  

直接在调用函数即可:
[csharp] view plain copy 在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 = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”";  
  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.              if(wx.EventKey=="P2")  
  21.                  res = sendTextMessage(wx, "你好,点击了产品2");  
  22.          }  
  23.          else  
  24.          {  
  25.              if (wx.MsgType == "text" && wx.Content == "你好")  
  26.              {  
  27.                  res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");  
  28.              }  
  29.              if (wx.MsgType == "text" && wx.Content == "图文")  
  30.              {  
  31.                  res = sendPicTextMessage(wx,"这里是一个标题","这里是摘要","http://mp.weixin.qq.com/wiki/skins/common/images/weixin_wiki_logo.png","http://www.4ugood.net");  
  32.              }  
  33.              else if (wx.MsgType == "voice")  
  34.              {  
  35.                  res = sendTextMessage(wx, wx.Recognition);  
  36.              }  
  37.              else  
  38.              {  
  39.                  res = sendTextMessage(wx, "你好,未能识别消息!");  
  40.              }  
  41.          }  
  42.   
  43.          Response.Write(res);  
  44.      }  
  45.   
  46.   
  47.   
  48.      private wxmessage GetWxMessage()  
  49.      {  
  50.          wxmessage wx = new wxmessage();  
  51.          StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);  
  52.          XmlDocument xml = new XmlDocument();  
  53.          xml.Load(str);  
  54.          wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;  
  55.          wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;  
  56.          wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;  
  57.          if (wx.MsgType.Trim() == "text")  
  58.          {  
  59.              wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;  
  60.          }  
  61.          if (wx.MsgType.Trim() == "event")  
  62.          {  
  63.              wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;  
  64.              wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;  
  65.          }  
  66.          if (wx.MsgType.Trim() == "voice")  
  67.          {  
  68.              wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;  
  69.          }  
  70.            
  71.          return wx;  
  72.      }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值