/// <summary> /// 菜单点击事件处理 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public override IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage) { IResponseMessageBase responseMessage = null; //回复日志 var replylog = new WeiChat_KeyWordReplyLog { ReceiveWords = requestMessage.EventKey, CreateTime = DateTime.Now, TenantId = TenantId, From = requestMessage.FromUserName, To = requestMessage.ToUserName, EventKey = requestMessage.EventKey, MsgId = requestMessage.MsgId }; try { #region 事件关键字回复,仅支持等于 var keyword = db.WeiChat_KeyWordAutoReplies.FirstOrDefault( p => p.AllowEventKey && (p.KeyWord == requestMessage.EventKey)); if (keyword != null) { replylog.ContentId = keyword.ContentId; replylog.KeyWord = keyword.KeyWord; replylog.WeiChat_KeyWordAutoReplyId = keyword.Id; responseMessage = this.ReplyResponseMessage(db, keyword.KeyWord, keyword.ContentId, keyword.KeyWordContentType, (key, contentId, type) => { replylog.IsSuccess = true; }); } #endregion else { return(null); } } catch (Exception ex) { replylog.Error = ex.ToString(); responseMessage = CreateResponseMessage <ResponseMessageText>(); ((ResponseMessageText)responseMessage).Content = HttpContext.Current.IsDebuggingEnabled ? ex.ToString() : "出现错误,无法处理请求!"; } //记录日志 db.WeiChat_KeyWordReplyLogs.Add(replylog); db.SaveChanges(); return(responseMessage); }
/// <summary> /// 订阅(关注)事件 /// </summary> /// <returns></returns> public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage) { IResponseMessageBase responseMessage = null; { #region 获取并更新新关注用户的信息 try { //由于这里是基于微信服务器事件,故不能使用WeChatApisContext.Current.UserApi方式获取到UserApi接口,请new一个然后通过SetKey进行赋值 var userApi = new UserApi(); userApi.SetKey(TenantId); //获取新关注用户的用户信息 var userInfoResult = userApi.Get(requestMessage.FromUserName); if (userInfoResult.IsSuccess()) { var user = db.WeiChat_Users.FirstOrDefault(p => p.OpenId == userInfoResult.OpenId); var userinfo = db.User_Infos.FirstOrDefault(p => p.OpenId == userInfoResult.OpenId); if (userinfo == null) { userinfo = new User_Info { OpenId = userInfoResult.OpenId, Balance = 0, Integral = 0, State = EnumUserState.Normal, TenantId = TenantId, CreateTime = DateTime.Now, LastLoginOn = DateTime.Now, LoginCount = 0 }; db.User_Infos.Add(userinfo); } if (user == null) { user = new WeiChat_User { City = userInfoResult.City, Country = userInfoResult.Country, GroupIds = userInfoResult.GroupIds, HeadImgUrl = userInfoResult.Headimgurl, Language = userInfoResult.Language, NickName = userInfoResult.NickName, OpenId = userInfoResult.OpenId, Province = userInfoResult.Province, Remark = userInfoResult.Remark, Sex = userInfoResult.Sex, Subscribe = true, SubscribeTime = userInfoResult.SubscribeTime, UnionId = userInfoResult.Unionid, TenantId = TenantId }; db.WeiChat_Users.Add(user); } else { user.City = userInfoResult.City; user.Country = userInfoResult.Country; user.GroupIds = userInfoResult.GroupIds; user.HeadImgUrl = userInfoResult.Headimgurl; user.Language = userInfoResult.Language; user.NickName = userInfoResult.NickName; //user.OpenId=userInfoResult.OpenId user.Province = userInfoResult.Province; user.Remark = userInfoResult.Remark; user.Sex = userInfoResult.Sex; user.Subscribe = true; user.SubscribeTime = userInfoResult.SubscribeTime; user.TenantId = TenantId; user.UnionId = userInfoResult.Unionid; } db.SaveChanges(); } else { logger.Log(LoggerLevels.Error, userInfoResult.GetFriendlyMessage() + "\n\r详细错误:" + userInfoResult.DetailResult); } } catch (Exception ex) { logger.Log(LoggerLevels.Error, "MessageId:" + requestMessage.MsgId + Environment.NewLine + "具体错误信息:" + ex); } #endregion var keyword = string.Format("{0}[系统关注事件]", requestMessage.EventKey); //回复日志 var replylog = new WeiChat_KeyWordReplyLog { ReceiveWords = keyword, CreateTime = DateTime.Now, TenantId = TenantId, From = requestMessage.FromUserName, To = requestMessage.ToUserName, EventKey = requestMessage.EventKey, MsgId = requestMessage.MsgId }; try { var subscribeReplies = db.WeiChat_SubscribeReplies.FirstOrDefault(); if (subscribeReplies != null) { replylog.ContentId = subscribeReplies.ContentId; replylog.KeyWord = ""; replylog.WeiChat_KeyWordAutoReplyId = subscribeReplies.Id; responseMessage = this.ReplyResponseMessage(db, keyword, subscribeReplies.ContentId, subscribeReplies.KeyWordContentType, (key, contentId, type) => { replylog.IsSuccess = true; }); } } catch (Exception ex) { replylog.Error = ex.ToString(); responseMessage = CreateResponseMessage <ResponseMessageText>(); ((ResponseMessageText)responseMessage).Content = HttpContext.Current.IsDebuggingEnabled ? ex.ToString() : "出现错误,无法处理请求,具体信息请查看回复日志!"; } //记录日志 db.WeiChat_KeyWordReplyLogs.Add(replylog); db.SaveChanges(); } if (responseMessage == null) { responseMessage = ResponseMessageBase.CreateFromRequestMessage <ResponseMessageText>(requestMessage); ((ResponseMessageText)responseMessage).Content = "欢迎您关注Magicodes.WeiChat!"; return(responseMessage); } return(responseMessage); }
/// <summary> /// 处理文字请求 /// </summary> /// <returns></returns> public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage) { IResponseMessageBase responseMessage = null; //客服用于唤醒多客服 if (requestMessage.Content == "客服") { responseMessage = CreateResponseMessage <ResponseMessageTransfer_Customer_Service>(); return(responseMessage); } //回复日志 var replylog = new WeiChat_KeyWordReplyLog { ReceiveWords = requestMessage.Content, CreateTime = DateTime.Now, TenantId = TenantId, From = requestMessage.FromUserName, To = requestMessage.ToUserName, //EventKey = requestMessage.EventKey, MsgId = requestMessage.MsgId }; { try { #region 关键字回复 var keyword = db.WeiChat_KeyWordAutoReplies.FirstOrDefault( p => (p.MatchType == KeyWordMatchTypes.Equals) && (p.KeyWord == requestMessage.Content)); if (keyword == null) { keyword = db.WeiChat_KeyWordAutoReplies.FirstOrDefault( p => (p.MatchType == KeyWordMatchTypes.Contains) && requestMessage.Content.Contains(p.KeyWord)); } if (keyword != null) { replylog.ContentId = keyword.ContentId; replylog.KeyWord = keyword.KeyWord; replylog.WeiChat_KeyWordAutoReplyId = keyword.Id; responseMessage = this.ReplyResponseMessage(db, keyword.KeyWord, keyword.ContentId, keyword.KeyWordContentType, (key, contentId, type) => { replylog.IsSuccess = true; }); } #endregion else { #region 答不上来配置 //答不上来 try { var notAnswerReply = db.WeiChat_NotAnswerReplies.FirstOrDefault(); if (notAnswerReply != null) { replylog.ContentId = notAnswerReply.ContentId; replylog.KeyWord = ""; replylog.WeiChat_KeyWordAutoReplyId = notAnswerReply.Id; responseMessage = this.ReplyResponseMessage(db, requestMessage.Content, notAnswerReply.ContentId, notAnswerReply.KeyWordContentType, (key, contentId, type) => { replylog.IsSuccess = true; }); } else { var sb = new StringBuilder(); sb.AppendLine("您好,该关键字本公众号尚不支持/(ㄒoㄒ)/~~。"); var keywords = db.WeiChat_KeyWordAutoReplies.Take(10).Select(p => p.KeyWord).ToArray(); if ((keywords != null) && (keywords.Length > 0)) { sb.AppendLine("不过,您可以试试以下关键字哦:"); sb.AppendLine(string.Join("、", keywords)); } responseMessage = CreateResponseMessage <ResponseMessageText>(); ((ResponseMessageText)responseMessage).Content = sb.ToString(); } } catch (Exception ex) { replylog.Error = ex.ToString(); responseMessage = CreateResponseMessage <ResponseMessageText>(); ((ResponseMessageText)responseMessage).Content = HttpContext.Current.IsDebuggingEnabled ? ex.ToString() : "出现错误,无法处理请求!"; } #endregion } } catch (Exception ex) { replylog.Error = ex.ToString(); responseMessage = CreateResponseMessage <ResponseMessageText>(); ((ResponseMessageText)responseMessage).Content = HttpContext.Current.IsDebuggingEnabled ? ex.ToString() : "出现错误,无法处理请求!"; } //记录日志 db.WeiChat_KeyWordReplyLogs.Add(replylog); db.SaveChanges(); } return(responseMessage); }