Beispiel #1
0
        public static void WalkThroughSchema(long currentChatId, string userInput, LibConfigurationModule config, CallbackQuery callbackQuery)
        {
            using (var ctx = (LibDbContext)Activator.CreateInstance(config.DbContextType))
            {
                Chat chat = GetChatById(currentChatId, config, ctx);

                do
                {
                    if (callbackQuery != null)
                    {
                        TelegramActions.removeInlineKeyboard(currentChatId, callbackQuery, config.BotClient);
                        callbackQuery = null;
                    }

                    if (chat.State.ProcessedUserInput)
                    {
                        userInput = null;
                    }

                    LibActionResult processedResult = ProcessChatState(chat, userInput, config);
                    if (!processedResult.Status)
                    {
                        string errMsg = processedResult.ErrorMessage ?? chat.State.CurrentMessage.ErrorHandlingMessage;
                        TelegramActions.sendMessage(chat.СhatId, errMsg, null, config.BotClient);
                        break;
                    }
                    StepForward(chat, userInput, config);

                    if (chat.State.CurrentMessage.Type == MessageType.saveUserInput || chat.State.WaitForUserTransition)
                    {
                        chat.State.ProcessedUserInput = false;
                        break;
                    }
                }while (true);

                ctx.SaveChanges();
            }

            return;
        }
Beispiel #2
0
        public static LibActionResult ProcessChatState(Chat chat, string userInput, LibConfigurationModule config)
        {
            if (chat.State.WaitForUserTransition)
            {
                return(new LibActionResult()
                {
                    Status = true
                });
            }

            Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup customKeyboard = null;
            if (chat.State.CurrentMessage == chat.State.CurrentMessageBlock.Messages.Last())
            {
                var possibleSteps = chat.State.Schema.Steps.Where(x => x.FromBlock == chat.State.CurrentMessageBlock).ToList();
                if (possibleSteps.Count >= 2)
                {
                    chat.State.WaitForUserTransition = true;
                    if (chat.State.CurrentMessage.Type != MessageType.saveUserInput || chat.State.HasBeenAtLastMessage)
                    {
                        customKeyboard = KeyboardManagement.createReplyKeyboard(possibleSteps);
                    }
                }
            }

            switch (chat.State.CurrentMessage.Type)
            {
            case MessageType.sendMessage:
                if (!String.IsNullOrEmpty(chat.State.CurrentMessage.Content))
                {
                    TelegramActions.sendMessage(chat.СhatId, chat.State.CurrentMessage.Content, customKeyboard, config.BotClient);
                }
                else if (chat.State.CurrentMessage.TextWithProperties != null)
                {
                    TelegramActions.sendMessage(chat.СhatId, chat.State.CurrentMessage.TextWithProperties(chat), customKeyboard, config.BotClient);
                }
                return(new LibActionResult()
                {
                    Status = true
                });

            case MessageType.sendImage:
                TelegramActions.sendImage(chat.СhatId, chat.State.CurrentMessage.Content, customKeyboard, config.BotClient);
                return(new LibActionResult()
                {
                    Status = true
                });

            case MessageType.saveUserInput:
                if (chat.State.CurrentMessage.CustomMethod != null)
                {
                    var result = chat.State.CurrentMessage.CustomMethod(userInput, chat, config.BotClient);
                    chat.State.ProcessedUserInput = result.Status;
                    return(result);
                }
                else if (chat.State.CurrentMessage.PropertySetter != null)
                {
                    LibActions.SetDataContextProperty(chat, userInput, chat.State.CurrentMessage.PropertySetter);
                    chat.State.ProcessedUserInput = true;
                    return(new LibActionResult()
                    {
                        Status = true
                    });
                }
                else
                {
                    return(new LibActionResult()
                    {
                        Status = false, ErrorMessage = "Действие не задано"
                    });
                }

            case MessageType.pause:
                TelegramActions.sendMessage(chat.СhatId, "Допустим, что тут будет пауза.", customKeyboard, config.BotClient);
                return(new LibActionResult()
                {
                    Status = true
                });

            case MessageType.Custom:
                if (chat.State.CurrentMessage.CustomMethod != null)
                {
                    return(chat.State.CurrentMessage.CustomMethod(userInput, chat, config.BotClient));
                }
                else if (chat.State.CurrentMessage.PropertySetter != null)
                {
                    LibActions.SetDataContextProperty(chat, userInput, chat.State.CurrentMessage.PropertySetter);
                    return(new LibActionResult()
                    {
                        Status = true
                    });
                }
                else
                {
                    TelegramActions.sendMessage(chat.СhatId, "Выполнилась кастомная хрень.", customKeyboard, config.BotClient);
                    return(new LibActionResult()
                    {
                        Status = true
                    });
                }

            default:
                return(new LibActionResult()
                {
                    Status = false
                });
            }
        }