微信公众平台接口,asp.net实现
我为自己的笑话网开发了一个微信公众平台的接口,在这里分享给大家,希望能对朋友们有帮助,如果有什么地方写的不好,好请大家指点!
首先是要进行认证,认证的时候,只需要在Page_Load事件里面单独去执行 认证的方法就可以了,具体代码见下面的RenZheng()
认证通过之后就可以对网友的消息进行处理,可以根据微信平台推送过来的数据进行分析!我相信大家在看到这篇文章的时候,在此之前肯定对平台都有所了解了,所以,废话不多说,直接上代码! 如果有什么疑问的欢迎加群:242384606 进行讨论!
protected void Page_Load(object sender, EventArgs e)
{
wxmessage wx = GetWxMessage();
string res = "";
//新用户添加
if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")
{
string content = "";
content = "/:rose欢迎关注52冷笑话/:rose\n看笑话请直接回复“x”\n无聊时候还可以找我聊聊天哦!";
res = sendTextMessage(wx, content);
}
else
{
bool sendJoke = false;
//看笑话
List<string> xhList = new List<string>() { "x", "笑话", "笑話" };
foreach (string item in xhList)
{
if (wx.Content.Trim().ToLower().Contains(item))
{
sendJoke = true;
break;
}
}
if (sendJoke)
{
JokeDemo joke = GetJoke(wx.FromUserName);
if (string.IsNullOrEmpty(joke.Img))
{
string title = string.Format("编号{0}:{1}\n-----------------\n", joke.ID, joke.Title);
string content = joke.Content;
if (content.Length > 300)
{
content = GetSubString(content, 300) + "\n-----------------\n点击连接阅读全文:URL" }
res = sendTextMessage(wx, title + content);
}
else
{
res = sendPictureMessage(wx, joke);
}
}
//智能学聊天
if (res == "" && Regex.IsMatch(wx.Content, "问(:|:)(.+?)答(:|:)(.+?)", RegexOptions.IgnoreCase))
{
string content = "";
string key = Regex.Match(wx.Content, "问(:|:)(.+?)答(:|:)(.+?)", RegexOptions.IgnoreCase).Groups[2].Value.Trim();
int startIndex = wx.Content.IndexOf("答:") + 2;
if (startIndex < 3)
{
startIndex = wx.Content.IndexOf("答:") + 2;
}
string rep = wx.Content.Substring(startIndex, wx.Content.Length - startIndex).Trim(); // Regex.Match(wx.Content, "问(:|:)(.+?)答(:|:)(.+?)", RegexOptions.IgnoreCase).Groups[4].Value;
if ((new ChatBLL()).isExists(key))
{
content = "/::)O啦!学会啦\n不信你问问!";
}
else
{
if ((new ChatBLL()).Add(key, rep) > 0)
{
content = "好啦,这个问题我学会啦!\n你现在提问我吧!/::P";
}
else
{
content = "糟糕了,系统出了点儿小意外!\n麻烦你再试一次!";
}
}
res = sendTextMessage(wx, content);
}
//未知情况
if (res == "")
{
string content = (new ChatBLL()).GetReplyByKey(wx.Content.Trim());
if (content == "")
{
content = "/:,@-D啊哦,你在说什么?\n你可以按照下面的格式告诉我:\n问:你说的话 答:你想让我说什么\n看笑话请直接回复“x”!";
}
res = sendTextMessage(wx, content);
}
}
Response.Write(res);
} /// <summary>
/// 发送文字消息
/// </summary>
/// <param name="wx">获取的收发者信息</param>
/// <param name="content">笑话内容</param>
/// <returns></returns>
private string sendTextMessage(wxmessage wx, string content)
{
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> ",
wx.FromUserName, wx.ToUserName, DateTime.Now, 内容);
return res;
} /// <summary>
/// 发送图文消息
/// </summary>
/// <param name="wx">获取的收发者信息</param>
/// <param name="joke">笑话信息</param>
/// <returns></returns>
private string sendPictureMessage(wxmessage wx, JokeDemo joke)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>", wx.FromUserName);
sb.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", wx.ToUserName);
sb.AppendFormat("<CreateTime>{0}</CreateTime>", DateTime.Now);
sb.AppendFormat("<MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content>");
sb.AppendFormat("<ArticleCount>1</ArticleCount>");
sb.AppendFormat("<Articles><item>");
sb.AppendFormat("<Title><![CDATA[{0}]]></Title>", 标题);
sb.AppendFormat("<Description><![CDATA[{0}]]></Description>", 说明文字);
sb.AppendFormat("<PicUrl><![CDATA[{0}]]></PicUrl>", 图片地址);
sb.AppendFormat("<Url><![CDATA[{0}]]></Url>", 连接地址);
sb.AppendFormat("</item></Articles><FuncFlag>0</FuncFlag></xml>");
return sb.ToString();
} /// <summary>
/// 获取请求过来的微信信息
/// </summary>
/// <returns></returns>
private wxmessage GetWxMessage()
{
wxmessage wx = new wxmessage();
StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
XmlDocument xml = new XmlDocument();
xml.Load(str);
wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
if (wx.MsgType.Trim() == "text")
{
wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
}
if (wx.MsgType.Trim() == "event")
{
wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
}
return wx;
} /// <summary>
/// 微信认证
/// </summary>
private void RenZheng()
{
#region 微信认证
//string res = "";
//string token = "52lxh";
//string signature = Request["signature"];
//string timestamp = Request["timestamp"];
//string nonce = Request["nonce"];
//string echostr = Request["echostr"];
//if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(nonce) || string.IsNullOrEmpty(echostr))
//{
// using (StreamWriter sw = new StreamWriter(Server.MapPath("wx.txt")))
// {
// sw.Write("参数错误" + Request.Url);
// }
//}
//else
//{
// ArrayList arr = new ArrayList() { token, timestamp, nonce };
// arr.Sort();
// string signature1 = GetSHA1(arr[0].ToString() + arr[1].ToString() + arr[2].ToString());
// if (signature == signature1.ToLower())
// {
// res = echostr;
// }
// else
// {
// res = "error";
// } // Response.Write(res);
//}
#endregion
} /// <summary>
/// 加密字符串
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
private string GetSHA1(string password)
{
string shh1string = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
return shh1string;
} /// <summary>
/// 截取字符串
/// </summary>
/// <param name="content"></param>
/// <param name="length"></param>
/// <returns></returns>
private string GetSubString(string content, int length)
{
if (content.Length >= length)
{
return content.Substring(0, length);
}
else
{
return content;
}
} //自定义一个微信消息实体类
class wxmessage
{
public string FromUserName { get; set; }
public string ToUserName { get; set; }
public string MsgType { get; set; }
public string EventName { get; set; }
public string Content { get; set; }
}
52冷笑话官网:http://www.52lxh.com 随时欢迎您!
欢迎关注52冷笑话微信看笑话!
微信公众平台接口,asp.net实现的更多相关文章
- Java微信公众平台接口封装源码分享
前言: 这篇博客是在三月初动手项目的时候准备写的,但是为了完成项目只好拖延时间写这篇博客,顺便也可以在项目中应用我自己总结的的一些经验.今天看来,这些方法的应用还是可以的,至少实现了我之前的 ...
- 微信公众平台接口API
<?php /** * Author: helen * CreateTime: 2015/12/9 20:14 * description: 微信公众平台接口API */ class Wecha ...
- the5fire博客对接微信公众平台接口 | the5fire的技术博客
the5fire博客对接微信公众平台接口 | the5fire的技术博客 the5fire博客对接微信公众平台接口
- C#-微信公众平台接口-上传临时素材
最烦做微信公众平台的东西..文档说得不清不楚,又没示例代码,只能自己 慢慢搜索,弄了一晚上,基本弄出来了,把本地的图片上传到微信的临时素材那里,返回媒体ID,用于其他操作,代码如下 :(自己导入相应的 ...
- 微信公众平台接口调用第一步(获取access_token)
最近公司需要开发微信公众号,闲着无聊就写写博客,希望能帮到你我 上代码: package test; import java.util.List; import java.util.ArrayList ...
- .net 后台以post方式调用微信公众平台接口
public class Fresult { public int errcode { get; set; } public string errmsg { get; set; } public st ...
- 微信公众平台接口获取时间戳为10位,java开发需转为13位
问题1:为什么会生成13位的时间戳,13位的时间戳和10时间戳分别是怎么来的 ? java的date默认精度是毫秒,也就是说生成的时间戳就是13位的,而像c++或者php生成的时间戳默认就是10位的, ...
- asp.net微信公众平台开发
http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97 ...
- .NET 微信开放平台接口(接收短信、发送短信)
.NET 微信开放平台接口(接收短信.发送短信) 前两天做个项目用到了微信api功能.项目完成后经过整理封装如下微信操作类. 以下功能的实现需要开发者已有微信的公众平台账号,并且开发模式已开启.接口配 ...
随机推荐
- win7下go web之revel
win7下go web之revel安装 接着上回记录的win7下go环境搭建,go的开发,现在除了sublime外,LiteIDE比较推荐,下载链接 下载安装后直接打开,需要配置下go环境(本机使 ...
- 读书笔记之SQL注入漏洞和SQL调优
原文:读书笔记之SQL注入漏洞和SQL调优 最近读了程序员的SQL金典这本书,觉得里面的SQL注入漏洞和SQL调优总结得不错,下面简单讨论下SQL注入漏洞和SQL调优. 1. SQL注入漏洞 由于“' ...
- POJ 3076 Sudoku DLX精确覆盖
DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 4416 Accepte ...
- x264 - open gop and closed gop
GOP - Group of picture(影像集团),它指的是两个I帧之间的距离. Reference(基准期). 它指的是两个P帧之间的距离. 简而言之, 跨参考gop的,变open gop: ...
- 操作系统学习笔记_12_I/O管理 --I/O管理概述
h1 { margin-bottom: 0.21cm; }h1.western { font-family: "Liberation Sans",sans-serif; font- ...
- Linux 常用命令汇总
1.shutdown -s 时间 如果是想马上关机就直接输入0或者now: 2.init 0 这个是运行级别关机: 3.halt 这个命令不是很好用: 4.power off 这个命令也是很好用的. ...
- javascript小白学习指南0---1
引言: 做为一名程序猿.都是真心的想把自己的东西分享出来,供大家一起学习探讨.一起提高技能.一起涨工资,呵 这一系列的文章都是关于Javascript 基础的 当然文章其中穿插了些我自己的理解.希 ...
- mvc5 解析route源码实现自己的route系统
Asp.net mvc5 解析route源码实现自己的route系统 url route 路由系统的责任是找到匹配的路由,创建路由数据,并将请求分配给一个处理程序. 选择动作是 MVC 的处理程序 ...
- Tigase XMPP Server在CentOS部署和配置
Tigase XMPP Server在CentOS部署与配置 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 以下讲述Tigase XMPP Server ...
- Ajax跨域请求数据实例(JSOPN方式)
今天在做取消申请的时候遇到了一个跨域ajax提交的问题. 情景是: 系统A是asp.net的站点,其中包括一个取消申请的接口(get方式通过参数提交到系统的某一个页面,然后返回提交成功或失败) 系统B ...