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);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取XML版的回复消息
        /// </summary>
        /// <param name="domainContext"></param>
        /// <param name="toUserName"></param>
        /// <param name="autoReply"></param>
        /// <returns></returns>
        public static KFMessageBase GetKFMessage(ClientDomainContext domainContext, string toUserName,
                                                 IAutoReply autoReply)
        {
            if (autoReply == null)
            {
                return(null);
            }

            KFMessageBase replyMessage = null;

            switch (autoReply.Type)
            {
            case EnumAutoReplyType.Text:
                //不能返回空白 Content 的XML给微信 API
                //否则它会在客户端提示该公众号暂时无法提供服务
                if (String.IsNullOrEmpty(autoReply.Content) == false)
                {
                    KFTextMessage textMessage = new KFTextMessage();
                    textMessage.Text.Content = autoReply.Content;
                    replyMessage             = textMessage;
                }
                break;

            case EnumAutoReplyType.Image:
                KFImageMessage imageMessage = new KFImageMessage();
                imageMessage.Image.MediaId = autoReply.MediaId;
                replyMessage = imageMessage;
                break;

            case EnumAutoReplyType.Article:
                //微信的图文消息有几种不同的格式,此处专用于自动回复
                //所以只考虑客服接口的内部文章页面格式
                //图文素材可能会被删除
                string mediaId = _materialManager.GetArticleMaterialMediaId(autoReply.ArticleId);
                if (String.IsNullOrEmpty(mediaId) == false)
                {
                    KFMpArticleMessage articleMessage = new KFMpArticleMessage();
                    articleMessage.Mpnews.MediaId = mediaId;
                    replyMessage = articleMessage;
                }
                break;

            default:
                Debug.Assert(false, "GetKFMessage 不支持的 AutoReplyOnKeyWordsRule.Type:" + autoReply.Type.ToString());
                _log.Write("GetKFMessage 不支持的 AutoReplyOnKeyWordsRule.Type:" + autoReply.Type.ToString(),
                           domainContext.AutoReplyOnSubscribe.Type.ToString(), TraceEventType.Error);
                break;
            }

            if (replyMessage != null)
            {
                replyMessage.ToUserName = toUserName;
            }
            return(replyMessage);
        }
Esempio n. 3
0
        public string Handle(string message, ClientDomainContext domainContext)
        {
            if (String.IsNullOrEmpty(message))
            {
                return(null);
            }

            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(message));

            return(Handle(_xmlSerializer.Deserialize(stream) as T, domainContext));
        }
        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);
        }
Esempio n. 5
0
        //TODO:这里应该用 ResponsiveXMLMessage
        /// <summary>
        /// 获取XML版的回复消息
        /// </summary>
        /// <param name="domainContext"></param>
        /// <param name="toUserName"></param>
        /// <param name="autoReply"></param>
        /// <returns></returns>
        public static ResponsiveXMLMessageBase GetXMLMessage(ClientDomainContext domainContext, string toUserName,
                                                             IAutoReply autoReply)
        {
            if (autoReply == null)
            {
                return(null);
            }

            ResponsiveXMLMessageBase replyMessage = null;

            switch (autoReply.Type)
            {
            case EnumAutoReplyType.Text:
                //不能返回空白 Content 的XML给微信 API
                //否则它会在客户端提示该公众号暂时无法提供服务
                if (String.IsNullOrEmpty(autoReply.Content) == false)
                {
                    ResponsiveXMLMessage_TextMessage textMessage = new ResponsiveXMLMessage_TextMessage();
                    textMessage.Content = autoReply.Content;
                    replyMessage        = textMessage;
                }
                break;

            case EnumAutoReplyType.Image:
                if (String.IsNullOrEmpty(autoReply.MediaId) == false)
                {
                    ResponsiveXMLMessage_ImageMessage imageMessage = new ResponsiveXMLMessage_ImageMessage();
                    imageMessage.Image.MediaId = autoReply.MediaId;
                    replyMessage = imageMessage;
                }
                break;

            default:
                Debug.Assert(false, "GetXMLMessage 不支持的 AutoReplyOnKeyWordsRule.Type:" + autoReply.Type.ToString());
                _log.Write("GetXMLMessage 不支持的 AutoReplyOnKeyWordsRule.Type:" + autoReply.Type.ToString(),
                           domainContext.AutoReplyOnSubscribe.Type.ToString(), TraceEventType.Error);
                break;
            }

            if (replyMessage != null)
            {
                replyMessage.ToUserName = toUserName;
                //这几个字段还是要的,因为当直接以HTTP返回的形式返回XML格式的数据时
                //是要求这几个字段的
                replyMessage.FromUserName = domainContext.UserName;
                replyMessage.CreateTime   = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now);
            }
            return(replyMessage);
        }
        public string Handle(string message, ClientDomainContext domainContext)
        {
            //对于普通消息 MsgType 就要以区分消息的类型了
            //但是对于事件消息,需要通过另外一个字段来区分事件的类型

            XElement xml = XElement.Parse(message);

            string messageType = xml.XPathSelectElement("MsgType").Value;

            if (String.IsNullOrEmpty(messageType))
            {
                return(String.Empty);
            }

            //事件消息
            if (messageType == "event")
            {
                string eventType = xml.XPathSelectElement("Event").Value;
                if (String.IsNullOrEmpty(eventType))
                {
                    return(String.Empty);
                }

                foreach (var receiver in _receiverList)
                {
                    if (receiver.Event == eventType)
                    {
                        return(receiver.Handle(message, domainContext));
                    }
                }
            }
            //普通消息
            else
            {
                foreach (var receiver in _receiverList)
                {
                    if (receiver.MsgType == messageType)
                    {
                        return(receiver.Handle(message, domainContext));
                    }
                }
            }

            return(String.Empty);
        }
Esempio n. 7
0
        protected override string Handle(ReceivingXMLMessage_LinkMessage message, ClientDomainContext domainContext)
        {
            //存储消息记录
            MessageEntity messageEntity = new MessageEntity();

            messageEntity.Receive = true;
            messageEntity.ReceivingMessageType = EnumReceivingMessageType.Link;

            messageEntity.OfficalWeixinId = message.ToUserName;
            messageEntity.MemberOpenId    = message.FromUserName;
            messageEntity.CreateTime      = WeixinApiHelper.ConvertIntToDateTime(message.CreateTime);
            messageEntity.MsgId           = message.MsgId;

            messageEntity.Link_Title       = message.Title;
            messageEntity.Link_Description = message.Description;
            messageEntity.Link_Url         = message.Url;

            _messageManager.AddMessage(domainContext.Domain.Id, domainContext.AppId, messageEntity);

            return(null);
        }
        protected override string Handle(ReceivingXMLMessage_ImageMessage message, ClientDomainContext domainContext)
        {
            //存储消息记录
            MessageEntity messageEntity = new MessageEntity();

            messageEntity.Receive = true;
            messageEntity.ReceivingMessageType = EnumReceivingMessageType.Image;

            messageEntity.OfficalWeixinId = message.ToUserName;
            messageEntity.MemberOpenId    = message.FromUserName;
            messageEntity.CreateTime      = WeixinApiHelper.ConvertIntToDateTime(message.CreateTime);
            messageEntity.MsgId           = message.MsgId;

            messageEntity.Image_PicUrl = message.PicUrl;
            messageEntity.MediaId      = message.MediaId;

            _messageManager.AddMessage(domainContext.Domain.Id, domainContext.AppId, messageEntity);

            //向文件服务器发起文件异步下载请求
            FileDownloadQueueWithMediaIdArgs args = new FileDownloadQueueWithMediaIdArgs();

            args.Domain      = domainContext.Domain.Id;
            args.AppId       = domainContext.AppId;
            args.MediaId     = message.MediaId;
            args.CallbackUrl = _fileDownloadCallbackUrl;
            args.Tag         = messageEntity.Id.ToString();

            ApiResult apiResult = _fileService.DownloadQueueWithMediaId(args);

            if (apiResult.Success == false)
            {
                _log.Write("队列下载请求失败", apiResult.Message, TraceEventType.Warning);
            }

            return(null);
        }
 protected override string Handle(ReceivingXMLMessage_GroupMessageFinishEvent message, ClientDomainContext domainContext)
 {
     _groupMessageManager.Finish(domainContext.Domain.Id, domainContext.AppId, message);
     return(null);
 }
Esempio n. 10
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
        }
        protected override string Handle(ReceivingXMLMessage_ScanEventMessage message, ClientDomainContext domainContext)
        {
            //扫描带参数二维码事件
            if (String.IsNullOrEmpty(message.EventKey))
            {
                return(String.Empty);
            }

            string strScenicQRCodeId = message.EventKey;

            //获取场景二维码Id
            Guid scenicQRCodeId;

            if (Guid.TryParse(strScenicQRCodeId, out scenicQRCodeId))
            {
                ScenicQRCodeLandingLogEntity log = new ScenicQRCodeLandingLogEntity();
                log.Domain        = domainContext.Domain.Id;
                log.QRCodeId      = scenicQRCodeId;
                log.VisitorOpenId = message.FromUserName;
                log.LandingTime   = DateTime.Now;
                _scenicQRCodeManager.IncrementLanding(scenicQRCodeId, log);
            }

            return(String.Empty);
        }
Esempio n. 12
0
 protected abstract string Handle(T message, ClientDomainContext domainContext);
Esempio n. 13
0
        protected override string Handle(ReceivingXMLMessage_UnsubscribeEventMessage message, ClientDomainContext domainContext)
        {
            _memberManager.UnsubscribeMember(domainContext.Domain.Id, domainContext.AppId, message.FromUserName);

            //清除该用户当前Session
            SessionPool.Instance.Abandon(message.FromUserName);

            return(String.Empty);
        }
        protected override string Handle(ReceivingXMLMessage_ClickEventMessage message, ClientDomainContext domainContext)
        {
            if (String.IsNullOrEmpty(message.EventKey))
            {
                return(String.Empty);
            }

            string[] keyArray = message.EventKey.Split(':');

            switch (keyArray[0])
            {
            case "MemberQRCode":


                Guid campaignId = Guid.Empty;
                if (Guid.TryParse(keyArray[1], out campaignId) == false)
                {
                    string replyMessage = GetReplyTextMessage(domainContext, message.FromUserName, "campaignId 参数无效:" + keyArray[1]);
                    return(replyMessage);
                }

                //  Campaign_MemberQRCodeEntity campaign_MemberQRCodeEntity = _campaignManager.GetMemberQRCode();
                MemberEntity memberEntity = _memberManager.GetMemberByOpenId(domainContext.Domain.Id, domainContext.AppId, message.FromUserName);
                Campaign_MemberQRCodeItemEntity memberQRCodeItem =
                    _campaignManager.MemberQRCode.GetMemberQRCodeItem(Guid.Parse(keyArray[0]), memberEntity.Id);
                if (memberQRCodeItem == null)
                {
                }
                break;

            default:
                break;
            }

            return(String.Empty);
        }