protected virtual IReplyMessage WeChat_OnReceiveMessage(IIncomeMessage msg)
 {
     if (ReplyHandles == null)
     {
         ReplyHandles = CreateReplyChain();
     }
     return(ReplyHandles.Reply(msg));
 }
Exemple #2
0
 public static ReplyNewsMessage CreateNewsReply(IIncomeMessage msgReplyTo, List <Article> articles)
 {
     return(new ReplyNewsMessage()
     {
         Articles = articles,
         CreateTime = DateTime.Now,
         FromUserName = msgReplyTo.ToUserName,
         ToUserName = msgReplyTo.FromUserName,
         MsgType = "news"
     });
 }
Exemple #3
0
        /// <summary>
        /// 异常处理记日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        protected virtual IReplyMessage WeChat_OnProcessMessageError(IIncomeMessage msg, Exception e)
        {
            string errMsg = string.Empty;

            if (msg != null)
            {
                errMsg = string.Format("Process {0} msg failed from:[{1}],", msg.MsgType, msg.FromUserName);
            }
            Nlab.WeChatApi.Logger.NlabLogger.LogError(e, errMsg);
            return(null);
        }
Exemple #4
0
 public static ReplyTextMessage CreateTextReply(IIncomeMessage msgReplyTo, string content, int funcFlag = 0)
 {
     return(new ReplyTextMessage()
     {
         Content = content,
         CreateTime = DateTime.Now,
         FromUserName = msgReplyTo.ToUserName,
         ToUserName = msgReplyTo.FromUserName,
         FuncFlag = funcFlag,
         MsgType = "text"
     });
 }
Exemple #5
0
        /// <summary>
        /// 非重复消息判定
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected virtual bool IsDuplicated(IIncomeMessage msg)
        {
            var      key = string.Format("{0}{1}{2:yyMMddHHmmss}", msg.MsgType, msg.FromUserName, msg.CreateTime);
            DateTime dt;

            if (!DuplicateMsgTab.TryGetValue(key, out dt))
            {
                DuplicateMsgTab.TryAdd(key, DateTime.Now);
                CleanDuplicateCacheData();
                return(false);
            }
            return(true);
        }
Exemple #6
0
 public virtual bool Predict(IIncomeMessage msg)
 {
     return
         //!IsDuplicated(msg) &&
         (MatchType.Contains(msg.MsgType) &&
          (
              (
                  MatchesList != null && IsMatchs(msg)
              ) ||
              IsMatchStartWith(msg)
              //(StartWith != null && msg.IsText() && (msg as TextMessage).Content.StartsWith(StartWith))
          ));
 }
Exemple #7
0
 public static ReplyMusicMessage CreateMusicReply(IIncomeMessage msgReplyTo, string title, string description, string musicUrl, string musicHQUrl, string thumbMediaId)
 {
     return(new ReplyMusicMessage()
     {
         MusicUrl = musicUrl,
         HQMusicUrl = musicHQUrl,
         CreateTime = DateTime.Now,
         FromUserName = msgReplyTo.ToUserName,
         ToUserName = msgReplyTo.FromUserName,
         //FuncFlag = funcFlag,
         ThumbMediaId = thumbMediaId,
         MsgType = "music",
         Title = title,
         Description = description
     });
 }
Exemple #8
0
        private bool IsMatchStartWith(IIncomeMessage msg)
        {
            if (string.IsNullOrEmpty(StartWith) || !msg.IsText())
            {
                return(false);
            }
            var spl     = StartWith.Split(',', ';');
            var content = (msg as TextMessage).Content;

            foreach (var s in spl)
            {
                if (content.StartsWith(s))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #9
0
 private bool IsMatchs(IIncomeMessage msg)
 {
     if (msg.MsgType == "text")
     {
         return(MatchesList.Contains((msg as TextMessage).Content.ToLower()) || MatchText == WidthCast);
     }
     if (msg.GetMsgType() == MessageTypes.MenuClick)
     {
         return(MatchesList.Contains((msg as MenuClickEventMessage).EventKey.ToLower()));
     }
     if (msg.GetMsgType() == MessageTypes.QRScan)
     {
         return(MatchesList.Contains((msg as QrScanEventMessage).EventKey.ToLower()));
     }
     if (msg.MsgType == "event")
     {
         return(MatchType.Contains((msg as EventMessage).Event));
     }
     return(false);
 }
Exemple #10
0
        public IReplyMessage Reply(IIncomeMessage msg)
        {
            if (index >= handlerList.Count)
            {
                index = 0;
                return(null);
            }
            var current = handlerList[index];

            index++;
            IReplyMessage reply = null;

            if (current.Predict(msg))
            {
                reply = current.Reply(msg);
            }
            if (reply == null)
            {
                return(Reply(msg));
            }
            index = 0;
            return(reply);
        }
Exemple #11
0
 public abstract IReplyMessage Reply(IIncomeMessage msg);
Exemple #12
0
        /// <summary>
        /// 消息处理流程
        /// </summary>
        /// <param name="msgBag"></param>
        /// <returns></returns>
        protected virtual string ProcessMessageReceive(Dictionary <string, string> msgBag)
        {
            IReplyMessage  reply = null;
            IIncomeMessage msg   = null;

            if (!msgBag.ContainsKey("MsgType"))
            {
                throw new WechatApiException(-3, "received unexpected message type.");
            }
            try
            {
                switch (msgBag["MsgType"])
                {
                case "text":
                {
                    msg   = MessageHelper.Create <TextMessage>(msgBag);
                    reply = OnReceiveTextMessage(msg as TextMessage);
                    break;
                }

                case "image":
                {
                    msg   = MessageHelper.Create <ImageMessage>(msgBag);
                    reply = OnReceiveImageMessage(msg as ImageMessage);
                    break;
                }

                case "voice":
                {
                    msg   = MessageHelper.Create <VoiceMessage>(msgBag);
                    reply = OnReceiveVoiceMessage(msg as VoiceMessage);
                    break;
                }

                case "video":
                {
                    msg   = MessageHelper.Create <VideoMessage>(msgBag);
                    reply = OnReceiveVideoMessage(msg as VideoMessage);
                    break;
                }

                //case "location":
                //    {
                //        msg = MessageHelper.Create<LocationMessage>(msgBag);
                //        reply = OnReceiveLocationMessage(msg as LocationMessage);
                //        break;
                //    }
                case "link":
                {
                    msg   = MessageHelper.Create <LinkMessage>(msgBag);
                    reply = OnReceiveLinkMessage(msg as LinkMessage);
                    break;
                }

                case "event":
                {
                    var eventType = msgBag["Event"].ToLower();
                    switch (eventType)
                    {
                    case "click":
                    case "view":
                        msg   = MessageHelper.Create <MenuClickEventMessage>(msgBag);
                        reply = OnReceiveMenuClickMessage(msg as MenuClickEventMessage);
                        break;

                    case "subscribe":
                        msg   = MessageHelper.Create <SubscribeEventMessage>(msgBag);
                        reply = OnReceiveSubscribeMessage(msg as SubscribeEventMessage);
                        break;

                    case "unsubscribe":
                        msg   = MessageHelper.Create <UnSubscribeEventMessage>(msgBag);
                        reply = OnReceiveUnSubscribeMessage(msg as UnSubscribeEventMessage);
                        break;

                    case "scan":
                        msg   = MessageHelper.Create <QrScanEventMessage>(msgBag);
                        reply = OnReceiveQrScanMessage(msg as QrScanEventMessage);
                        break;

                    case "location":
                        msg   = MessageHelper.Create <LocationEventMessage>(msgBag);
                        reply = OnReceiveLocationMessage(msg as LocationEventMessage);
                        break;

                    default:
                        msg   = MessageHelper.Create <EventMessage>(msgBag);
                        reply = OnReceiveEventMessage(msg as EventMessage);
                        break;
                    }
                    break;
                }
                }
                if (reply == null && msg != null && OnReceiveMessage != null)
                {
                    reply = OnReceiveMessage(msg);
                }

                if (reply != null)
                {
                    switch (reply.MsgType)
                    {
                    case "text":
                    {
                        return(MessageHelper.TransReply(reply as ReplyTextMessage));
                    }

                    case "music":
                    {
                        return(MessageHelper.TransReply(reply as ReplyMusicMessage));
                    }

                    case "news":
                    {
                        return(MessageHelper.TransReply(reply as ReplyNewsMessage));
                    }
                    }
                }
                return(string.Empty);
            }
            catch (Exception ex)
            {
                if (OnProcessMessageError != null)
                {
                    OnProcessMessageError(msg, ex);
                }
                return(string.Empty);
            }
        }