コード例 #1
0
        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;
            }


            if (int.TryParse(query.Data, out int result))
            {
                this._logger.LogInformation($"The number is not valid:{query.Data}");
                return;
            }
            var unauthorizedUserAnswer = result;

            if (unauthorizedUserAnswer != unauthorizedUser.CorrectAnswer)
            {
                string log = await KickChatAsync(chatId, query.From.Id, unauthorizedUser);

                if (log != null)
                {
                    this._logger.LogInformation($"KickChatAsync:{log}");
                }
            }
            else
            {
                ChatMember preBanPermissions = unauthorizedUser.ChatMember;

                ChatPermissions defaultPermissions = (await _telegramBot.GetChatAsync(chatId)).Permissions;

                var postBanPermissions = CreateChatPermissions(preBanPermissions, defaultPermissions);//

                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);
        }
コード例 #2
0
        public async Task ProcessNewChatMember(Update update)
        {
            var message   = update.Message;
            var freshness = DateTime.UtcNow - message.Date.ToUniversalTime();

            if (freshness > _settings.ProcessEventTimeout)
            {
                _logger.LogInformation($"Сообщение о {message.NewChatMembers.Length} получено {freshness} назад и проигнорировано");
                return;
            }

            foreach (var unauthorizedUser in message.NewChatMembers)
            {
                await _telegramBot.RestrictChatMemberAsync(
                    message.Chat.Id,
                    unauthorizedUser.Id,
                    new ChatPermissions
                {
                    CanAddWebPagePreviews = false,
                    CanChangeInfo         = false,
                    CanInviteUsers        = false,
                    CanPinMessages        = false,
                    CanSendMediaMessages  = false,
                    CanSendMessages       = false,
                    CanSendOtherMessages  = false,
                    CanSendPolls          = false
                },
                    DateTime.Now.AddDays(1));

                var answer         = new Random().Next(1, ButtonsCount + 1);
                var prettyUserName = GetPrettyName(unauthorizedUser);

                var sendTextMessage = await _telegramBot.SendTextMessageAsync(
                    message.Chat.Id,
                    $"Привет, {prettyUserName}, нажми кнопку {answer}, чтобы тебя не забанили!",
                    replyToMessageId : message.MessageId,
                    replyMarkup : new InlineKeyboardMarkup(GetKeyboardButtons()));

                var telegramUser = new TelegramUser(
                    message.Chat.Id,
                    message.From.Id,
                    sendTextMessage.MessageId,
                    message.MessageId,
                    GetPrettyName(message.From),
                    answer);

                _userService.Add(telegramUser);
                _logger.LogInformation(
                    $"Новый пользователь {unauthorizedUser.Id} с именем {prettyUserName} Обнаружен и испытан. " +
                    $"У него есть {_settings.ProcessEventTimeout.ToString()}, чтобы ответить.");
            }
        }
コード例 #3
0
        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,
コード例 #4
0
        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,
コード例 #5
0
        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);
        }
コード例 #6
0
ファイル: BotClient.cs プロジェクト: Liminiens/vahter-bot
 private Task Restrict(int userId)
 {
     return(_client.RestrictChatMemberAsync(_configuration.ChatId, userId, DateTime.MaxValue, false, false, false, false));
 }