Esempio n. 1
0
def pass_handler(message: Message):
    try:
        if message.forward_from:
            raise InvalidConditionError()
        if not methods.is_admin(message.from_user):
            raise UnauthorizedCommandError(message=message, service=methods, bot=bot, logger=logger)
        try:
            target_message = message.reply_to_message
        except AttributeError:
            raise InvalidConditionError()
        if target_message is None:
            raise InvalidConditionError()
        newbie_list = newbie_storage.get_user_list()
        if not newbie_list:
            raise InvalidConditionError()

        for newbie in newbie_storage:
            if newbie.greeting.message_id == target_message.message_id:
                methods.delete_chat_message(message)
                methods.delete_chat_message(newbie.greeting)
                bot.restrict_chat_member(
                    chat_id=target_message.chat.id,
                    user_id=newbie.user.id,
                    can_send_messages=True,
                )
                newbie_storage.remove(newbie.user)
                return
        raise InvalidConditionError()

    except ApiException:
        logger.error(f'Can not pass message')
    except InvalidConditionError:
        pass
Esempio n. 2
0
    def restore_restriction(self, restricted: RestrictedUserDto):
        try:
            try:
                actual_restricted = self._restriction_storage.get(restricted.user)
            except UserNotFoundInStorageError:
                raise InvalidConditionError()

            if actual_restricted.restore_at != restricted.restore_at:
                raise InvalidConditionError()

            self._bot.restrict_chat_member(
                chat_id=restricted.chat_id,
                user_id=restricted.user.id,
                until_date=max(
                    restricted.until_date,
                    time.time() + RestrictDuration.UNSAFE_DURATION_SECONDS
                ),
                can_send_messages=restricted.restriction.messages,
                can_send_media_messages=restricted.restriction.media,
                can_send_other_messages=restricted.restriction.other,
                can_add_web_page_previews=restricted.restriction.web_preview,
            )
            self._logger.info(
                f'Custom restriction was restored for @{restricted.user.username}. {restricted.restriction.__dict__}'
            )
        except ApiException:
            self._logger.error(f'Can not set custom restriction for chat member @{restricted.user.username}')
        except InvalidConditionError:
            pass
Esempio n. 3
0
def restrict_handler(message: Message):
    try:
        if message.forward_from:
            raise InvalidConditionError()
        if not methods.is_admin(message.from_user):
            raise UnauthorizedCommandError(message=message, service=methods, bot=bot, logger=logger)

        command = message.text[:3]
        task_list = {
            f'{Command.RO.bot_command}': methods.set_read_only,
            f'{Command.TO.bot_command}': methods.set_text_only,
        }

        try:
            target_message = message.reply_to_message
        except AttributeError:
            raise InvalidConditionError()
        if methods.is_admin(target_message.from_user):
            logger.warning(f'@{message.from_user.username} trying to restrict another admin. Abort.')
            raise InvalidConditionError()

        try:
            query = methods.prepare_query(message.text)
            restrict_duration = methods.get_duration(text=query, duration_class=RestrictDuration())
        except ParseBanDurationError:
            raise InvalidCommandError

        target_user = target_message.from_user
        try:
            logger.info(f'Try to restrict @{target_user.username} with {command} for {query}.')
            try:
                restrict_task = task_list.get(command)
                restriction_text = restrict_task(
                    user=target_user,
                    message=message,
                    duration=restrict_duration
                )
            except (KeyError, TypeError):
                raise InvalidCommandError()

            bot.send_message(
                chat_id=message.chat.id,
                text=f'*{restriction_text}*',
                reply_to_message_id=message.message_id,
                parse_mode=TelegramParseMode.MARKDOWN,
            )
        except ApiException:
            logger.error(f'Can not restrict chat member {target_user}')

    except InvalidCommandError:
        logger.warning(f'Can not execute command \'{message.text}\' from @{message.from_user.username}')
        methods.delete_chat_message(message)
    except InvalidConditionError:
        pass
Esempio n. 4
0
def ban_handler(message: Message):
    try:
        if message.forward_from:
            raise InvalidConditionError()
        if not methods.is_admin(message.from_user):
            raise UnauthorizedCommandError(message=message,
                                           service=methods,
                                           bot=bot,
                                           logger=logger)

        try:
            target_message = message.reply_to_message
            if methods.is_admin(target_message.from_user):
                logger.warning(
                    f'@{message.from_user.username} trying to ban another admin. Abort.'
                )
                raise InvalidCommandError()
        except AttributeError:
            raise InvalidConditionError()

        try:
            query = methods.prepare_query(message.text)
            ban_duration = methods.get_duration(text=query,
                                                duration_class=BanDuration())
        except ParseBanDurationError:
            raise InvalidCommandError

        target_user = target_message.from_user
        try:
            logger.info(f'Try to ban @{target_user.username} for {query}.')
            ban_text = methods.ban_kick(user=target_user,
                                        message=message,
                                        duration=ban_duration)
            bot.send_message(
                chat_id=message.chat.id,
                text=f'*{ban_text}*',
                reply_to_message_id=message.message_id,
                parse_mode=TelegramParseMode.MARKDOWN,
            )
        except ApiException:
            logger.error(f'Can not kick chat member @{target_user.username}')

    except InvalidCommandError:
        logger.warning(
            f'Can not execute command \'{message.text}\' from @{message.from_user.username}'
        )
        methods.delete_chat_message(message)
    except InvalidConditionError:
        pass
Esempio n. 5
0
def permit_handler(message: Message):
    try:
        if message.forward_from:
            raise InvalidConditionError()
        if not methods.is_admin(message.from_user):
            raise UnauthorizedCommandError(message=message,
                                           service=methods,
                                           bot=bot,
                                           logger=logger)

        try:
            target_message = message.reply_to_message
        except AttributeError:
            raise InvalidCommandError()

        target_user = target_message.from_user

        try:
            chat_member = bot.get_chat_member(message.chat.id, target_user.id)
            if chat_member.status != TelegramMemberStatus.RESTRICTED:
                raise InvalidConditionError()

            logger.info(f'Try to permit @{target_user.username}.')
            permission_text = methods.set_read_write(user=target_user,
                                                     message=message)

            bot.send_message(
                chat_id=message.chat.id,
                text=f'*{permission_text}*',
                reply_to_message_id=message.message_id,
                parse_mode=TelegramParseMode.MARKDOWN,
            )
        except ApiException:
            logger.error(f'Can not permit chat member {target_user}')

    except InvalidCommandError:
        logger.warning(
            f'Can not execute command \'{message.text}\' from @{message.from_user.username}'
        )
        methods.delete_chat_message(message)
    except InvalidConditionError:
        pass
Esempio n. 6
0
def greeting_callback(call: CallbackQuery):
    try:
        if not call.message:
            raise InvalidConditionError()
        if call.from_user.id not in newbie_storage.get_user_list():
            raise InvalidConditionError()

        newbie = newbie_storage.get(call.from_user)
        greeting_message = newbie.greeting
        if call.message.message_id != greeting_message.message_id:
            raise InvalidConditionError()

        methods.remove_inline_keyboard(call.message)
        try:
            reply = newbie.question.reply[call.data]
        except (KeyError, TypeError):
            reply = '*{first_name} ответил "{call_data}".*'
        bot.send_message(
            chat_id=call.message.chat.id,
            text=reply.format(first_name=call.from_user.first_name,
                              call_data=call.data),
            reply_to_message_id=call.message.message_id,
            parse_mode=TelegramParseMode.MARKDOWN,
        )

        newbie_storage.remove(newbie.user)
        try:
            bot.restrict_chat_member(chat_id=call.message.chat.id,
                                     user_id=call.from_user.id,
                                     can_send_messages=True,
                                     can_send_media_messages=True,
                                     can_send_other_messages=True,
                                     can_add_web_page_previews=True)
        except ApiException:
            logger.error(
                f'Can not disable restriction for chat member @{call.from_user.username}'
            )
    except InvalidConditionError:
        pass
Esempio n. 7
0
def test_handler(message: Message):
    response_list = {
        '/ping': 'pong',
        '/id': message.chat.id,
        '/ver': __version__,
    }

    try:
        admin_list = [x.user.id for x in bot.get_chat_administrators(message.chat.id)]
        if message.from_user.id not in admin_list:
            raise InvalidConditionError()

        response_message = bot.send_message(message.chat.id, response_list[message.text])
        for current_message in message, response_message:
            methods.create_scheduled_threat(
                pause=MessageSettings.SELF_DESTRUCT_TIMEOUT,
                action=methods.delete_chat_message,
                args=(current_message,)
            )
    except (ApiException, InvalidConditionError):
        methods.delete_chat_message(message)