我是靠谱客的博主 落后灯泡,这篇文章主要介绍.NET 微信开发自动内容回复实例代码,现在分享给大家,希望可以做个参考。

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

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

文件名 : v.ashx

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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; } } } }
登录后复制

这段代码中具有开发者验证的功能,同时也考虑到了 由菜单发送到平台的文本的接收和响应。

相关文章:

微信开发消息推送实现技巧(附代码)

一个基于WebSocket的WEB消息推送框架

在Java中通过websocket实现消息推送的实现代码详解

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

最后

以上就是落后灯泡最近收集整理的关于.NET 微信开发自动内容回复实例代码的全部内容,更多相关.NET内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部