public async Task AnswerInlineKeyboard(ITelegramBotClient client, User from, Message message, string answer, CancellationToken cancellationToken) { var tasks = new List <Task>(2); tasks.Add( client.EditMessageTextAsync(message.Chat.Id, message.MessageId, string.Format(L10n.strings.InlineQueryAnswerMessage, message.Text, answer), parseMode: ParseMode.Markdown, cancellationToken: cancellationToken) ); tasks.Add(_session.RemoveAsync(message.Chat.Id, cancellationToken)); if (answer != L10n.strings.YesKeyword) { await Task.WhenAll(tasks); return; } tasks.Add( client.SendTextMessageAsync(message.Chat.Id, L10n.strings.StoppedBotMessage, cancellationToken: cancellationToken) ); var user = await _userRepository.GetByIdentifierAsync(from.Id, cancellationToken); if (user.Identifier != from.Id) { var authorizedUser = user.AuthorizedUsers.Single(x => x.Identifier == from.Id); user.AuthorizedUsers.Remove(authorizedUser); _userCache.AddOrUpdate(user); tasks.Add(_userRepository.RemoveAuthorizedUserAsync(user.Identifier, authorizedUser, cancellationToken)); } else { _userCache.Remove(user.Identifier); tasks.Add(_userRepository.DeleteAsync(user.Identifier, cancellationToken)); tasks.Add(_packagesRepository.DeleteByUsernameAsync(user.Username, cancellationToken)); if (user.AuthorizedUsers?.Any() == true) { tasks.Add( client.SendTextMessageAsync(message.Chat.Id, L10n.strings.StoppedBotFollowUpMessage, cancellationToken: cancellationToken) ); } foreach (var authorizedUser in user.AuthorizedUsers) { tasks.Add( client.SendTextMessageAsync( authorizedUser.ChatIdentifier, string.Format(L10n.strings.StoppedBotAuthorizedUsersMessage, user.Username), cancellationToken: cancellationToken ) ); } } await Task.WhenAll(tasks); }
public async Task Login(ITelegramBotClient client, Message message, CancellationToken cancellationToken) { _session.Add(message, message.From.Id, SessionScope.Login); var credentials = message.Text.Split(',', 2, StringSplitOptions.RemoveEmptyEntries); if (credentials.Length < 2) { var sent = await client.SendTextMessageAsync( chatId : message.Chat, text : L10n.strings.SendUsernameAndPasswordSecondMessage, parseMode : ParseMode.Markdown, replyMarkup : new ForceReplyMarkup() { InputFieldPlaceholder = L10n.strings.UsernameAndPasswordPlaceholder }, cancellationToken : cancellationToken ); _session.Add(sent, message.From.Id, SessionScope.Login); return; } var validCredentials = await _flypack.TestCredentialsAsync(credentials[0], credentials[1]); if (validCredentials == false) { var sent = await client.SendTextMessageAsync( chatId : message.Chat, text : L10n.strings.WrongUsernameAndPasswordMessage, parseMode : ParseMode.Markdown, replyMarkup : new ForceReplyMarkup() { InputFieldPlaceholder = L10n.strings.UsernameAndPasswordPlaceholder }, cancellationToken : cancellationToken ); _session.Add(sent, message.From.Id, SessionScope.Login); return; } foreach (var msg in _session.Get(message.Chat.Id).Messages) { await client.DeleteMessageAsync(message.Chat, msg.Identifier, cancellationToken); } await _session.RemoveAsync(message.Chat.Id, cancellationToken); await client.SendChatActionAsync(message.Chat, ChatAction.Typing, cancellationToken); var loggedUser = await _userRepository.GetAsync(x => x.Username == credentials[0], cancellationToken); var authorizedUsers = loggedUser?.AuthorizedUsers ?? new SecondaryUser[] { }; if (loggedUser != null && authorizedUsers.Count(x => x.Identifier == message.From.Id) == 0) { await NotifyUserOfLoginAttempt(client, loggedUser, message.From, message.Chat.Id, cancellationToken); var sent = await client.SendTextMessageAsync( chatId : message.Chat, text : L10n.strings.AlreadyLoggedInWithThatAccountMessage, parseMode : ParseMode.Markdown, cancellationToken : cancellationToken ); _session.Add(sent, message.From.Id, SessionScope.Login); return; } var encrypted = _encrypter.Encrypt(credentials[1]); var user = new LoggedUser(message, credentials[0], encrypted.Password, encrypted.Salt); _userCache.AddOrUpdate(user); var task1 = _userRepository.AddAsync(user, cancellationToken); var task2 = client.SendTextMessageAsync( chatId: message.Chat, text: string.Format(L10n.strings.LoginWelcomeMessage, message.From.FirstName), parseMode: ParseMode.Markdown, cancellationToken: cancellationToken ); var task3 = _session.RemoveAsync(message.Chat.Id); await Task.WhenAll(task1, task2, task3); var packages = await _flypack.LoginAndFetchPackagesAsync(credentials[0], credentials[1]); await SendPackagesToChat(client, packages, message.From.Id, cancellationToken); }