来源:自学PHP网 时间:2015-04-14 12:58 作者: 阅读:次
[导读] 接收到微信用户发送过来的消息,我们服务器处理并回应。需要注意一下,微信说明对于每一个POST请求,开发者在响应包(Get)中返回特定XML结构,对该消息进行响应(现支持回复文本...
接收到微信用户发送过来的消息,我们服务器处理并回应。 需要注意一下,微信说明
我们无法保证我们服务器能在5秒回应的话,推荐使用异步处理,使用客服消息回复用户(不讨论客服消息次数限制等问题)
WeixinServer #region 静态全局变量 private static Token _Token = null; private static Ticket _Ticket = null; private readonly static string appid = "wx123456789000"; private readonly static string secret = "abcdefghijklmnopqrstuvwxyz"; private readonly static string domain = "http://wechat.mydomain.com"; #endregion // 消息处理 public void DisposeMsg(Stream stream) { XmlDocument doc = new XmlDocument(); doc.Load(stream); string type = GetMsgType(doc); string openid = GetMsgOpenid(doc); string access_token = AccessToken(); string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_token; switch (type) { case "event": { string ev = GetMsgEvent(doc); if (ev == "subscribe") { //关注时,回复用户消息 string data = "{\"touser\":\"" + openid + "\",\"msgtype\":\"news\",\"news\":{\"articles\": [{\"title\":\"欢迎关注 “XXXX“ \",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/face1.png\"},{\"title\":\"“XXXX” 安卓版下载\",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/jiantou.png\"},{\"title\":\"“XXXX” 苹果版下载\",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/jiantou.png\"}]}}"; HttpHelper.Post(url, data); } } break; case "text": { string content = GetMsgContent(doc); string data = ""; switch (content) { case "tw": { data = "{\"touser\":\"" + openid + "\",\"msgtype\":\"news\",\"news\":{\"articles\": [{\"title\":\"欢迎关注 “XXXX“ \",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/face1.png\"},{\"title\":\" “XXXX” 安卓版下载\",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/jiantou.png\"},{\"title\":\" “XXXX” 苹果版下载\",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/jiantou.png\"}]}}"; } break; default: { content = "欢迎关注 “XXXX”\n\nAPP下载:<a href=\\\"\\\">Android</a> <a href=\\\"\\\">IOS</a>"; data = "{\"touser\":\"" + openid + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + content + "\"}}"; } break; } HttpHelper.Post(url, data); } break; default: break; } } Action里面处理 [HttpPost] public ActionResult Access() { //异步操作 使用客服消息接口回复用户 Stream stream = Request.InputStream; AsyncManager.OutstandingOperations.Increment(); var task = Task.Factory.StartNew(() => weixin.DisposeMsg(stream)); task.ContinueWith(t => { //AsyncManager.Parameters["model"] = t.Result; AsyncManager.OutstandingOperations.Decrement(); }); return Content(""); } |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com