這篇文章將為大家詳細講解有關.Net如何實現(xiàn)微信公眾平臺開發(fā)接口“信息回復”,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在策勒等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供網(wǎng)站制作、成都做網(wǎng)站 網(wǎng)站設計制作按需求定制制作,公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,成都品牌網(wǎng)站建設,成都營銷網(wǎng)站建設,外貿(mào)營銷網(wǎng)站建設,策勒網(wǎng)站建設費用合理。
對于每一個POST請求,開發(fā)者在響應包(Get)中返回特定XML結(jié)構(gòu),對該消息進行響應(現(xiàn)支持回復文本、圖片、圖文、語音、視頻、音樂)。請注意,回復圖片等多媒體消息時需要預先上傳多媒體文件到微信服務器,只支持認證服務號。
今天說說下面三個
1、關注回復
2、自動回復
3、關鍵字回復
一、關注回復,自動默認回復
所謂關注回復,就是當關注著搜到公眾號的時候,點擊關注之后,微信返回給用戶的信息,具體的實現(xiàn)辦法
自動默認回復,就是無論你發(fā)送什么信息,如果沒有特別處理,系統(tǒng)默認回復的信息。
接收微信的信息和發(fā)送信息,都是xml格式的,具體在開發(fā)文檔中都有具體的說明的,現(xiàn)在就來說說如果實現(xiàn)微信信息的處理和應答。
1、先把預先設置好的回復信息保存到數(shù)據(jù)庫表里面
CREATE TABLE [dbo].[w_reply]( [reply_id] [int] IDENTITY(1,1) NOT NULL, [reply_text] [varchar](max) NULL, [reply_type] [varchar](50) NULL, [article_id] [int] NULL, [wechat_id] [int] NULL, [reply_fangshi] [int] NULL, CONSTRAINT [PK_w_reply] PRIMARY KEY CLUSTERED ( [reply_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
wechatapi.aspx頁面處理下面信息
2、接收微信發(fā)過來的信息
#region 接收微信消息 ////// 接收微信信息 /// private void RequestMsg() { //接收信息流 Stream requestStream = System.Web.HttpContext.Current.Request.InputStream; byte[] requestByte = new byte[requestStream.Length]; requestStream.Read(requestByte, 0, (int)requestStream.Length); //轉(zhuǎn)換成字符串 string requestStr = Encoding.UTF8.GetString(requestByte); if (!string.IsNullOrEmpty(requestStr)) { //封裝請求類到xml文件中 XmlDocument requestDocXml = new XmlDocument(); requestDocXml.LoadXml(requestStr); XmlElement rootElement = requestDocXml.DocumentElement; XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); //將XML文件封裝到實體類requestXml中 RequestXml requestXml = new RequestXml(); requestXml.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;//開發(fā)者微信號 requestXml.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;//發(fā)送方微信號 requestXml.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;//消息發(fā)送信息 requestXml.MsgType = MsgType.InnerText; //獲取接收信息的類型 switch (requestXml.MsgType) { //接收普通信息 case "text"://文本信息 requestXml.Content = rootElement.SelectSingleNode("Content").InnerText; break; case "image"://圖片信息 requestXml.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText; break; case "location"://地理位置信息 requestXml.Location_X = rootElement.SelectSingleNode("Location_X").InnerText; requestXml.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText; break; //接收事件推送 //大概包括有:關注/取消關注事件、掃描帶參數(shù)二維碼事件、上報地理位置事件、自定義菜單事件、點擊菜單拉取消息時的事件推送、點擊菜單跳轉(zhuǎn)鏈接時的事件推送 case "event": requestXml.Event = rootElement.SelectSingleNode("Event").InnerText; requestXml.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; break; } string selday = "0"; int hh = selday == "0" ? 60 : int.Parse(selday) * 24 * 60; //將發(fā)送方和接收方寫入cookie中,后期使用 CookieHelper.WriteCookie("WeChatFrom", "ToUserName", requestXml.ToUserName, hh); CookieHelper.WriteCookie("WeChatFrom", "FromUserName", requestXml.FromUserName, hh); //回復消息 ResponseMsg(requestXml); } } #endregion 接收微信消息
3、回復信息
#region 回復消息(微信信息返回) ////// 回復消息(微信信息返回) /// /// private void ResponseMsg(RequestXml requestXml) { string resXml = ""; string WeChat_Key = Request.QueryString["key"]; try { DataTable dtWeChat = wechatdal.GetList("wechat_key='" + WeChat_Key + "'").Tables[0]; if (dtWeChat.Rows.Count > 0) { replyset.User_ID = dtWeChat.Rows[0]["user_id"].ToString(); replyset.WeChat_ID = dtWeChat.Rows[0]["wechat_id"].ToString(); replyset.WeChat_Type = dtWeChat.Rows[0]["wechat_type"].ToString(); replyset.WeChat_Name = dtWeChat.Rows[0]["wechat_name"].ToString(); switch (requestXml.MsgType) { //當收到文本信息的時候回復信息 case "text": resXml = replyset.GetKeyword(requestXml.FromUserName, requestXml.ToUserName, requestXml.Content); break; //當接收推送事件時回復的信息 case "event": switch (requestXml.Event) { //關注的時候回復信息 case "subscribe": resXml = replyset.GetSubscribe(requestXml.FromUserName, requestXml.ToUserName); break; //自定義菜單的時候回復信息 case "CLICK": resXml = replyset.GetMenuClick(requestXml.FromUserName, requestXml.ToUserName, requestXml.EventKey); break; } break; } } } catch (Exception ex) { Writebug("異常:" + ex.Message + "Struck:" + ex.StackTrace.ToString()); } //發(fā)送xml格式的信息到微信中 Response.Write(resXml); Response.End(); } #endregion 回復消息(微信信息返回)
加載wechatapi.aspx的load時間
protected void Page_Load(object sender, EventArgs e) { if (Request.HttpMethod.ToLower() == "post") { RequestMsg(); } else { //微信通過get請求驗證api接口 CheckWeChat(); } }
reply.cs
public class replyset { public string hostUrl = "http://" + HttpContext.Current.Request.Url.Authority; //域名 public string upfileurl = "http://file.api.weixin.qq.com/cgi-bin/media/upload"; public string baiduImg = "http://api.map.baidu.com/staticimage?center={0},{1}&width=700&height=300&zoom=11"; public string User_ID = ""; public string WeChat_ID = ""; public string WeChat_Type = ""; public string WeChat_Name = ""; w_caidan_dal caidandal = new w_caidan_dal(); w_reply_dal replydal = new w_reply_dal(); w_article_dal articledal = new w_article_dal(); w_keyword_dal keyworddal = new w_keyword_dal(); w_vlimg_dal vlimgdal = new w_vlimg_dal(); w_vlimg_model vlimgmodel = new w_vlimg_model(); w_images_dal imagesdal = new w_images_dal(); common wxCommand = new common(); JsonOperate JsonOperate = new JsonOperate(); JavaScriptSerializer Jss = new JavaScriptSerializer(); public replyset() { } #region 關注回復 ////// 關注的時候回復 /// /// /// ///public string GetSubscribe(string FromUserName, string ToUserName) { string resXml = ""; string sqlWhere = !string.IsNullOrEmpty(WeChat_ID) ? "WeChat_ID=" + WeChat_ID + " and reply_fangshi=2" : ""; DataTable dtSubscribe = replydal.GetRandomList(sqlWhere, "1").Tables[0]; if (dtSubscribe.Rows.Count > 0) { string article_id = dtSubscribe.Rows[0]["article_id"].ToString(); string reply_type = dtSubscribe.Rows[0]["reply_type"].ToString(); string reply_text = dtSubscribe.Rows[0]["reply_text"].ToString(); if (reply_type == "text") { resXml = " "; } else { resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID); } } return resXml; } #endregion 關注回復 #region 自動回復 /// " + ConvertDateTimeInt(DateTime.Now) + " 0 /// 自動默認回復 /// /// /// /// /// ///public string GetDefault(string FromUserName, string ToUserName, string WeChat_ID, string User_ID) { string resXml = ""; string sqlWhere = !string.IsNullOrEmpty(WeChat_ID) ? "WeChat_ID=" + WeChat_ID + " and reply_fangshi=1" : ""; //獲取保存的默認回復設置信息 DataTable dtDefault = replydal.GetRandomList(sqlWhere, "1").Tables[0]; if (dtDefault.Rows.Count > 0) { string article_id = dtDefault.Rows[0]["article_id"].ToString(); string reply_type = dtDefault.Rows[0]["reply_type"].ToString(); string reply_text = dtDefault.Rows[0]["reply_text"].ToString(); //如果選擇的是文本 if (reply_type == "text") { resXml = " "; } else { //返回素材(圖文列表) resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID); } } return resXml; } #endregion 默認回復 #region 關鍵字回復 /// " + ConvertDateTimeInt(DateTime.Now) + " 0 /// 關鍵字回復 /// /// /// /// ///public string GetKeyword(string FromUserName, string ToUserName, string Content) { string resXml = ""; string sqlWhere = "wechat_id=" + WeChat_ID + " and keyword_name='" + Content+"'"; DataTable dtKeyword = keyworddal.GetList(sqlWhere).Tables[0]; if (dtKeyword.Rows.Count > 0) { dtKeyword = keyworddal.GetRandomList(sqlWhere, "1").Tables[0]; if (dtKeyword.Rows.Count > 0) { string article_id = dtKeyword.Rows[0]["article_id"].ToString(); string keyword_type = dtKeyword.Rows[0]["keyword_type"].ToString(); string keyword_text = dtKeyword.Rows[0]["keyword_text"].ToString(); switch (keyword_type) { case "text": resXml = " "; break; case "news": resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID); break; } } } else { resXml = GetDefault(FromUserName, ToUserName, WeChat_ID, User_ID); } return resXml; } #endregion 關鍵字回復 #region 菜單單擊 /// " + ConvertDateTimeInt(DateTime.Now) + " 0 /// 菜單點擊事件回復信息 /// /// /// /// ///public string GetMenuClick(string FromUserName, string ToUserName, string EventKey) { string resXml = ""; string sqlWhere = "wechat_id=" + WeChat_ID + " and caidan_key='" + EventKey + "'"; WriteTxt(sqlWhere); try { DataTable dtMenu = caidandal.GetList(sqlWhere).Tables[0]; if (dtMenu.Rows.Count > 0) { string article_id = dtMenu.Rows[0]["article_id"].ToString(); string caidan_retype = dtMenu.Rows[0]["caidan_retype"].ToString(); string caidan_retext = dtMenu.Rows[0]["caidan_retext"].ToString(); switch (caidan_retype) { case "text": resXml = " "; break; case "news": resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID); break; } } } catch (Exception ex) { WriteTxt("異常:" + ex.Message + "Struck:" + ex.StackTrace.ToString()); } return resXml; } #endregion 菜單單擊 #region 獲取素材 /// " + ConvertDateTimeInt(DateTime.Now) + " 0 /// 獲取素材 /// /// /// /// /// ///public string GetArticle(string FromUserName, string ToUserName, string Article_ID, string User_ID) { string resXml = ""; DataTable dtArticle = articledal.GetList("article_id=" + Article_ID + " OR article_layid=" + Article_ID).Tables[0]; if (dtArticle.Rows.Count > 0) { resXml = " "; } return resXml; } #endregion 獲取圖文列表 #region 通用方法 /// " + ConvertDateTimeInt(DateTime.Now) + " " + dtArticle.Rows.Count + " "; foreach (DataRow Row in dtArticle.Rows) { string article_title = Row["article_title"].ToString(); string article_description = Row["article_description"].ToString(); string article_picurl = Row["article_picurl"].ToString(); string article_url = Row["article_url"].ToString(); string article_type = Row["article_type"].ToString(); switch (article_type) { case "Content": article_url = hostUrl + "/web/wechat/api/article.aspx?aid=" + Row["Article_ID"].ToString(); break; case "Href": article_url = Row["article_url"].ToString(); break; } if (string.IsNullOrEmpty(article_url)) { article_url = hostUrl + "/web/wechat/api/article.aspx?aid=" + Row["Article_ID"].ToString(); } article_url += (article_url.IndexOf("uid=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "uid=" + User_ID); article_url += (article_url.IndexOf("wxid=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "wxid=" + FromUserName); article_url += (article_url.IndexOf("wxref=") > -1 ? "" : (article_url.IndexOf("?") > -1 ? "&" : "?") + "wxref=mp.weixin.qq.com"); resXml += " - "; } resXml += "
1 /// unix時間轉(zhuǎn)換為datetime /// /// ///private DateTime UnixTimeToTime(string timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = long.Parse(timeStamp + "0000000"); TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); } /// /// datetime轉(zhuǎn)換為unixtime /// /// ///private int ConvertDateTimeInt(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); return (int)(time - startTime).TotalSeconds; } /// /// 記錄bug,以便調(diào)試 /// ///public bool WriteTxt(string str) { try { FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("Log/wxbugLog.txt"), FileMode.Append); StreamWriter sw = new StreamWriter(fs); //開始寫入 sw.WriteLine(str); //清空緩沖區(qū) sw.Flush(); //關閉流 sw.Close(); fs.Close(); } catch (Exception) { return false; } return true; } #endregion 通用方法 } }
二、關鍵字回復
關鍵字的回復也很簡單,我們先把對應的關鍵字和返回的信息都設置好,然后根據(jù)接收的信息中是否存在設置好的關鍵詞來返回對應的信息即可
1、設置關鍵詞(這里就不在多說了)
2、接收信息和回復信息前面也說到了,在這里只把關鍵詞回復判斷的方法貼出來供大家參考一下
#region 關鍵字回復 ////// 關鍵字回復 /// /// /// /// ///public string GetKeyword(string FromUserName, string ToUserName, string Content) { string resXml = ""; string sqlWhere = "wechat_id=" + WeChat_ID + " and keyword_name='" + Content+"'"; DataTable dtKeyword = keyworddal.GetList(sqlWhere).Tables[0]; if (dtKeyword.Rows.Count > 0) { dtKeyword = keyworddal.GetRandomList(sqlWhere, "1").Tables[0]; if (dtKeyword.Rows.Count > 0) { string article_id = dtKeyword.Rows[0]["article_id"].ToString(); string keyword_type = dtKeyword.Rows[0]["keyword_type"].ToString(); string keyword_text = dtKeyword.Rows[0]["keyword_text"].ToString(); switch (keyword_type) { case "text": resXml = " "; break; case "news": resXml = GetArticle(FromUserName, ToUserName, article_id, User_ID); break; } } } else { resXml = GetDefault(FromUserName, ToUserName, WeChat_ID, User_ID); } return resXml; } #endregion 關鍵字回復 " + ConvertDateTimeInt(DateTime.Now) + " 0
其他還有很多圖片回復,二維碼掃描回復信息等都大同小異,處理方式都差不多,參考開發(fā)文檔很快就能搞定,這里就不在多說了,不明白的地方在討論。
關于“.Net如何實現(xiàn)微信公眾平臺開發(fā)接口“信息回復””這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。