search
HomeWeChat AppletWeChat Development.NET WeChat development automatic content reply example code

In WeChat development, the first problem encountered is how to receive and respond to user messages. This article will introduce you to the methods and key codes.

The code developed by ASP.NET to receive WeChat messages and respond to user messages is as follows:

File name: 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;
            }
        }
    }
}

This code has the function of developer verification , also taking into account the reception and response of texts sent by the menu to the platform.

Related articles:

WeChat development message push implementation skills (with code)

A WEB message push framework based on WebSocket

Detailed explanation of the implementation code for message push through websocket in Java

The above is the detailed content of .NET WeChat development automatic content reply example code. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor