public static string Handle(string message) { XElement xml = XElement.Parse(message); string messageType = xml.XPathSelectElement("MsgType").Value; if (String.IsNullOrEmpty(messageType)) { return(String.Empty); } _fromUserName = xml.XPathSelectElement("FromUserName").Value; //事件消息 if (messageType == "event") { string eventType = xml.XPathSelectElement("Event").Value; if (String.IsNullOrEmpty(eventType)) { return(String.Empty); } ResponsiveXMLMessage_TextMessage textMessage = new ResponsiveXMLMessage_TextMessage(); textMessage.Content = eventType + "from_callback"; textMessage.ToUserName = _fromUserName; textMessage.FromUserName = UserName; textMessage.CreateTime = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now); return(XMLMessageHelper.XmlSerialize(textMessage)); } //普通消息 else { string content = xml.XPathSelectElement("Content").Value; if (content == "TESTCOMPONENT_MSG_TYPE_TEXT") { ResponsiveXMLMessage_TextMessage textMessage = new ResponsiveXMLMessage_TextMessage(); textMessage.Content = "TESTCOMPONENT_MSG_TYPE_TEXT_callback"; textMessage.ToUserName = _fromUserName; textMessage.FromUserName = UserName; textMessage.CreateTime = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now); return(XMLMessageHelper.XmlSerialize(textMessage)); } else { _query_auth_code = content.Remove(0, "QUERY_AUTH_CODE:".Length); RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> getAuthorizationInfo = ThirdPartyApiWrapper.GetAuthorizationInfo(_query_auth_code); _accessToken = getAuthorizationInfo.ApiResult.AuthorizationInfo.AccessToken; _timer = new System.Threading.Timer(SendKFMessage, null, 2 * 1000, 1 * 60 * 1000); return(null); } } }
protected override string Handle(ReceivingXMLMessage_TextMessage message, ClientDomainContext domainContext) { //存储消息记录 MessageEntity messageEntity = new MessageEntity(); messageEntity.Receive = true; messageEntity.ReceivingMessageType = EnumReceivingMessageType.Text; messageEntity.OfficalWeixinId = message.ToUserName; messageEntity.MemberOpenId = message.FromUserName; messageEntity.CreateTime = WeixinApiHelper.ConvertIntToDateTime(message.CreateTime); messageEntity.MsgId = message.MsgId; messageEntity.Content = message.Content; _messageManager.AddMessage(domainContext.Domain.Id, domainContext.AppId, messageEntity); bool handled = false; string replyMessageString = null; //判断是否存在关键词自动回复 if (domainContext.AutoReplyOnKeyWords != null && domainContext.AutoReplyOnKeyWords.RuleList != null && domainContext.AutoReplyOnKeyWords.RuleList.Count > 0) { //这里无法直接返回一个string去响应微信服务发出的http请求 //因为那样的话只能回复一条消息,要回复多条消息必须得调用客服接口去发 List <KFMessageBase> replyMessageList = domainContext.AutoReplyOnKeyWords.GetReplyMessage(domainContext, message); if (replyMessageList != null && replyMessageList.Count > 0) { foreach (var item in replyMessageList) { KFApiWrapper.Send(domainContext, item); } handled = true; } } //如果没有命中关键字回复,则看有没有消息自动回复 if (handled == false && domainContext.AutoReplyOnMessage != null) { ResponsiveXMLMessageBase replyMessage = AutoReplyHelper.GetXMLMessage(domainContext, message.FromUserName, domainContext.AutoReplyOnMessage); if (replyMessage != null) { handled = true; replyMessageString = XMLMessageHelper.XmlSerialize(replyMessage); } } return(replyMessageString); }
protected string GetReplyTextMessage(DomainContext domainContext, string toUserName, string content) { ResponsiveXMLMessage_TextMessage replyMessage = new ResponsiveXMLMessage_TextMessage(); replyMessage.Content = content; replyMessage.ToUserName = toUserName; //这几个字段还是要的,因为当直接以HTTP返回的形式返回XML格式的数据时 //是要求这几个字段的 replyMessage.FromUserName = domainContext.UserName; replyMessage.CreateTime = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now); return(XMLMessageHelper.XmlSerialize(replyMessage)); }
protected override string Handle(ReceivingXMLMessage_SubscribeEventMessage message, ClientDomainContext domainContext) { //用户管理-获取用户基本信息 //未认证订阅号 未认证服务号 没有此权限 RequestApiResult <WeixinUser> getUserInfoResult = UserApiWrapper.GetUserInfo(domainContext, message.FromUserName); if (getUserInfoResult.Success == false) { return(String.Empty); } if (getUserInfoResult.ApiResult.Subscribe == 0) { return(String.Empty); } #region 判断是否是扫描带参数二维码事件 //扫描带参数二维码事件 string strScenicQRCodeId = String.Empty; if (String.IsNullOrEmpty(message.EventKey) == false) { //用户未关注时,进行关注后的事件推送 if (message.EventKey.StartsWith("qrscene_")) { strScenicQRCodeId = message.EventKey.Remove(0, "qrscene_".Length); } } //获取场景二维码Id Guid?scenicQRCodeId = null; ScenicQRCodeLandingLogEntity scenicQRCodeLandingLog = null; if (String.IsNullOrEmpty(strScenicQRCodeId) == false) { Guid scenicQRCodeId2; if (Guid.TryParse(strScenicQRCodeId, out scenicQRCodeId2)) { scenicQRCodeId = scenicQRCodeId2; scenicQRCodeLandingLog = new ScenicQRCodeLandingLogEntity(); scenicQRCodeLandingLog.Domain = domainContext.Domain.Id; scenicQRCodeLandingLog.QRCodeId = scenicQRCodeId.Value; scenicQRCodeLandingLog.VisitorOpenId = message.FromUserName; scenicQRCodeLandingLog.LandingTime = DateTime.Now; } } #endregion #region 判断是否是通过RecomendUrl引流关注的 Guid?refereeMemberId = null; //如果有场景二维码ID就不用判断RecomendUrl了 if (String.IsNullOrEmpty(strScenicQRCodeId)) { RecommendUrlLogEntity recomendUrlLog = _recommendUrlManager.GetLogByOpenId(getUserInfoResult.ApiResult.OpenId); if (recomendUrlLog != null) { refereeMemberId = recomendUrlLog.UrlOwnMember; } } #endregion //根据OpenId获取用户信息 MemberEntity member = _memberManager.GetMemberByOpenId(domainContext.Domain.Id, domainContext.AppId, getUserInfoResult.ApiResult.OpenId); AddMemberArgs args = new AddMemberArgs(); args.WeixinUser = getUserInfoResult.ApiResult; args.ScenicQRCodeId = scenicQRCodeId; args.RefereeMemberId = refereeMemberId; if (member == null) { //添加新用户 member = _memberManager.AddMember(domainContext, args); //更新场景二维码计数 if (scenicQRCodeId.HasValue) { _scenicQRCodeManager.IncrementAttentionPerson(scenicQRCodeId.Value, scenicQRCodeLandingLog); } //如果通过RecomendUrl引流关注的,奖励积分 if (refereeMemberId.HasValue && domainContext.RecommendUrlSettings != null && domainContext.RecommendUrlSettings.AttentionPoint > 0) { PointTrackArgs pointTrackArgs = new PointTrackArgs(); pointTrackArgs.DomainId = domainContext.Domain.Id; pointTrackArgs.MemberId = refereeMemberId.Value; pointTrackArgs.Quantity = domainContext.RecommendUrlSettings.AttentionPoint; pointTrackArgs.Type = MemberPointTrackType.RecommendUrl; pointTrackArgs.TagName = member.NickName; pointTrackArgs.TagId = member.Id; _memberManager.PointTrack(pointTrackArgs); } //判断是否有二级上线,对其奖励积分 if (refereeMemberId.HasValue && domainContext.RecommendUrlSettings != null && domainContext.RecommendUrlSettings.Level2AttentionPoint > 0) { RefereeUplineWrapper refereeUplineWrapper = _recommendUrlManager.GetRefereeUplineList(domainContext.Domain.Id, domainContext.AppId, refereeMemberId.Value); if (refereeUplineWrapper.Upline != null) { PointTrackArgs pointTrackArgs = new PointTrackArgs(); pointTrackArgs.DomainId = domainContext.Domain.Id; pointTrackArgs.MemberId = refereeUplineWrapper.Upline.MemberId; pointTrackArgs.Quantity = domainContext.RecommendUrlSettings.Level2AttentionPoint; pointTrackArgs.Type = MemberPointTrackType.RecommendUrl; pointTrackArgs.TagName = member.NickName; pointTrackArgs.TagId = member.Id; _memberManager.PointTrack(pointTrackArgs); } } } else { //更新当前用户信息 _memberManager.UpdateMember(member, args); //更新场景二维码计数 if (scenicQRCodeId.HasValue) { _scenicQRCodeManager.IncrementLanding(scenicQRCodeId.Value, scenicQRCodeLandingLog); } } #region 关注时的自动回复 if (domainContext.AutoReplyOnSubscribe != null && String.IsNullOrEmpty(domainContext.AutoReplyOnSubscribe.Content) == false) { ResponsiveXMLMessageBase replyMessage = AutoReplyHelper.GetXMLMessage(domainContext, message.FromUserName, domainContext.AutoReplyOnSubscribe); if (replyMessage != null) { return(XMLMessageHelper.XmlSerialize(replyMessage)); } else { return(String.Empty); } } else { return(String.Empty); } #endregion }