微信開發之(四)獲取關注後的圖文推送信息

當咱們關注公衆號後,咱們能夠推送一篇圖文裏面能夠有相關的介紹和活動及歡迎的話:微信

    //請求處理的方法public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

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

                if (!string.IsNullOrEmpty(postString))
                {
                    ResponseXML(postString);//返回給微信用戶信息
                   // ReturnMessage(postString);
                }
      }
}     

        /// <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://mmbiz.qpic.cn/mmbiz/7iaiaRJE9s3HAbvxtjqbE85wqmektib1xnwdl8dniaFuK5BMnVhehWzTlK2zW0YVzFfLKjexQfgj12SJcwicGSFOyvw/640?wx_fmt=jpeg&wxfrom=5";

            responseContent = string.Format(ResponseXMl.Message_News_Main, requestXML.FromUserName, requestXML.ToUserName, newdate, "1",
          
          //參數分別表明發送圖文的標題,簡介,以及圖文的連接地址
           string.Format(ResponseXMl.Message_News_Item, "歡迎關注個人測試號", "2016年新的機遇和挑戰...", PUrlfileName, "http://mp.weixin.qq.com/s?__biz=MzA4NjIwMTg5Nw==&tempkey=1D1FlQbenM0RdbGL7jPebIYV7OWGdyEY8DClNjaJHJ3yoGh1t3KfoVvK2FGXjgeuTMemMzu4GASlT2p4yyPT4oZp8bVWH1kF3BxYJGHGHnPpcr%2Bb8zdejjshCRSr8V18HPS9gh%2Fa0wKarCp%2B2mYOEA%3D%3D&#rd"));


            HttpContext.Current.Response.ContentType = "text/xml";
            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.Write(responseContent);
            HttpContext.Current.Response.End();
        }
        /// <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 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());
            }
        }


        }
        /// <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);//調用圖文信息

        }
相關文章
相關標籤/搜索