private async Task HandleUpdateAsync(Update update, CancellationToken ct)
        {
            using var scope = this.scopeFactory.CreateScope();
            var context = (MarketplaceContext)scope.ServiceProvider.GetService(typeof(MarketplaceContext));

            var(userId, command, messageId) = UpdateHelpers.GetUserAndCommand(update);
            var user = await context.Users.FirstOrDefaultAsync(u => u.TelegramId == userId);

            if (user != null && user.IsBanned)
            {
                return;
            }

            var userContext = this.userContextService.GetUserContext(user, userId);

            PersonifiedUpdate personifiedUpdate = new PersonifiedUpdate()
            {
                Update      = update,
                UserContext = userContext,
                Command     = command,
                MessageId   = messageId,
            };

            var updateHandler = (IBotUpdateService)scope.ServiceProvider.GetService(typeof(IBotUpdateService));
            await updateHandler.ProceedUpdate(personifiedUpdate);
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public Type FindCommand(PersonifiedUpdate update)
        {
            var types = new List <System.Type>();

            var userState = update.UserContext.GetContext <UserStateEnum>(UserContextEnum.UserState);

            if (this.routes.ContainsKey(userState))
            {
                foreach (var commandPattern in this.routes[userState])
                {
                    if (Regex.IsMatch(update.Command, commandPattern.Pattern, RegexOptions.IgnoreCase))
                    {
                        types.Add(Type.GetType(commandPattern.Type));
                        break;
                    }
                }
            }

            if (types.Count > 1)
            {
                this.logger.LogWarning($"Found more than one routes for command - {update.Command}");
            }

            return(!types.Any() ? typeof(MainMenuCommand) : types.First());
        }
        /// <inheritdoc/>
        public override async Task <OperationExecutionResult> Execute(PersonifiedUpdate update)
        {
            this.Update = update;

            update.UserContext.SetContext(UserContextEnum.UserState, UserStateEnum.TurnipMarketVisitor);

            var isCreating = update.UserContext.GetContext <bool>("CreatingTMV");
            var isEditing  = update.UserContext.GetContext <bool>("ChangingTMV");

            if (update.Command == "/CreateTMV" || isCreating)
            {
                this.Update.UserContext.SetContext("CreatingTMV", true);
                await this.CreateTMV();

                return(OperationExecutionResult.Success);
            }

            if (update.Command.StartsWith("/Change") || isEditing)
            {
                this.Update.UserContext.SetContext("ChangingTMV", true);
                await this.EditTMV();

                return(OperationExecutionResult.Success);
            }

            if (update.Command.StartsWith("/DeleteTMV"))
            {
                await this.DeleteVisitor();
            }

            await this.ManageTMV();

            return(OperationExecutionResult.Success);
        }
        /// <inheritdoc/>
        public override async Task <OperationExecutionResult> Execute(PersonifiedUpdate update)
        {
            update.UserContext.SetContext(UserContextEnum.UserState, UserStateEnum.TurnipMarket);
            update.UserContext.RemoveContext("TMHId");
            update.UserContext.RemoveContext("TMVId");

            await this.Client.EditMessageAsync(
                update.UserContext.TelegramId,
                update.MessageId,
                "What do you wish for right now?",
                new InlineKeyboardMarkup(
                    new[]
            {
                new[] { new InlineKeyboardButton()
                        {
                            CallbackData = "/ManageTMH", Text = "Manage hosted markets"
                        } },
                new[] { new InlineKeyboardButton()
                        {
                            CallbackData = "/CreateTMH", Text = "Crate hosted market"
                        } },
                new[] { new InlineKeyboardButton()
                        {
                            CallbackData = "/ManageTMV", Text = "Manage visit applications"
                        } },
                new[] { new InlineKeyboardButton()
                        {
                            CallbackData = "/BackMainMenu", Text = "<- Back"
                        } },
            }));

            return(OperationExecutionResult.Success);
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public override async Task <OperationExecutionResult> Execute(PersonifiedUpdate update)
        {
            this.Update = update;

            update.UserContext.SetContext(UserContextEnum.UserState, UserStateEnum.TurnipMarketEntryFee);

            var isCreating = update.UserContext.GetContext <bool>("CreatingTMEF");
            var isEditing  = update.UserContext.GetContext <bool>("ChangingTMEF");

            try
            {
                if (update.Command == "/ChangeEntryFee" ||
                    update.Command.StartsWith("/ManageTMEF"))
                {
                    await this.ManageTMEF();

                    return(OperationExecutionResult.Success);
                }

                if (update.Command == "/CreateTMEF" || isCreating)
                {
                    this.Update.UserContext.SetContext("CreatingTMEF", true);
                    await this.CreateTMEF();

                    return(OperationExecutionResult.Success);
                }

                if (update.Command.StartsWith("/Change") || isEditing)
                {
                    this.Update.UserContext.SetContext("ChangingTMEF", true);
                    await this.EditTMEF();

                    return(OperationExecutionResult.Success);
                }

                if (update.Command.StartsWith("/Delete"))
                {
                    await this.DeleteEntryFee();

                    await this.ManageTMEF();

                    return(OperationExecutionResult.Success);
                }
            }
            catch (Exception ex)
            {
                if (ex is ArgumentException)
                {
                    this.Update.Command = "/BackTMMM";
                    return(OperationExecutionResult.Reroute);
                }

                throw ex;
            }

            return(OperationExecutionResult.Success);
        }
Exemplo n.º 6
0
        /// <inheritdoc/>
        public override async Task <OperationExecutionResult> Execute(PersonifiedUpdate update)
        {
            this.Update    = update;
            this.UserState = update.UserContext.GetContext <UserStateEnum>(UserContextEnum.UserState);

            switch (this.UserState)
            {
            case UserStateEnum.NotRegistered:
                await this.RegistrateUser();

                break;

            case UserStateEnum.ProfileMain:
                if (update.Command == "/BackMainMenu")
                {
                    update.UserContext.SetContext(UserContextEnum.UserState, UserStateEnum.MainPage);
                    return(OperationExecutionResult.Reroute);
                }
                else if (update.Command.StartsWith("/Change") || update.Command.StartsWith("/Delete"))
                {
                    await this.ProfileEdit();
                }
                else
                {
                    await this.ProfileMain();
                }

                break;

            case UserStateEnum.ProfileEdit:
                if (update.Command == "/BackMainMenu")
                {
                    update.UserContext.SetContext(UserContextEnum.UserState, UserStateEnum.MainPage);
                    return(OperationExecutionResult.Reroute);
                }
                else if (update.Command == "/ProfileMain")
                {
                    await this.ProfileMain();
                }
                else
                {
                    await this.ProfileEdit();
                }

                break;
            }

            return(OperationExecutionResult.Success);
        }
Exemplo n.º 7
0
 /// <inheritdoc/>
 public async Task ProceedUpdate(PersonifiedUpdate update)
 {
     try
     {
         var result = OperationExecutionResult.Reroute;
         do
         {
             using var scope = this.scopeFactory.CreateScope();
             var type    = this.commandRouter.FindCommand(update);
             var command = (ICommand)scope.ServiceProvider.GetService(type);
             result = await command.Execute(update);
         }while (result == OperationExecutionResult.Reroute);
     }
     catch (Exception ex)
     {
         if (!(ex is CommandNotFoundException))
         {
             this.logger.LogError(ex, "Unhandled exception");
         }
     }
 }
Exemplo n.º 8
0
        /// <inheritdoc/>
        public override async Task <OperationExecutionResult> Execute(PersonifiedUpdate update)
        {
            this.Update = update;
            update.UserContext.SetContext(UserContextEnum.UserState, UserStateEnum.MainPage);

            if (update.Command.Equals("/ProfileMain", StringComparison.OrdinalIgnoreCase))
            {
                update.UserContext.SetContext(UserContextEnum.UserState, UserStateEnum.ProfileMain);
                return(OperationExecutionResult.Reroute);
            }

            if (update.Command.Equals("/ImActive", StringComparison.OrdinalIgnoreCase))
            {
                await this.ExtendUserActivity();

                return(OperationExecutionResult.Success);
            }

            await this.Client.EditMessageAsync(
                update.UserContext.TelegramId,
                update.MessageId,
                "What do you wish for right now?",
                new InlineKeyboardMarkup(new[]
            {
                new[] { new InlineKeyboardButton()
                        {
                            CallbackData = "/TurnipMarket", Text = "Turnip market"
                        } },
                new[] { new InlineKeyboardButton()
                        {
                            CallbackData = "/ProfileMain", Text = "Edit profile"
                        } },
                new[] { new InlineKeyboardButton()
                        {
                            CallbackData = "/ImActive", Text = "I'm active right now!"
                        } },
            }));

            return(OperationExecutionResult.Success);
        }
        /// <inheritdoc/>
        public override async Task <OperationExecutionResult> Execute(PersonifiedUpdate update)
        {
            this.Update = update;
            update.UserContext.SetContext(UserContextEnum.UserState, UserStateEnum.TurnipMarketFinder);

            var backToTMMM = true;

            if (this.Update.Command.StartsWith("/FindHoster"))
            {
                backToTMMM = await this.FindHoster();
            }
            else if (this.Update.Command.StartsWith("/FindVisitor"))
            {
                backToTMMM = await this.FindVisitor();
            }
            else if (this.Update.Command.StartsWith("/HosterDescription"))
            {
                backToTMMM = await this.ViewHosterDescription();
            }
            else if (this.Update.Command.StartsWith("/VisitorDescription"))
            {
                backToTMMM = await this.ViewVisitorDescription();
            }
            else if (this.Update.Command.StartsWith("/Report"))
            {
                await this.ReportEntry();

                backToTMMM = false;
            }

            if (!backToTMMM)
            {
                return(OperationExecutionResult.Success);
            }

            this.Update.Command = "/BackTMMM";
            return(OperationExecutionResult.Reroute);
        }
        public async Task <IActionResult> Post([FromBody] Update update)
        {
            var(userId, command, messageId) = UpdateHelpers.GetUserAndCommand(update);
            var user = await this.context.Users.FirstOrDefaultAsync(u => u.TelegramId == userId);

            if (user != null && user.IsBanned)
            {
                return(this.Ok());
            }

            var userContext = this.userContextService.GetUserContext(user, userId);

            PersonifiedUpdate personifiedUpdate = new PersonifiedUpdate()
            {
                Update      = update,
                UserContext = userContext,
                Command     = command,
                MessageId   = messageId,
            };

            await this.botUpdate.ProceedUpdate(personifiedUpdate);

            return(this.Ok());
        }
Exemplo n.º 11
0
 /// <inheritdoc/>
 public abstract Task <OperationExecutionResult> Execute(PersonifiedUpdate update);