Esempio n. 1
0
        public async Task ProcessUpdate(Update update, Bot.FSM <SearchBot> fsm)
        {
            if (!await fsm.TryStartSubFSM(update) && !(asyncTaskExecutor == null))
            {
                if (update.Message.Text == "/start")
                {
                    messagesSender.AddItem(new TextMessage(null, update.Message.Chat.Id, "Приветствую! Для первого поиска просто отправьте мне слово/словосочетание.\n\nДля настройки параметров поиска нажмите /settings", null, new ReplyKeyboardRemove()));
                    return;
                }
                fsm.config.BotState     = PrivateChatState.Busy;
                CancellationTokenSource = new CancellationTokenSource();
                SearchReciever <SearchBot> searchClient = (SearchReciever <SearchBot>)serviceProvider.GetService(typeof(SearchReciever <SearchBot>));
                Task SearchingTask    = searchClient.Search(update.Message.From.Id, GetRequest(update, fsm.config), CancellationTokenSource.Token);
                Task searchFinalsTask = SearchingTask.ContinueWith((_) =>
                {
                    fsm.config.BotState = PrivateChatState.Ready;
                    messagesSender.AddItem(new TextMessage(null, update.Message.Chat.Id, "Завершено!", null, new ReplyKeyboardRemove()));
                    return(Task.CompletedTask);
                });

                //Task.WhenAll(SearchingTask, searchFinalsTask).Wait();
                asyncTaskExecutor.Add(Task.WhenAll(SearchingTask, searchFinalsTask));
                messagesSender.AddItem(Bot.Common.CreateOk(update, Bot.Constants.Keyboards.searchingKeyboard));
            }
        }
Esempio n. 2
0
 public async Task ProcessUpdate <TBot>(Update update, Bot.FSM <TBot> fsm) where TBot : IConfig, new()
 {
     if (Constants.Cancells.Contains(update.Message.Text.ToLower()))
     {
         ISendedItem reply = fsm.readyProcessor.Stop(update);
         messagesSender.AddItem(reply);
         fsm.config.BotState = PrivateChatState.Ready;
     }
     else
     {
         messagesSender.AddItem(Bot.Common.CreateImBusy(update));
     }
 }
Esempio n. 3
0
 public async Task <bool> Check <TBot>(Update update, Bot.FSM <TBot> fsm) where TBot : IConfig, new()
 {
     if (update.Type == Telegram.Bot.Types.Enums.UpdateType.Message && update.Message.Chat.Type == Telegram.Bot.Types.Enums.ChatType.Private)
     {
         if (fsm.config.Status > botSettings.BoundUserStatus)
         {
             messagesSender.AddItem(new TextMessage(null, update.Message.Chat.Id, "Пожалуйста, обратитесь к администратору для выдачи разрешения на продолжение работы.", null));
             return(false);
         }
         else
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 4
0
        public async Task HandleUpdate(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
        {
            if (update.Type == UpdateType.Message || update.Type == UpdateType.ChatMember)
            {
                await writer.PutData(update);

                Bot.FSM <TBot> processor = await Bot.FSM <TBot> .Factory.Get(update, cancellationToken);

                await processor.Process(update, cancellationToken);
            }
            else if (update.Type == UpdateType.CallbackQuery)
            {
                if (ObjectId.TryParse(update.CallbackQuery.Data, out ObjectId guid))
                {
                    if (update.CallbackQuery.Message.Chat.Id == update.CallbackQuery.From.Id)
                    {
                        Task sending = searchState.TryEdit(update.CallbackQuery.Message.Chat.Id, update.CallbackQuery.Message.MessageId, guid, cancellationToken);
                        asyncTaskExecutor.Add(sending);
                    }
                    else if ((await TextMessage.defaultClient.GetChatAdministratorsAsync(update.CallbackQuery.Message.Chat.Id)).ToList().FindIndex(item => item.User.Id == update.CallbackQuery.From.Id) >= 0)
                    {
                        Task sending = searchState.SendPage(update.CallbackQuery.From.Id, guid, cancellationToken);
                        asyncTaskExecutor.Add(sending);
                    }
                }
                else if (update.CallbackQuery.Data.Split('_').Length == 3)
                {
                    var spl = update.CallbackQuery.Data.Split('_');
                    if (long.TryParse(spl[0], out long chatId) && long.TryParse(spl[1], out long userId) && Enum.TryParse <Command>(spl[2], out var command))
                    {
                        var b = new TBot()
                        {
                            Id       = userId,
                            userType = UserType.User,
                        };

                        if ((await TextMessage.defaultClient.GetChatAdministratorsAsync(chatId)).ToList().FindIndex(item => item.User.Id == update.CallbackQuery.From.Id) >= 0)
                        {
                            switch (command)
                            {
                            case Command.Ban:
                            {
                                b.Status = UserStatus.banned;
                                asyncTaskExecutor.Add(TextMessage.defaultClient.KickChatMemberAsync(chatId, userId, revokeMessages: true));

                                break;
                            }

                            case Command.Trust:
                            {
                                b.Status = UserStatus.common;
                                break;
                            }
                            }
                            asyncTaskExecutor.Add(Bot.FSM <TBot> .Factory.dBWorker.SaveChat(b, cancellationToken, TextMessage.defaultClient.BotId.Value));
                        }
                    }
                }
                else
                {
                    Task sending = searchState.TryEdit(update, cancellationToken);
                    asyncTaskExecutor.Add(sending);
                }
            }
        }