微信.NET 微信開發 自動內容回覆 ASP.NET C#代碼

微信開發中,首先遇到的問題就是處理如何接收和響應用戶消息 , 本文將向你們介紹一下方法和關鍵的代碼。git

本文使用的接口庫是  :https://github.com/chendong152/Weixin_api_.net 很是感謝 TD的做者github

ASP.NET開發的  接收微信消息和響應用戶消息代碼以下:api

文件名 :  v.ashx微信

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Td.Weixin.Public.Common;
using Td.Weixin.Public.Message;

namespace WeiWeiXin.Net6
{
    /// <summary>
    /// v 的摘要說明
    /// </summary>
    public class v : IHttpHandler
    {

        /// <summary>
        ///    開發者 驗證 模塊
        /// </summary>
        /// <param name="context"></param>
        public bool ProcessRequest2(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //  context.Response.Write("Hello World");
            try
            {
                string echoStr = context.Request["echoStr"]; 
                if (!string.IsNullOrEmpty(echoStr))
                {
                    context.Response.Write(echoStr);
                    return true;
                }
                else
                {
                    // context.Response.Write("end");
                    //   context.Response.End();
                }
            }
            catch (Exception e)
            {
                //   context.Response.Write("end" + e.Message + e.ToString());
                // context.Response.End();
            }
            return false;
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //若是 是 驗證  則 直接 退出 
            if (ProcessRequest2(context))
                return;

            context.Response.ContentType = "text/plain";
            var m = ReceiveMessage.ParseFromContext();

            if (m == null)
                return;

            //被關注
            if (m.MsgType == MessageType.Event && m.InnerToXmlText().IndexOf("subscribe") >= 0)
            {       
                //發送AIML請求
                var r2 = m.GetTextResponse();
                string result = "[微笑]歡迎關注";  
                r2.Data = (TextMsgData)result;
                r2.Response();
                return;
            }

            //數據解析
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(m.ToXmlText());//"<xml><description><![CDATA[木子屋:http://www.mzwu.com/]]></description></xml>");

            //菜單 或者 用戶文本輸入
            if (m.MsgType == MessageType.Text || (m.MsgType == MessageType.Event && m.InnerToXmlText().IndexOf("subscribe") < 0))
            {
                //讀取
                string rr = "";

                if (m.MsgType == MessageType.Text)
                {
                    rr = xmlDoc.SelectSingleNode("//Content").FirstChild.InnerText.ToLower().Trim();
                }
                else
                {
                    rr = xmlDoc.SelectSingleNode("//EventKey").FirstChild.InnerText.ToLower().Trim();
                }
               
                //發送 
                var r2 = m.GetTextResponse();
                string result = "歡迎使用,您發送的是:" +rr;//  
                r2.Data = (TextMsgData)result;
                r2.Response();
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

這段代碼中具備開發者驗證的功能,同時也考慮到了 由菜單發送到平臺的文本的接收和響應。微信開發

最後 廣告一下 : 完整的 微微信.NET 能夠在這裏找到  udoo123.taobao.comspa

相關文章
相關標籤/搜索