Exemplo n.º 1
0
        private void BotClient_OnMessage(object sender, MessageEventArgs e)
        {
            try
            {
                Message message = e ? .Message;

                if (message is null)
                {
                    return;
                }

                TUser           user = UserProvider.GetUser(message.From.Id);
                Session <TUser> currentSession;
                object          tag = null;

                if (user is null)
                {
                    if (message.Chat.Type == ChatType.Private)
                    {
                        currentSession = SessionProvider.GetSession(message.From.Id);
                        currentSession.PrivateChatId = message.Chat.Id;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    currentSession = SessionProvider.GetSession(user);
                    if (message.Chat.Type == ChatType.Private)
                    {
                        currentSession.PrivateChatId = message.Chat.Id;
                    }
                }

                currentSession.BotClient = BotClient;

                ICommand <TUser> routeTarget      = currentSession.RouteBind;
                string []        args             = default;
                bool             isExactlyMatched = false;

                string text = message.Text ? .Normalize(NormalizationForm.FormKD) ? .Trim( );

                if (routeTarget is null)
                {
                    if (string.IsNullOrWhiteSpace(text))
                    {
                        return;
                    }

                    if (text.First( ) == '/')
                    {
                        text = text.TrimStart('/');

                        args = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                        if (args.Length > 0)
                        {
                            string commandName = args.First( ).GetCommandName( );

                            routeTarget = CommandProvider.GetCommand(commandName, currentSession);

                            if (routeTarget is null)
                            {
                                routeTarget      = CommandProvider.GetCommandFuzzy(commandName, currentSession);
                                isExactlyMatched = false;
                            }
                            else
                            {
                                isExactlyMatched = true;
                            }
                        }
                        else
                        {
                            routeTarget      = EmptyCommand <TUser> .Current;
                            isExactlyMatched = false;
                        }
                    }
                    else
                    {
                        //no command
                        //
                        if (message.Chat.Type == ChatType.Private)
                        {
                            routeTarget      = EmptyCommand <TUser> .Current;
                            isExactlyMatched = false;
                        }
                    }
                }
                else
                {
                    args = text ? .Split(' ', StringSplitOptions.RemoveEmptyEntries) ?? new string [] { };

                    isExactlyMatched = true;
                }

                if (!(routeTarget is null))
                {
                    if (!PermissionProvider.IsAllowedToInvoke(user, routeTarget.PermissionGroup))
                    {
                        tag         = routeTarget;
                        routeTarget = PermissionDeniedCommand <TUser> .Current;
                    }

                    TaskDispatcher.Dispatch(
                        new OnetimeTask(
                            ()
                            => routeTarget.Process(
                                message,
                                args ?? new string [] { },
                                currentSession,
                                isExactlyMatched,
                                tag),
                            routeTarget.Timeout));
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }