/// <summary> /// 处理文字请求 /// </summary> public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage) { IResponseMessageBase IR = null; Common.MessageFunction cmfun = new Common.MessageFunction(); BLL.weixin_request_rule ruleBLL = new BLL.weixin_request_rule(); try { string keywords = requestMessage.Content; //发送了文字信息 //验证公众账户原始ID是否一致 if (!cmfun.ExistsOriginalId(requestMessage.ToUserName)) { new BLL.weixin_response_content().Add(requestMessage.FromUserName, requestMessage.MsgType.ToString(), requestMessage.Content, "none", "验证微帐号与微信原始帐号id不一致,说明【1】配置错误,【2】数据来源有问题", requestMessage.ToUserName); return(cmfun.GetResponseMessageTxtByContent(requestMessage, "验证微帐号与微信原始帐号id不一致,可能原因【1】系统配置错误,【2】非法的数据来源")); } int responseType = 0; int ruleId = ruleBLL.GetKeywordsRuleId(keywords, out responseType); if (ruleId <= 0 || responseType <= 0) { new BLL.weixin_response_content().Add(requestMessage.FromUserName, requestMessage.MsgType.ToString(), requestMessage.Content, "none", "未取到关键词对应的数据", requestMessage.ToUserName); return(cmfun.GetResponseMessageTxtByContent(requestMessage, "未找到匹配的关键词")); } switch (responseType) { case 1: //发送纯文字 IR = cmfun.GetResponseMessageTxt(requestMessage, ruleId); break; case 2: //发送多图文 IR = cmfun.GetResponseMessageNews(requestMessage, ruleId); break; case 3: //发送语音 IR = cmfun.GetResponseMessageeMusic(requestMessage, ruleId); break; default: break; } new BLL.weixin_response_content().Add(requestMessage.MsgType.ToString(), requestMessage.FromUserName, requestMessage.CreateTime.ToString(), requestMessage.Content, requestMessage.ToString(), string.Empty); } catch (Exception ex) { } return(IR); }
/// <summary> /// 处理文字请求 /// </summary> /// <param name="requestMessage">请求消息</param> /// <returns></returns> public override async Task <IResponseMessageBase> OnTextRequestAsync(RequestMessageText requestMessage) { var defaultResponseMessage = base.CreateResponseMessage <ResponseMessageText>(); IResponseMessageBase IR = null; Common.MessageFunction cmfun = new Common.MessageFunction(); int ruleId = 0; var requestHandler = await requestMessage.StartHandler().Default(async() => { string keywords = requestMessage.Content; if (!string.IsNullOrEmpty(keywords)) { keywords = keywords.Trim(); } var currentMessageContext = await base.GetCurrentMessageContext(); //先精确匹配,再模糊匹配 Core.WeixinRequestRule rule = Core.WeixinRequestRule.Find(Core.WeixinRequestRule._.Keywords == keywords & Core.WeixinRequestRule._.RequestType == (int)Core.WeixinRequestRule.XRequestType.Text); if (rule == null) { //模糊匹配 IList <Core.WeixinRequestRule> listRule = Core.WeixinRequestRule.FindAll(Core.WeixinRequestRule._.RequestType == (int)Core.WeixinRequestRule.XRequestType.Text & Core.WeixinRequestRule._.IsLikeQuery == 1, null, null, 0, 0); if (listRule != null && listRule.Count > 0) { int lenKeyword = keywords.Length; foreach (var item in listRule) { if (lenKeyword >= item.Keywords.Length && keywords.IndexOf(item.Keywords) >= 0) { rule = item;//规则 break; } } } } if (rule == null) { Core.WeixinRequestRule notFindKeywordRule = Core.WeixinRequestRule.Find(Core.WeixinRequestRule._.RequestType == (int)Core.WeixinRequestRule.XRequestType.NotFindKeyword); if (notFindKeywordRule == null) { //找不到规则 Core.WeixinResponseContent entity = new Core.WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = requestMessage.Content, ResponseType = "none", ReponseContent = "未取到关键词对应的数据", XmlContent = requestMessage.ToUserName }; entity.Insert(); return(cmfun.GetResponseMessageTxtByContent(requestMessage, "未找到匹配的关键词", 0)); } else { ruleId = notFindKeywordRule.Id; switch (notFindKeywordRule.ResponseType) { case (int)Core.WeixinRequestRule.XResponseType.Text: //发送纯文字 IR = cmfun.GetResponseMessageTxt(requestMessage, ruleId, 0); break; case (int)Core.WeixinRequestRule.XResponseType.Image: //发送多图文 IR = cmfun.GetResponseMessageNews(requestMessage, ruleId, 0); break; case (int)Core.WeixinRequestRule.XResponseType.Voice: //发送语音 IR = cmfun.GetResponseMessageeMusic(requestMessage, ruleId, 0); break; case (int)Core.WeixinRequestRule.XResponseType.SingleImage: IR = cmfun.GetResponseMessageSingleImagesContent(requestMessage, ruleId, 0); break; default: break; } return(IR); } } Core.WeixinResponseContent model = new Core.WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = requestMessage.Content, ResponseType = Core.WeixinResponseContent.GetResponseType((int)requestMessage.MsgType), ReponseContent = requestMessage.ToString(), XmlContent = string.Empty }; model.Insert(); return(IR); }); return(requestHandler.GetResponseMessage() as IResponseMessageBase); }