.net MVC 微信公衆號 點擊菜單拉取消息時的事件推送

官方文檔:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141016&token=&lang=zh_CN數據庫

業務描述:點擊菜單,推送消息,消息內容爲自定義的讀取數據庫信息以後,爲用戶推送的動態消息。服務器

首先須要用代碼實現自定義菜單,爲對應的菜單指定click事件。微信

關於自定義菜單的建立及事件指定,請看上一篇文章,本篇主要介紹事件響應的實現。post

MVC controller 中的代碼以下:測試

public void MenuEventHandle()
        {
            #region 驗證Token 此塊代碼只在啓用服務器配置時執行一次驗證token令牌,服務器配置啓用以後能夠刪掉塊內代碼,相關代碼請看驗證微信簽名那一篇
            string echoStr = Request.QueryString["echoStr"];
            string signature = Request.QueryString["signature"];
            string timestamp = Request.QueryString["timestamp"];
            string nonce = Request.QueryString["nonce"];
            Log.logmsg("測試輸出: echoStr = " + echoStr);
            Log.logmsg("測試輸出: signature = " + signature);
            Log.logmsg("測試輸出: timestamp = " + timestamp);
            Log.logmsg("測試輸出: nonce = " + nonce);

            if (AdminUtil.CheckSignature("基本設置中的Token令牌", signature, timestamp, nonce) && !string.IsNullOrEmpty(echoStr))
            {
                Response.Write(echoStr);
                Response.End();
            }
            #endregion
            Log.logmsg("開始執行按鈕事件");

            string postString = string.Empty;
            if (HttpContext.Request.HttpMethod.ToUpper() == "POST")
            {
                using (Stream stream = HttpContext.Request.InputStream)
                {
                    //獲取微信提交過來的參數
                    Byte[] postBytes = new Byte[stream.Length];
                    stream.Read(postBytes, 0, (Int32)stream.Length);
                    postString = Encoding.UTF8.GetString(postBytes);
                    //對其進行處理
                    AdminUtil.MenuMessage(postString);
                }
            }
        }

AdminUtil類spa

#region 處理應答消息
        /// <summary>
        /// 處理應答消息
        /// </summary>
        /// <param name="postStr">微信提交過來的參數</param>
        /// <returns></returns>
        public static string MenuMessage(string postStr)
        {
            string responseContent = "";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("GB2312").GetBytes(postStr)));
            XmlNode MsgType = xmldoc.SelectSingleNode("/xml/MsgType");
            if (MsgType != null)
            {
                switch (MsgType.InnerText)
                {
                    case "event":
                        responseContent = WXApi.MenuEventHandle(xmldoc);//事件處理
                        break;
                    //case "text":
                    //    responseContent = TextHandle(xmldoc);//接受文本消息處理
                    //    break;
                    default:
                        break;
                }
            }
            return responseContent;
        }

        #endregion

WXApi類:code

 public static string MenuEventHandle(XmlDocument xmldoc)
        {
            string responseContent = "";
            XmlNode Event = xmldoc.SelectSingleNode("/xml/Event");//事件類型,CLICK
            XmlNode EventKey = xmldoc.SelectSingleNode("/xml/EventKey");//事件KEY值,與自定義菜單接口中KEY值對應
            XmlNode ToUserName = xmldoc.SelectSingleNode("/xml/ToUserName");//開發者微信號
            XmlNode FromUserName = xmldoc.SelectSingleNode("/xml/FromUserName");//發送方帳號一個openid
            if (Event != null)
            {
                //菜單單擊事件
                if (Event.InnerText.Equals("CLICK"))
                {
                    if (EventKey.InnerText.Equals("V1001_01"))//能夠進行相關內容的實現,如發送自定義的動態消息
                    {
                        Log.logmsg("Event=" + Event + ";EventKey=" + EventKey + ";ToUserName=" + ToUserName + ";FromUserName=" + FromUserName + "");
                        AdminUtil.SendNewsMsg(FromUserName.InnerText, "進度提醒", "描述", "", "");
                    }
                }
            }           
            return responseContent;
        }

基本配置以下:xml

相關文章
相關標籤/搜索