public async Task ProcessCallback(CallbackQuery query) { var unauthorizedUser = _usersStore.Get(query.Message.Chat.Id, query.From.Id); if (unauthorizedUser == null) { _logger.LogInformation("User with id {UserId} not found", query.From.Id); return; } var unauthorizedUserAnswer = int.Parse(query.Data); if (unauthorizedUserAnswer != unauthorizedUser.CorrectAnswer) { await _telegramBot.KickChatMemberAsync( query.Message.Chat.Id, query.From.Id, DateTime.Now.AddDays(1)); _logger.LogInformation( "User {UserId} with name {UserName} was banned after incorrect answer {UserAnswer}, " + "while correct one is {CorrectAnswer}.", unauthorizedUser.Id, unauthorizedUser.PrettyUserName, unauthorizedUserAnswer, unauthorizedUser.CorrectAnswer); } else { await _telegramBot.RestrictChatMemberAsync( query.Message.Chat.Id, query.From.Id,
private async Task BanSlowUsers() { var users = _usersStore.GetAll(); var usersToBan = users.Where( x => { var diff = DateTimeOffset.Now - x.JoinDateTime; return(diff > TimeSpan.FromSeconds(60)); }) .ToArray(); foreach (var newUser in usersToBan) { await _telegramBot.KickChatMemberAsync(newUser.ChatId, newUser.Id, DateTime.Now.AddDays(1)); await _telegramBot.DeleteMessageAsync(newUser.ChatId, newUser.InviteMessageId); await _telegramBot.DeleteMessageAsync(newUser.ChatId, newUser.JoinMessageId); _usersStore.Remove(newUser); _logger.LogInformation( "User {UserId} with name {UserName} was banned after one minute silence.", newUser.Id, newUser.PrettyUserName); } }
public async void Handle(Message message) { Debug.Assert(message != null); if (message.From.Id == _botClient.BotId) { // Don't process messages sent by us. return; } if (message.Text != null) { _logger.Information($"{Utilities.UserToString(message.From)}: {message.Text}"); } if (_spammerDetector.IsSpam(message)) { // Don't ban admins. ChatMember[] admins = await _botClient.GetChatAdministratorsAsync(message.Chat.Id).ConfigureAwait(false); if (admins.Select(a => a.User.Id).Contains(message.From.Id)) { _logger.Information($"Ignoring spam from admin {message.From.Id}"); return; } await _botClient.DeleteMessageAsync(message.Chat.Id, message.MessageId).ConfigureAwait(false); await _botClient.KickChatMemberAsync(message.Chat.Id, message.From.Id).ConfigureAwait(false); await _botClient.SendTextMessageAsync(message.Chat.Id, $"Banned spammer {Utilities.UserToString(message.From)}.").ConfigureAwait(false); } }
public async Task ProcessCallback(Update update) { var query = update.CallbackQuery; var chatId = query.Message.Chat.Id; var userId = query.From.Id; var unauthorizedUser = _userService.Get(chatId, userId); if (unauthorizedUser == null) { _logger.LogInformation($"Пользователь с id {userId} не найден"); return; } var unauthorizedUserAnswer = int.Parse(query.Data); if (unauthorizedUserAnswer != (int)unauthorizedUser.Answer) { await _telegramBot.KickChatMemberAsync( chatId, query.From.Id, DateTime.Now.AddDays(1)); _logger.LogInformation( $"Пользователь {userId} с ником {unauthorizedUser.PrettyUserName} был забанен после ввода неправильного ответа {unauthorizedUserAnswer}, " + $"в то время как правильный это - {unauthorizedUser.Answer}."); } else { var defaultPermissions = (await _telegramBot.GetChatAsync(chatId)).Permissions; var postBanPermissions = new ChatPermissions { CanAddWebPagePreviews = defaultPermissions?.CanAddWebPagePreviews ?? true, CanChangeInfo = defaultPermissions?.CanChangeInfo ?? true, CanInviteUsers = defaultPermissions?.CanInviteUsers ?? true, CanPinMessages = defaultPermissions?.CanPinMessages ?? true, CanSendMediaMessages = defaultPermissions?.CanSendMediaMessages ?? true, CanSendMessages = defaultPermissions?.CanSendMessages ?? true, CanSendOtherMessages = defaultPermissions?.CanSendOtherMessages ?? true, CanSendPolls = defaultPermissions?.CanSendPolls ?? true }; await _telegramBot.RestrictChatMemberAsync( chatId, query.From.Id, postBanPermissions); _logger.LogInformation($"Пользователь {unauthorizedUser.UserId} с именем {unauthorizedUser.PrettyUserName} был авторизован с ответом {unauthorizedUser.Answer}."); } await _telegramBot.DeleteMessageAsync(unauthorizedUser.ChatId, unauthorizedUser.InviteMessageId); await _telegramBot.DeleteMessageAsync(unauthorizedUser.ChatId, unauthorizedUser.JoinMessageId); _userService.Remove(unauthorizedUser); }
private async Task BanSlowUsers() { var banTime = DateTime.Now + _settings.BanTime; foreach (var newUser in _userService .GetAll() .Where(x => DateTimeOffset.Now - x.JoinDateTime > _settings.ProcessEventTimeout)) { await _telegramBot.KickChatMemberAsync(newUser.ChatId, (int)newUser.UserId, banTime); await _telegramBot.DeleteMessageAsync(newUser.ChatId, newUser.InviteMessageId); await _telegramBot.DeleteMessageAsync(newUser.ChatId, newUser.JoinMessageId); _userService.Remove(newUser); _logger.LogInformation($"Пользователь {newUser.UserId} с именем {newUser.PrettyUserName} был забанен после {_settings.ProcessEventTimeout.ToString()} ожидания."); } }
private async Task <string> KickChatAsync(long chatId, int queryFromId, NewUser?newUser) { try { await _telegramBot.KickChatMemberAsync( chatId, queryFromId, DateTime.Now.AddDays(1)); _logger.LogInformation( "User {UserId} with name {UserName} was banned after incorrect answer {UserAnswer}, " + "while correct one is {CorrectAnswer}.", newUser.Id, newUser.PrettyUserName, newUser, newUser.CorrectAnswer); } catch (Exception ex) { return(ex.Message); }; return(null); }
private async Task BanSlowUsers() { var users = _usersStore.GetAll(); var usersToBan = users.Where( x => { var diff = DateTimeOffset.Now - x.JoinDateTime; return(diff > TimeSpan.FromSeconds(60)); }) .ToArray(); foreach (var newUser in usersToBan) { await _telegramBot.KickChatMemberAsync(newUser.ChatId, newUser.Id, DateTime.Now.AddDays(1)); await _telegramBot.DeleteMessageAsync(newUser.ChatId, newUser.InviteMessageId); _usersStore.Remove(newUser); } }
public async Task ProcessCallback(CallbackQuery query) { var user = _usersStore.Get(query.Message.Chat.Id, query.From.Id); if (user == null) { _logger.LogInformation("User {UserId} not found", query.From.Id); return; } var userAnswer = Int32.Parse(query.Data); if (userAnswer != user.CorrectAnswer) { await _telegramBot.KickChatMemberAsync( query.Message.Chat.Id, query.From.Id, DateTime.Now.AddDays(1)); } else { await _telegramBot.RestrictChatMemberAsync( query.Message.Chat.Id, query.From.Id,
public async Task ProcessCallback(CallbackQuery query) { var chatId = query.Message.Chat.Id; var unauthorizedUser = _usersStore.Get(chatId, query.From.Id); if (unauthorizedUser == null) { _logger.LogInformation("User with id {UserId} not found", query.From.Id); return; } var unauthorizedUserAnswer = int.Parse(query.Data); if (unauthorizedUserAnswer != unauthorizedUser.CorrectAnswer) { await _telegramBot.KickChatMemberAsync( chatId, query.From.Id, DateTime.Now.AddDays(1)); _logger.LogInformation( "User {UserId} with name {UserName} was banned after incorrect answer {UserAnswer}, " + "while correct one is {CorrectAnswer}.", unauthorizedUser.Id, unauthorizedUser.PrettyUserName, unauthorizedUserAnswer, unauthorizedUser.CorrectAnswer); } else { var preBanPermissions = unauthorizedUser.ChatMember; var defaultPermissions = (await _telegramBot.GetChatAsync(chatId)).Permissions; var postBanPermissions = new ChatPermissions { CanAddWebPagePreviews = preBanPermissions.CanAddWebPagePreviews ?? defaultPermissions?.CanAddWebPagePreviews ?? true, CanChangeInfo = preBanPermissions.CanChangeInfo ?? defaultPermissions?.CanChangeInfo ?? true, CanInviteUsers = preBanPermissions.CanInviteUsers ?? defaultPermissions?.CanInviteUsers ?? true, CanPinMessages = preBanPermissions.CanPinMessages ?? defaultPermissions?.CanPinMessages ?? true, CanSendMediaMessages = preBanPermissions.CanSendMediaMessages ?? defaultPermissions?.CanSendMediaMessages ?? true, CanSendMessages = preBanPermissions.CanSendMessages ?? defaultPermissions?.CanSendMessages ?? true, CanSendOtherMessages = preBanPermissions.CanSendOtherMessages ?? defaultPermissions?.CanSendOtherMessages ?? true, CanSendPolls = preBanPermissions.CanSendPolls ?? defaultPermissions?.CanSendPolls ?? true }; await _telegramBot.RestrictChatMemberAsync( chatId, query.From.Id, postBanPermissions); _logger.LogInformation( "User {UserId} with name {UserName} was authorized with answer {UserAnswer}. " + "With post ban permissions {PostBanPermissions}.", unauthorizedUser.Id, unauthorizedUser.PrettyUserName, unauthorizedUserAnswer, JsonSerializer.Serialize(postBanPermissions)); } await _telegramBot.DeleteMessageAsync(unauthorizedUser.ChatId, unauthorizedUser.InviteMessageId); await _telegramBot.DeleteMessageAsync(unauthorizedUser.ChatId, unauthorizedUser.JoinMessageId); _usersStore.Remove(unauthorizedUser); }