コード例 #1
0
        public List <KFMessageBase> GetReplyMessage(ClientDomainContext domainContext, ReceivingXMLMessage_TextMessage userMessage)
        {
            if (userMessage == null || String.IsNullOrEmpty(userMessage.Content))
            {
                return(null);
            }

            if (RuleList == null || RuleList.Count == 0)
            {
                return(null);
            }

            List <KFMessageBase> messageList = new List <KFMessageBase>();

            foreach (AutoReplyOnKeyWordsRuleEntity rule in RuleList)
            {
                if (rule.IsMatch(userMessage.Content))
                {
                    //判断是随机回复一条还是回复全部
                    if (rule.ReplyAll)
                    {
                        foreach (var content in rule.ContentList)
                        {
                            if (content == null)
                            {
                                continue;
                            }

                            KFMessageBase message =
                                AutoReplyHelper.GetKFMessage(domainContext, userMessage.FromUserName, content);

                            if (message != null)
                            {
                                messageList.Add(message);
                            }
                        }
                    }
                    else
                    {
                        Random random = new Random(DateTime.Now.Millisecond);
                        AutoReplyOnKeyWordsContentEntity content =
                            rule.ContentList[random.Next(0, rule.ContentList.Count)];

                        KFMessageBase message =
                            AutoReplyHelper.GetKFMessage(domainContext, userMessage.FromUserName, content);

                        if (message != null)
                        {
                            messageList.Add(message);
                        }
                    }
                    break;
                }
            }

            return(messageList);
        }
        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);
        }
コード例 #3
0
        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
        }