微信公众平台接口,asp.net实现

来源:互联网 发布:南风知我意下载 编辑:程序博客网 时间:2024/05/17 22:44
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         wxmessage wx = GetWxMessage();  
  4.         string res = "";  
  5.         //新用户添加  
  6.         if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")  
  7.         {  
  8.             string content = "";  
  9.             content = "/:rose欢迎关注52冷笑话/:rose\n看笑话请直接回复“x”\n无聊时候还可以找我聊聊天哦!";  
  10.             res = sendTextMessage(wx, content);  
  11.         }  
  12.         else  
  13.         {  
  14.             bool sendJoke = false;  
  15.             //看笑话  
  16.             List<string> xhList = new List<string>() { "x""笑话""笑話" };  
  17.             foreach (string item in xhList)  
  18.             {  
  19.                 if (wx.Content.Trim().ToLower().Contains(item))  
  20.                 {  
  21.                     sendJoke = true;  
  22.                     break;  
  23.                 }  
  24.             }  
  25.             if (sendJoke)  
  26.             {  
  27.                 JokeDemo joke = GetJoke(wx.FromUserName);  
  28.                 if (string.IsNullOrEmpty(joke.Img))  
  29.                 {  
  30.                     string title = string.Format("编号{0}:{1}\n-----------------\n", joke.ID, joke.Title);  
  31.                     string content = joke.Content;  
  32.                     if (content.Length > 300)  
  33.                     {  
  34.                         content = GetSubString(content, 300) + "\n-----------------\n点击连接阅读全文:URL"  
  35.   
  36.                     }  
  37.                     res = sendTextMessage(wx, title + content);  
  38.                 }  
  39.                 else  
  40.                 {  
  41.                     res = sendPictureMessage(wx, joke);  
  42.                 }  
  43.             }  
  44.             //智能学聊天  
  45.             if (res == "" && Regex.IsMatch(wx.Content, "问(:|:)(.+?)答(:|:)(.+?)", RegexOptions.IgnoreCase))  
  46.             {  
  47.                 string content = "";  
  48.                 string key = Regex.Match(wx.Content, "问(:|:)(.+?)答(:|:)(.+?)", RegexOptions.IgnoreCase).Groups[2].Value.Trim();  
  49.                 int startIndex = wx.Content.IndexOf("答:") + 2;  
  50.                 if (startIndex < 3)  
  51.                 {  
  52.                     startIndex = wx.Content.IndexOf("答:") + 2;  
  53.                 }  
  54.                 string rep = wx.Content.Substring(startIndex, wx.Content.Length - startIndex).Trim();  
  55.   
  56.         // Regex.Match(wx.Content, "问(:|:)(.+?)答(:|:)(.+?)", RegexOptions.IgnoreCase).Groups[4].Value;  
  57.                 if ((new ChatBLL()).isExists(key))  
  58.                 {  
  59.                     content = "/::)O啦!学会啦\n不信你问问!";  
  60.                 }  
  61.                 else  
  62.                 {  
  63.                     if ((new ChatBLL()).Add(key, rep) > 0)  
  64.                     {  
  65.                         content = "好啦,这个问题我学会啦!\n你现在提问我吧!/::P";  
  66.                     }  
  67.                     else  
  68.                     {  
  69.                         content = "糟糕了,系统出了点儿小意外!\n麻烦你再试一次!";  
  70.                     }  
  71.                 }  
  72.                 res = sendTextMessage(wx, content);  
  73.             }  
  74.             //未知情况  
  75.             if (res == "")  
  76.             {  
  77.                 string content = (new ChatBLL()).GetReplyByKey(wx.Content.Trim());  
  78.                 if (content == "")  
  79.                 {  
  80.                     content = "/:,@-D啊哦,你在说什么?\n你可以按照下面的格式告诉我:\n问:你说的话 答:你想让我说什么\n看笑话请直接回复“x”!";  
  81.                 }  
  82.                 res = sendTextMessage(wx, content);  
  83.             }  
  84.         }  
  85.         Response.Write(res);  
  86.     }  
  87.   
  88.   
  89.     /// <summary>  
  90.     /// 发送文字消息  
  91.     /// </summary>  
  92.     /// <param name="wx">获取的收发者信息</param>  
  93.     /// <param name="content">笑话内容</param>  
  94.     /// <returns></returns>  
  95.     private string sendTextMessage(wxmessage wx, string content)  
  96.     {  
  97.         string res = string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime><MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{3}]]></Content> <FuncFlag>0</FuncFlag></xml> ",  
  98.             wx.FromUserName, wx.ToUserName, DateTime.Now, 内容);  
  99.         return res;  
  100.     }  
  101.   
  102.     /// <summary>  
  103.     /// 发送图文消息  
  104.     /// </summary>  
  105.     /// <param name="wx">获取的收发者信息</param>  
  106.     /// <param name="joke">笑话信息</param>  
  107.     /// <returns></returns>  
  108.     private string sendPictureMessage(wxmessage wx, JokeDemo joke)  
  109.     {  
  110.         StringBuilder sb = new StringBuilder();  
  111.         sb.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>", wx.FromUserName);  
  112.         sb.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", wx.ToUserName);  
  113.         sb.AppendFormat("<CreateTime>{0}</CreateTime>", DateTime.Now);  
  114.         sb.AppendFormat("<MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content>");  
  115.         sb.AppendFormat("<ArticleCount>1</ArticleCount>");  
  116.         sb.AppendFormat("<Articles><item>");  
  117.         sb.AppendFormat("<Title><![CDATA[{0}]]></Title>", 标题);  
  118.         sb.AppendFormat("<Description><![CDATA[{0}]]></Description>", 说明文字);  
  119.         sb.AppendFormat("<PicUrl><![CDATA[{0}]]></PicUrl>", 图片地址);  
  120.         sb.AppendFormat("<Url><![CDATA[{0}]]></Url>", 连接地址);  
  121.         sb.AppendFormat("</item></Articles><FuncFlag>0</FuncFlag></xml>");  
  122.         return sb.ToString();  
  123.     }  
  124.   
  125.     /// <summary>  
  126.     /// 获取请求过来的微信信息  
  127.     /// </summary>  
  128.     /// <returns></returns>  
  129.     private wxmessage GetWxMessage()  
  130.     {  
  131.         wxmessage wx = new wxmessage();  
  132.         StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);  
  133.         XmlDocument xml = new XmlDocument();  
  134.         xml.Load(str);  
  135.         wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;  
  136.         wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;  
  137.         wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;  
  138.         if (wx.MsgType.Trim() == "text")  
  139.         {  
  140.             wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;  
  141.         }  
  142.         if (wx.MsgType.Trim() == "event")  
  143.         {  
  144.             wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;  
  145.         }  
  146.         return wx;  
  147.     }  
  148.   
  149.     /// <summary>  
  150.     /// 微信认证  
  151.     /// </summary>  
  152.     private void RenZheng()  
  153.     {  
  154.         #region 微信认证  
  155.         //string res = "";  
  156.         //string token = "52lxh";  
  157.         //string signature = Request["signature"];  
  158.         //string timestamp = Request["timestamp"];  
  159.         //string nonce = Request["nonce"];  
  160.         //string echostr = Request["echostr"];  
  161.         //if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(nonce) || string.IsNullOrEmpty(echostr))  
  162.         //{  
  163.         //    using (StreamWriter sw = new StreamWriter(Server.MapPath("wx.txt")))  
  164.         //    {  
  165.         //        sw.Write("参数错误" + Request.Url);  
  166.         //    }  
  167.         //}  
  168.         //else  
  169.         //{  
  170.         //    ArrayList arr = new ArrayList() { token, timestamp, nonce };  
  171.         //    arr.Sort();  
  172.         //    string signature1 = GetSHA1(arr[0].ToString() + arr[1].ToString() + arr[2].ToString());  
  173.         //    if (signature == signature1.ToLower())  
  174.         //    {  
  175.         //        res = echostr;  
  176.         //    }  
  177.         //    else  
  178.         //    {  
  179.         //        res = "error";  
  180.         //    }  
  181.   
  182.         //    Response.Write(res);  
  183.         //}  
  184.         #endregion  
  185.     }  
  186.   
  187.     /// <summary>  
  188.     /// 加密字符串  
  189.     /// </summary>  
  190.     /// <param name="password"></param>  
  191.     /// <returns></returns>  
  192.     private string GetSHA1(string password)  
  193.     {  
  194.         string shh1string = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");  
  195.         return shh1string;  
  196.     }  
  197.   
  198.     /// <summary>  
  199.     /// 截取字符串  
  200.     /// </summary>  
  201.     /// <param name="content"></param>  
  202.     /// <param name="length"></param>  
  203.     /// <returns></returns>  
  204.     private string GetSubString(string content, int length)  
  205.     {  
  206.         if (content.Length >= length)  
  207.         {  
  208.             return content.Substring(0, length);  
  209.         }  
  210.         else  
  211.         {  
  212.             return content;  
  213.         }  
  214.     }  
  215.   
  216.     //自定义一个微信消息实体类  
  217.     class wxmessage  
  218.     {  
  219.         public string FromUserName { getset; }  
  220.         public string ToUserName { getset; }  
  221.         public string MsgType { getset; }  
  222.         public string EventName { getset; }  
  223.         public string Content { getset; }  
  224.     } 
0 0
原创粉丝点击