這篇文章主要介紹了asp.net微信開發中有關訊息應答的相關內容,需要的朋友可以參考下
當普通微信用戶向公眾帳號發送訊息時,微信伺服器將POST訊息的XML封包到開發者填寫的URL上。
請注意:
1、關於重試的訊息排重,建議使用msgid排重。
2、微信伺服器在五秒鐘內收不到回應會斷掉連接,並且重新發起請求,總共重試三次。假如伺服器無法保證在五秒內處理並回复,可以直接回复空串,微信伺服器不會對此作任何處理,並且不會發起重試。詳情請見「發送訊息-被動回覆訊息」。
3、為了確保更高的安全保障,開發者可以在公眾平台官網的開發者中心處設定訊息加密。開啟加密後,用戶發送的訊息會被加密,公眾號被動回覆用戶的訊息也需要加密(但開發者透過客服介面等API呼叫形式向用戶發送訊息,則不受影響)。關於訊息加解密的詳細說明,請參閱「訊息加解密說明」。
各訊息類型的推送XML封包結構如下:
文字訊息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> <MsgId>1234567890123456</MsgId> </xml>
圖片訊息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[this is a url]]></PicUrl> <MediaId><![CDATA[media_id]]></MediaId> <MsgId>1234567890123456</MsgId> </xml>
#語音訊息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1357290913</CreateTime> <MsgType><![CDATA[voice]]></MsgType> <MediaId><![CDATA[media_id]]></MediaId> <Format><![CDATA[Format]]></Format> <MsgId>1234567890123456</MsgId> </xml>
# 請注意,開啟語音辨識後,使用者每次傳送語音給公用號碼時,微信會在推送的語音訊息XML資料包中,增加一個Recongnition欄位(註:由於客戶端緩存,開發者開啟或關閉語音辨識功能,對新追蹤者立刻生效,對已關注用戶需要24小時生效。開發者可以重新關注此帳號進行測試)。開啟語音辨識後的語音XML封包如下:
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1357290913</CreateTime> <MsgType><![CDATA[voice]]></MsgType> <MediaId><![CDATA[media_id]]></MediaId> <Format><![CDATA[Format]]></Format> <Recognition><![CDATA[腾讯微信团队]]></Recognition> <MsgId>1234567890123456</MsgId> </xml>
多出的欄位中,Format為語音格式,一般為amr,Recognition為語音辨識結果,使用UTF8編碼。
視訊訊息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1357290913</CreateTime> <MsgType><![CDATA[video]]></MsgType> <MediaId><![CDATA[media_id]]></MediaId> <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> <MsgId>1234567890123456</MsgId> </xml>
小視頻訊息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1357290913</CreateTime> <MsgType><![CDATA[shortvideo]]></MsgType> <MediaId><![CDATA[media_id]]></MediaId> <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> <MsgId>1234567890123456</MsgId> </xml>
# 地理位置訊息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1351776360</CreateTime> <MsgType><![CDATA[location]]></MsgType> <Location_X>23.134521</Location_X> <Location_Y>113.358803</Location_Y> <Scale>20</Scale> <Label><![CDATA[位置信息]]></Label> <MsgId>1234567890123456</MsgId> </xml>
#連結訊息
##
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1351776360</CreateTime> <MsgType><![CDATA[link]]></MsgType> <Title><![CDATA[公众平台官网链接]]></Title> <Description><![CDATA[公众平台官网链接]]></Description> <Url><![CDATA[url]]></Url> <MsgId>1234567890123456</MsgId> </xml>
接續上篇,看ResponseXML(postString);方法如下
#
/// <summary> /// 获取用户发送的消息 /// </summary> /// <param name="postString"></param> private void ResponseXML(string postString) { //使用XMLDocument加载信息结构 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(postString); XmlElement rootElement = xmlDoc.DocumentElement;//获取文档的根 XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); //获取消息的文本类型 RequestXML requestXML = new RequestXML();//声明实例,获取各个属性并赋值 requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;//公众号 requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;//用户 requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;//创建时间 requestXML.MsgType = MsgType.InnerText;//消息类型 ///对消息的不同类型进行赋值 if (requestXML.MsgType == "text") { //赋值文本信息内容 requestXML.Content = rootElement.SelectSingleNode("Content").InnerText; } if (requestXML.MsgType.Trim() == "location") { ///赋值地理位置纬度,经度,地图缩放比例,地理位置说明 requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText; requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText; requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText; requestXML.Label = rootElement.SelectSingleNode("Label").InnerText; } if (requestXML.MsgType.Trim().ToLower() == "event") { ///赋值事件名称和事件key值 requestXML.EventName = rootElement.SelectSingleNode("Event").InnerText; requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; } if (requestXML.MsgType.Trim().ToLower() == "voice") { ///赋值语音识别结果,赋值之前一定要记得在开发者模式下,把语音识别功能开启,否则获取不到 requestXML.Recognition = rootElement.SelectSingleNode("Recognition").InnerText; } ResponseMsg(requestXML); }
語音辨識功能開啟如下:#
requestXML是我单独创建的一个类,该类声明了消息中常用的属性字段,如下:
/// <summary> /// 接收消息的实体类 /// </summary> public class RequestXML { private String toUserName = String.Empty; /// <summary> /// 本公众号 /// </summary> public String ToUserName{get;set;} /// <summary> /// 用户微信号 /// </summary> public String FromUserName{get;set;} /// <summary> /// 创建时间 /// </summary> public String CreateTime{get;set;} /// <summary> /// 信息类型 /// </summary> public String MsgType{get;set;} /// <summary> /// 信息内容 /// </summary> public String Content{get;set;} /*以下为事件类型的消息特有的属性*/ /// <summary> /// 事件名称 /// </summary> public String EventName{get;set;} /// <summary> /// 事件值 /// </summary> public string EventKey { get; set; } /*以下为图文类型的消息特有的属性*/ /// <summary> /// 图文消息的个数 /// </summary> public int ArticleCount { get; set; } /// <summary> /// 图文消息的标题 /// </summary> public string Title { get; set; } /// <summary> /// 图文消息的简介 /// </summary> public string Description { get; set; } /// <summary> /// 图文消息图片的链接地址 /// </summary> public string PicUrl { get; set; } /// <summary> /// 图文消息详情链接地址 /// </summary> public string Url { get; set; } /// <summary> /// 图文消息集合 /// </summary> public List<RequestXML> Articles { get; set;} /*以下为地理位置类型的消息特有的属性*/ /// <summary> /// 地理位置纬度 /// </summary> public String Location_X { get; set; } /// <summary> /// 地理位置经度 /// </summary> public String Location_Y { get; set; } /// <summary> /// 地图缩放比例 /// </summary> public String Scale { get; set; } /// <summary> /// 地图位置说明 /// </summary> public String Label { get; set; } /// <summary> /// 语音消息特有字段 /// </summary> public String Recognition { get; set; } }
继续关注 ResponseMsg(requestXML);方法如下
private void ResponseMsg(RequestXML requestXML) { string MsgType = requestXML.MsgType; try { //根据消息类型判断发送何种类型消息 switch (MsgType) { case "text": SendTextCase(requestXML);//发送文本消息 break; case "event"://发送事件消息 if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("subscribe")) { SendWelComeMsg(requestXML);//关注时返回的图文消息 } else if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("CLICK")) { SendEventMsg(requestXML);//发送事件消息 } break; case "voice": SendVoiceMsg(requestXML);//发送语音消息 break; case "location"://发送位置消息 SendMapMsg(requestXML); break; default: break; } } catch (Exception ex) { HttpContext.Current.Response.Write(ex.ToString()); } }
先来关注发送文本消息,SendTextCase(requestXML);//发送文本消息
/// <summary> /// 发送文本 /// </summary> /// <param name="requestXML"></param> private void SendTextCase(RequestXML requestXML) { string responseContent = FormatTextXML(requestXML.FromUserName, requestXML.ToUserName, requestXML.Content); HttpContext.Current.Response.ContentType = "text/xml"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); HttpContext.Current.Response.End(); }
FormatTextXML方法制定格式
/// <summary> /// 返回格式化的Xml格式内容 /// </summary> /// <param name="p1">公众号</param> /// <param name="p2">用户号</param> /// <param name="p3">回复内容</param> /// <returns></returns> private string FormatTextXML(string p1, string p2, string p3) { return "<xml><ToUserName><![CDATA[" + p1 + "]]></ToUserName><FromUserName><![CDATA[" + p2 + "]]></FromUserName><CreateTime>" + DateTime.Now.Subtract(new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds.ToString() + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + p3 + "]]></Content><FuncFlag>1</FuncFlag></xml>"; }
这样就能实现消息的应答,如果用户点击的按钮,如下代码:
case "event"://发送事件消息 if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("subscribe")) { SendWelComeMsg(requestXML);//关注时返回的图文消息 } else if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("CLICK")) { SendEventMsg(requestXML);//发送事件消息 } break; /// <summary> /// 发送响应事件消息 /// </summary> /// <param name="requestXML"></param> private void SendEventMsg(RequestXML requestXML) { string keyStr = requestXML.EventKey.ToString(); switch (keyStr) { case "mypay": SendPayDetails(requestXML);//发送薪资账单 break; case "tianqiyubao": SendWeaterMessage(requestXML);//发送天气预报 break; case "kaixinyixiao": SendKaiXinMessage(requestXML);//发送开心一笑结果集 break; case "updateMessage": SendUpdateMessage(requestXML);//发送修改信息链接 break; case "yuangonghuodong": SendYuanGongHuoDong(requestXML);//发送学生活动 break; case "yuangongtongzhi": SendYuanGongTongZhi(requestXML);//发送员工通知 break; case "youwenbida": SendWenti(requestXML);//发送员工提交问题链接 break; case "mywen": SendWentiList(requestXML);//发送问题列表链接 break; case "PhoneSerices": SendKeFuMessage(requestXML);//接入客服 break; default: String responseContent = String.Empty; responseContent = FormatTextXML(requestXML.FromUserName, requestXML.ToUserName,"此功能暂未开放!敬请期待!"); HttpContext.Current.Response.ContentType = "text/xml"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); HttpContext.Current.Response.End(); break; } }
SendWelComeMsg(requestXML);//关注时返回的图文消息
/// <summary> /// 发送关注时的图文消息 /// </summary> /// <param name="requestXML"></param> private void SendWelComeMsg(RequestXML requestXML) { String responseContent = String.Empty; string newdate = DateTime.Now.Subtract(new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds.ToString(); string PUrlfileName = "http://www.deqiaohr.com.cn/weixin/welcome.jpg"; responseContent = string.Format(Message_News_Main, requestXML.FromUserName, requestXML.ToUserName, newdate, "1", string.Format(Message_News_Item, "欢迎关注德桥员工服务中心", "苏州德桥人力资源创立于2002年...", PUrlfileName, "http://www.deqiaohr.com.cn/weixin/WxGsjianjie.aspx")); HttpContext.Current.Response.ContentType = "text/xml"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); HttpContext.Current.Response.End(); }
Message_News_Main 和Message_News_Item是图文消息格式化
/// <summary> /// 返回图文消息主体 /// </summary> public static string Message_News_Main { get { return @"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>{3}</ArticleCount> <Articles> {4} </Articles> </xml> "; } } /// <summary> /// 返回图文消息项 /// </summary> public static string Message_News_Item { get { return @"<item> <Title><![CDATA[{0}]]></Title> <Description><![CDATA[{1}]]></Description> <PicUrl><![CDATA[{2}]]></PicUrl> <Url><![CDATA[{3}]]></Url> </item>"; } } /// <summary> /// 发送响应语音识别结果 /// </summary> /// <param name="requestXML"></param> private void SendVoiceMsg(RequestXML requestXML) { string responseContent = FormatTextXML(requestXML.FromUserName, requestXML.ToUserName, "您刚才说的语音消息识别结果为:" + requestXML.Recognition.ToString()); HttpContext.Current.Response.ContentType = "text/xml"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); HttpContext.Current.Response.End(); }
以上是asp.net微信開發訊息應答的技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!