1.1 接受消息
微信公眾平臺開發者文檔://mp.weixin.qq.com/wiki/index.php?title=接收普通消息,我們使用微信就是要對用戶發送的信息進行處理,這邊以接受普通消息為例,語音、圖片消息等,舉一反三可得。
當普通微信用戶向公眾賬號發消息時,微信服務器將POST消息的XML數據包到開發者填寫的URL上。文本類型的推送XML數據包結構如下:
< / xml >
我們在ashx添加下面代碼:
public void ProcessRequest(HttpContext param_context)
{
string postString = string.Empty;
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
{
using (Stream stream = HttpContext.Current.Request.InputStream)
{
Byte[] postBytes = new Byte[stream.Length];
stream.Read(postBytes, 0, (Int32)stream.Length);
postString = Encoding.UTF8.GetString(postBytes);
Handle(postString);
}
}
}
///
/// 處理信息并應答
///
private void Handle(string postStr)
{
messageHelp help = new messageHelp();
string responseContent = help.ReturnMessage(postStr);
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.Write(responseContent);
}
messageHelp是消息處理幫助類,獲取的postString是xml,格式如上,我們這邊只需要轉換成XmlDocument進行解析就行了:
//接受文本消息
public string TextHandle(XmlDocument xmldoc)
{
string responseContent = "";
XmlNode ToUserName = xmldoc.SelectSingleNode("/xml/ToUserName");
XmlNode FromUserName = xmldoc.SelectSingleNode("/xml/FromUserName");
XmlNode Content = xmldoc.SelectSingleNode("/xml/Content");
if (Content != null)
{
responseContent = string.Format(ReplyType.Message_Text,
FromUserName.InnerText,
ToUserName.InnerText,
DateTime.Now.Ticks,
"歡迎使用微信公共賬號,您輸入的內容為:
" + Content.InnerText + "\r\n點擊進入");
}
return responseContent;
}
/// <summary>
/// 普通文本消息
/// </summary>
public static string Message_Text
{
get {
return @"<xml>
<ToUserName>
<FromUserName>
<CreateTime>{2}
<MsgType>
<Content></Content>
";
}
}
上面的代碼就是接受消息,并做一些處理操作,返回消息。
1.2 發送消息(圖文、菜單事件響應)
這邊發送消息我分為三種:普通消息、圖文消息和菜單事件響應。普通消息其實上面說接受消息的時候講到了。我們先看下圖文消息和菜單事件響應,微信公眾平臺開發者文檔://mp.weixin.qq.com/wiki/14/89b871b5466b19b3efa4ada8e577d45e.html
xml格式為:
<ArticleCount>2 < / ArticleCount >
<Articles>
<item>
<Title> < ![CDATA[title1]] > < / Title >
<Description> < ![CDATA[description1]] > < / Description >
<PicUrl> < ![CDATA[picurl]] > < / PicUrl >
<Url> < ![CDATA[url]] >< / Url >
< / item >
<item>
<Title> < ![CDATA[title]] > < / Title >
<Description> < ![CDATA[description]] > < / Description >
<PicUrl> < ![CDATA[picurl]] > < / PicUrl >
<Url> < ![CDATA[url]] > < / Url >
< / item >
< / Articles >
< / xml >
//事件
public string EventHandle(XmlDocument xmldoc)
{
string responseContent = "";
XmlNode Event = xmldoc.SelectSingleNode("/xml/Event");
XmlNode EventKey = xmldoc.SelectSingleNode("/xml/EventKey");
XmlNode ToUserName = xmldoc.SelectSingleNode("/xml/ToUserName");
XmlNode FromUserName = xmldoc.SelectSingleNode("/xml/FromUserName");
if (Event != null)
{
//菜單單擊事件
if (Event.InnerText.Equals("CLICK"))
{
if (EventKey.InnerText.Equals("click_one"))//click_one
{
responseContent = string.Format(ReplyType.Message_Text,
FromUserName.InnerText,
ToUserName.InnerText,
DateTime.Now.Ticks,
"你點擊的是click_one");
}
else if (EventKey.InnerText.Equals("click_two"))//click_two
{
responseContent = string.Format(ReplyType.Message_News_Main,
FromUserName.InnerText,
ToUserName.InnerText,
DateTime.Now.Ticks,
"1",
string.Format(ReplyType.Message_News_Item, "FarSight Watch開源智能手表", "",
"//image.baidu.com/i?tn=....jpg",
"//dev.hqyj.com/products/case40.htm"));
}
}
}
return responseContent;
}
/// <summary>
/// 圖文消息主體
/// </summary>
public static string Message_News_Main
{
get
{
return @"
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>{3}</ArticleCount>
<Articles>
{4}
</Articles>
</xml> ";
}
}
需要注意的是:XmlNode Event = xmldoc.SelectSingleNode("/xml/Event")表示獲取的是事件類型,XmlNode EventKey = xmldoc.SelectSingleNode("/xml/EventKey")表示事件標示,就是我們創建菜單添加click的key,通過key我們就可以判斷出是點的哪個菜單。
還有一點是回復超鏈接,有時候在服務號會發送一些鏈接,我們打開直接就會鏈接到相關網址,只需要在回復內容中添加:<a href="// dev.hqyj.com">點擊進入</a>,就可以了。