微信公众平台开发之发送图文消息.Net代码解析
程序员文章站
2024-02-09 19:24:22
之前我们讲过让微信发送给我们普通的文本信息,下面我们来看看如何发送图文信息,需要注意的是这里说的是,让微信发给我们,而不是我们拍个图片发给微信处理,我们上传图片在以后的章节...
之前我们讲过让微信发送给我们普通的文本信息,下面我们来看看如何发送图文信息,需要注意的是这里说的是,让微信发给我们,而不是我们拍个图片发给微信处理,我们上传图片在以后的章节介绍.下面是发送图文消息的函数,涉及title(标题),description(摘要),picurl(图片),链接(url)几个关键的参数:
protected string sendpictextmessage(msg _mode,string title,string description,string picurl,string url) { string res = string.format(@"<xml> <tousername><![cdata[{0}]]></tousername> <fromusername><![cdata[{1}]]></fromusername> <createtime>{2}</createtime> <msgtype><![cdata[news]]></msgtype> <articlecount>1</articlecount> <articles> <item> <title><![cdata[{3}]]></title> <description><![cdata[{4}]]></description> <picurl><![cdata[{5}]]></picurl> <url><![cdata[{6}]]></url> </item> </articles> </xml> ", _mode.fromusername, _mode.tousername, datetime.now,title, description, picurl, url); return res; }
直接在调用函数即可:
protected void page_load(object sender, eventargs e) { mymenu(); wxmessage wx = getwxmessage(); string res = ""; if (!string.isnullorempty(wx.eventname) && wx.eventname.trim() == "subscribe") { string content = ""; content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”"; res = sendtextmessage(wx, content); } else if (!string.isnullorempty(wx.eventname) && wx.eventname.trim() == "click") { if(wx.eventkey=="hello") res = sendtextmessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!"); if(wx.eventkey=="p1") res = sendtextmessage(wx, "你好,点击了产品1"); if(wx.eventkey=="p2") res = sendtextmessage(wx, "你好,点击了产品2"); } else { if (wx.msgtype == "text" && wx.content == "你好") { res = sendtextmessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!"); } if (wx.msgtype == "text" && wx.content == "图文") { res = sendpictextmessage(wx,"这里是一个标题","这里是摘要","http://mp.weixin.qq.com/wiki/skins/common/images/weixin_wiki_logo.png","http://www.4ugood.net"); } else if (wx.msgtype == "voice") { res = sendtextmessage(wx, wx.recognition); } else { res = sendtextmessage(wx, "你好,未能识别消息!"); } } response.write(res); } private wxmessage getwxmessage() { wxmessage wx = new wxmessage(); streamreader str = new streamreader(request.inputstream, system.text.encoding.utf8); xmldocument xml = new xmldocument(); xml.load(str); wx.tousername = xml.selectsinglenode("xml").selectsinglenode("tousername").innertext; wx.fromusername = xml.selectsinglenode("xml").selectsinglenode("fromusername").innertext; wx.msgtype = xml.selectsinglenode("xml").selectsinglenode("msgtype").innertext; if (wx.msgtype.trim() == "text") { wx.content = xml.selectsinglenode("xml").selectsinglenode("content").innertext; } if (wx.msgtype.trim() == "event") { wx.eventname = xml.selectsinglenode("xml").selectsinglenode("event").innertext; wx.eventkey = xml.selectsinglenode("xml").selectsinglenode("eventkey").innertext; } if (wx.msgtype.trim() == "voice") { wx.recognition = xml.selectsinglenode("xml").selectsinglenode("recognition").innertext; } return wx; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。