Ejemplo n.º 1
0
        private Task <bool> ProcessAdminCommand(ITgInboundMessage tLInboundMessage)
        {
            foreach (var VARIABLE in tLInboundMessage.Message.Split(" ").Skip(1))
            {
                //                int
            }

            return(Task.FromResult(true));
        }
Ejemplo n.º 2
0
        private bool IsAdminCommand(ITgInboundMessage tLInboundMessage)
        {
            if (BotSettings.AdminIds == null)
            {
                return(false);
            }

            if (BotSettings.AdminIds.All(id => id != tLInboundMessage.ChatId))
            {
                return(false);
            }

            return(_adminCommands.Any(command => tLInboundMessage.Message.ToLower().StartsWith(command)));
        }
Ejemplo n.º 3
0
        private async void OnMessage(object sender, ITgInboundMessage tLInboundMessage)
        {
            Trace.TraceInformation($"{tLInboundMessage.ChatId} {tLInboundMessage.Message}");
            string message = tLInboundMessage.Message;
            long   chatId  = tLInboundMessage.ChatId;

            if (IsAdminCommand(tLInboundMessage))
            {
                return;
            }

            using ITgChatsRepository chatsRepository = _dbService.GetChatsRepository();

            ChatInfoEntity chatInfo = await chatsRepository.Get(chatId)
                                      ?? await chatsRepository.Create(chatId, "ru", _cancellationTokenSource.Token);

            chatInfo.LastMessage = DateTime.Now;

            //check if user wants to return at first
            var startCmd = _commands.First();

            if (startCmd.IsMessageCorrect(message))
            {
                using (var usersRepository = _dbService.GetUsersRepository())
                {
                    var userEntity = usersRepository.Get(chatInfo.ChatId);
                    if (null == userEntity)
                    {
                        await usersRepository.Create(chatInfo.ChatId, chatInfo.Culture, _cancellationTokenSource.Token);
                    }
                }

                chatInfo.Clear();
            }

            var prevCmd = GetPreviousCommand(chatInfo);

            if (message.ToLower().StartsWith("нет") || (prevCmd?.IsReturnCommand(message) ?? false))
            {
                var prevCommand = GetPreviousCommand(chatInfo);

                if (prevCommand == null)
                {
                    return;
                }

                chatInfo.DialogState     = DialogStateEnum.DialogReturned;
                chatInfo.CurrentStepId  -= 1;
                chatInfo.PreviousStepId -= 1;

                await CommandAskQuestion(prevCommand, chatInfo, null);

                if (null == GetNextCommand(chatInfo))
                {
                    await chatsRepository.Delete(chatInfo);
                }
                else
                {
                    await chatsRepository.Update(chatInfo);
                }

                return;
            }

            IDialogCommand command = _commands.FirstOrDefault(cmd => cmd.Label == chatInfo.CurrentStepId);

            if (!command.IsMessageCorrect(message))
            {
                SendWrongCommandMessage(chatId, message, chatInfo.CurrentStepId);
                return;
            }

            ITgOutboundMessage acknowledgement =
                await command.ApplyResultAsync(chatInfo, message, _cancellationTokenSource.Token);

            chatInfo.PreviousStepId = chatInfo.CurrentStepId;
            ++chatInfo.CurrentStepId;
            await chatsRepository.Update(chatInfo);

            var nextCommand = _commands.FirstOrDefault(cmd => cmd.Label == chatInfo.CurrentStepId);

            await CommandAskQuestion(nextCommand, chatInfo, acknowledgement);

            if (null == GetNextCommand(chatInfo))
            {
                await chatsRepository.Delete(chatInfo);
            }
        }