温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

.NET微信开发如何实现自动内容回复功能

发布时间:2021-07-27 11:25:26 来源:亿速云 阅读:122 作者:小新 栏目:移动开发

这篇文章主要介绍.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 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微信开发如何实现自动内容回复功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI