zoukankan      html  css  js  c++  java
  • 微信开发 之 接受信息与回复信息

      在第一步中  我们配置好了服务器,然后当我们的公众号的使用用户发送信息时,就会把信息发送到服务器中,然后我们在服务器中创建个页面接受发送到的信息,再根据用户发送到的信息回复我们想要给用户回复的信息就好。

      用户发送过来的信息格式为

      

    <xml>
     <ToUserName><![CDATA[toUser]]></ToUserName>
     <FromUserName><![CDATA[fromUser]]></FromUserName> 
     <CreateTime>1348831860</CreateTime>
     <MsgType><![CDATA[text]]></MsgType>
     <Content><![CDATA[this is a test]]></Content>
     <MsgId>1234567890123456</MsgId>
     </xml>

      其中 MsgType 是用户发送过来的信息的类型 有文本,图片,语音之类的。

      创建个一般处理程序接受发过来的信息,代码如下

      

      public void ProcessRequest(HttpContext context)
            {
                string postString = string.Empty;
                if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
                {
                    string weixin = "";//获取xml数据
                    weixin = PostInput();//自定义方法,获取xml数据
                    LogHelper.LogHelper.WriteLog(typeof(_Default), "收到信息:" + weixin);
    
                    if (!string.IsNullOrEmpty(weixin))
                    {
                        ResponseMsg(weixin, context);//根据获取的内容,自动回复一定的内容。
                    }
                }
                context.Response.ContentType = "text/plain";
    
            }
            private string PostInput()/// 获取post请求数据
            {
                Stream s = System.Web.HttpContext.Current.Request.InputStream;
                byte[] b = new byte[s.Length];
                s.Read(b, 0, (int)s.Length);
                return Encoding.UTF8.GetString(b);
            }

     LogHelper类是我创建的日志类,用的是log4net,大家可以看我的另一篇博文

      http://www.cnblogs.com/mchuang/p/5075106.html

       ResponseMsg方法根据获取的内容,自动回复一定的内容。
      private void ResponseMsg(string weixin, HttpContext context)// 服务器响应微信请求
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(weixin);//读取xml字符串
                XmlElement root = doc.DocumentElement;
                XmlNode MsgType = root.SelectSingleNode("MsgType");//获取收到的消息类型。文本(text),图片(image),语音等。
                string messageType = MsgType.InnerText;
                try
                {
                    switch (messageType)
                    {
                        case "text":
                            context.Response.Write(GetResponseMessage(weixin));
                            break;
                        case "image":
    
                            break;
                        case "voice":
                            break;
                        case "vedio":
                            break;
                        case "location":
                            break;
                        case "link":
                            break;
                        case "event":
                            context.Response.Write(GetLoction(weixin));
                            break;
                        default:
                            break;
                    }
                }
                catch (Exception)
                {
                }
            }

      这里根据类型进行回复,回复的格式可以在公众平台上看到

      

    <xml>
    <ToUserName><![CDATA[toUser]]></ToUserName>
    <FromUserName><![CDATA[fromUser]]></FromUserName>
    <CreateTime>12345678</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[你好]]></Content>
    </xml>

      然后我们就拼接成这个格式,发送回去就可以了

      

      public static string GetResponseMessage(string weixin)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(weixin);//读取xml字符串
                XmlElement root = doc.DocumentElement;
                string message = "对不起,无法识别您发送的命令。请发送“帮助”来获取已经实现的功能。";
                string resxml = "";
                var model = new
                {
                    ToUserName = root.SelectSingleNode("ToUserName").InnerText,//本公众账号
                    FromUserName = root.SelectSingleNode("FromUserName").InnerText,//用户
                    CreateTime = root.SelectSingleNode("CreateTime").InnerText,//创建时间
                    Content = root.SelectSingleNode("Content").InnerText,//消息内容
                    MsgId = root.SelectSingleNode("MsgId").InnerText//消息ID,64位整型
                };//获取xml数据里各个结点的内容。
                resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[";
                resxml += model.ToUserName + "]]></FromUserName><CreateTime>" + (DateTime.Now) + "</CreateTime>";
                if (model.Content == "帮助")
                {
                    message = "目前实现功能有: ";
                    message += "
    1,翻译功能。只要发送“翻译”+空格+需要翻译的内容,即可实现翻译功能。";
                    message += "
    2,天气预报。发送 “天气”+空格+城市名 ,即可查询该城市6天内天气预报。如发送“天气 北京”。";
                }
                if (model.Content == "天气")
                {
                    message = "当前天气 9 ℃";
                    message += "当前天气 9 ℃";
                    message += "当前天气 9 ℃";
                    message += "当前天气 9 ℃";
                    message += "当前天气 9 ℃";
                    message += "当前天气 9 ℃";
                    message += "当前天气 9 ℃";
    
                }
    
                else if (model.Content.Trim().StartsWith(""))
                {
    
                }
    
                else
                {
                    message = "对不起,无法识别您发送的命令。请发送“帮助”来获取已经实现的功能。";
                }
    
                resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + message + "]]></Content><FuncFlag>0</FuncFlag></xml>";
                LogHelper.LogHelper.WriteLog(typeof(_Default), "回复信息:" + resxml);
                return resxml;//回复内容有了哦。基本实现了
            }

      

    喜欢的朋友请帮忙点个赞!!!
  • 相关阅读:
    bzoj 1176 cdq分治套树状数组
    Codeforces 669E cdq分治
    Codeforces 1101D 点分治
    Codeforces 1100E 拓扑排序
    Codeforces 1188D Make Equal DP
    Codeforces 1188A 构造
    Codeforces 1188B 式子转化
    Codeforces 1188C DP 鸽巢原理
    Codeforces 1179D 树形DP 斜率优化
    git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题
  • 原文地址:https://www.cnblogs.com/mchuang/p/5132326.html
Copyright © 2011-2022 走看看