您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/6/17 1:51:08发布14次查看
微信开发中,首先遇到的问题就是处理如何接收和响应用户消息 , 本文将向大家介绍一下方法和关键的代码。
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; } } } }
这段代码中具有开发者验证的功能,同时也考虑到了 由菜单发送到平台的文本的接收和响应。
相关文章:
微信开发消息推送实现技巧(附代码)
一个基于websocket的web消息推送框架
在java中通过websocket实现消息推送的实现代码详解
以上就是.net 微信开发自动内容回复实例代码的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product