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


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

業務描述:點擊菜單,推送消息,消息內容為自定義的讀取數據庫信息之后,為用戶推送的動態消息。

首先需要用代碼實現自定義菜單,為對應的菜單指定click事件。

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

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類

#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類:

 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;
        }

基本配置如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM