小程序 · 2023年9月22日

.NET 微信开发自动内容回复实例代码

微信开发中,首先遇到的问题就是处理如何接收和响应用户消息 , 本文将向大家介绍一下方法和关键的代码。

ASP.NET开发的  接收微信消息和响应用户消息代码如下:

文件名 :  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>
        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 &amp;&amp; m.InnerToXmlText().IndexOf("subscribe") &gt;= 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></description></xml>");
 
            //菜单 或者 用户文本输入
            if (m.MsgType == MessageType.Text || (m.MsgType == MessageType.Event &amp;&amp; m.InnerToXmlText().IndexOf("subscribe") <p>这段代码中具有开发者验证的功能,同时也考虑到了 由菜单发送到平台的文本的接收和响应。</p><p>相关文章:</p><p><a href="http://www.php.cn/weixin-kaifa-355902.html" target="_self">微信开发消息推送实现技巧(附代码)</a></p><p><a href="http://www.php.cn/php-weizijiaocheng-232879.html" target="_self">一个基于WebSocket的WEB消息推送框架</a></p><p><a href="http://www.php.cn/java-article-355016.html" target="_self">在Java中通过websocket实现消息推送的实现代码详解</a></p>

登录后复制

以上就是.NET 微信开发自动内容回复实例代码的详细内容,更多请关注GTHOST其它相关文章!