Ejemplo n.º 1
0
    async def verify_wait(client, chat, user, message=None):
        try:
            await client.restrict_chat_member(
                chat_id=chat.id,
                user_id=user.id,
                permissions=types.ChatPermissions(can_send_messages=False))

            msg = chat.get_setting('waiting_for_verification')

            if msg:
                msg = msg.format(mention=user.mention)
            else:
                msg = chat.get_message('waiting_for_verification',
                                       mention=user.mention)

            button = chat.get_setting('verify_now') or chat.get_message(
                'verify_now')
            destination = getattr(
                TokenDestination,
                chat.get_setting('verification_type', TokenDestination.GROUP))
            token = VerificationToken.generate(user,
                                               chat,
                                               destination=destination)
            button_data = {'text': button}

            if destination == TokenDestination.GROUP:
                button_data['callback_data'] = f'verify_{token.id}'
            elif destination == TokenDestination.PRIVATE:
                button_data['url'] = formatting.deeplink(f'verify_{token.id}')

            msg = await client.send_message(
                chat.id,
                text=msg,
                reply_markup=InlineKeyboardMarkup(
                    [[InlineKeyboardButton(**button_data)]]),
                reply_to_message_id=message.message_id if message else None)

            token.set_message_id(msg.message_id)
            token.set_reply_to_message_id(message.message_id)

            if token.timeout:
                await token.timeout.start(client)
        except Exception as e:
            raise e
Ejemplo n.º 2
0
    async def verify_user(client,
                          chat,
                          user,
                          message=None,
                          restrict=True,
                          captcha=None,
                          private: bool = False,
                          token: 'VerificationToken' = None) -> bool:
        try:
            if restrict:
                await client.restrict_chat_member(
                    chat_id=chat.id,
                    user_id=user.id,
                    permissions=types.ChatPermissions(can_send_messages=False))

            if not captcha:
                captcha = Captcha.generate(user, chat)

            if token:
                captcha.set_token(token)

            chat_id = user.id if private or not chat else chat.id

            msg = await captcha.send_message(
                client,
                chat_id=chat_id,
                reply_to_message_id=message.message_id if message else None)
            captcha.set_message(msg)

            if captcha.timeout:
                await captcha.timeout.start(client)

            return True
        except Exception as e:
            print(e, flush=True)
            return False
Ejemplo n.º 3
0
    async def update_message(self, message: types.Message,
                             client: pyrogram.Client) -> types.Message:
        with db_session:
            captcha = self.current
            user = captcha.user
            chat = captcha.chat
            chat_id = None

            status = captcha.get_status()
            reply_to_message_id = message.reply_to_message.message_id if message.reply_to_message else None
            keyboard = None
            should_unban = False

            if status == CaptchaStatus.WAITING:
                message = await message.edit_reply_markup(
                    captcha.create_keyboard())

                return message

            try:
                await message.delete(revoke=True)
            except RPCError:
                pass

            if status == CaptchaStatus.COMPLETED:
                seconds = captcha.completion_seconds
                kwargs = dict(time=timeutils.string(seconds, chat or user, 1),
                              mention=user.mention)

                if chat:
                    group_text = chat.get_setting('captcha_completed')

                    if group_text:
                        group_text = group_text.format(**kwargs)
                    else:
                        group_text = chat.get_message('captcha_completed',
                                                      **kwargs)
                else:
                    group_text = user.get_message('captcha_completed',
                                                  **kwargs)

                if self.token and self.token.destination == TokenDestination.PRIVATE:
                    private_text = user.get_message(
                        'captcha_completed_private',
                        mention=chat.mention,
                        time=timeutils.string(seconds, user, 1))

                    msg = await message.reply_text(
                        private_text,
                        reply_to_message_id=reply_to_message_id,
                        disable_web_page_preview=True)
                    chat_id = self.token.chat.id
                    reply_to_message_id = None

                if captcha.chat:
                    captcha.chat.verified_users.add(captcha.user)

                    if captcha.chat.has_permissions:
                        try:
                            await client.restrict_chat_member(
                                chat_id=captcha.chat.id,
                                user_id=captcha.user.id,
                                permissions=types.ChatPermissions(
                                    can_send_messages=True))
                        except RPCError:
                            pass
            elif status == CaptchaStatus.LOST:
                delay = captcha.chat.get_setting(
                    'captcha_delay') if chat else None

                if delay is None:
                    delay = self.DEFAULT_DELAY

                delay = int(delay)

                localizator = captcha.chat or captcha.user

                if delay > 30:
                    time_str = localizator.get_message(
                        'in') + ' ' + timeutils.string(delay, localizator, 100)
                else:
                    time_str = localizator.get_message('when_they_rejoin')

                kwargs = dict(time=time_str, mention=captcha.user.mention)

                if captcha.chat:
                    group_text = captcha.chat.get_setting('captcha_incorrect')

                    if group_text:
                        group_text = group_text.format(**kwargs)
                    else:
                        group_text = captcha.chat.get_message(
                            'captcha_incorrect', **kwargs)
                else:
                    group_text = captcha.user.get_message(
                        'captcha_incorrect', **kwargs)

                if self.token and self.token.destination == TokenDestination.PRIVATE:
                    if delay > 30:
                        time_str = user.get_message(
                            'in') + ' ' + timeutils.string(delay, user, 100)
                    else:
                        time_str = user.get_message('when_you_rejoin')

                    private_text = user.get_message(
                        'captcha_incorrect_private',
                        mention=chat.mention,
                        time=time_str)

                    msg = await message.reply_text(
                        private_text,
                        reply_to_message_id=reply_to_message_id,
                        disable_web_page_preview=True)
                    chat_id = self.token.chat.id
                    reply_to_message_id = None

                if captcha.chat and captcha.chat.has_permissions:
                    keyboard = unban.unban_keyboard(captcha.chat, captcha.user)

                    msg = await client.kick_chat_member(
                        chat_id=captcha.chat.id,
                        user_id=captcha.user.id,
                        until_date=delay if delay >= 30 else 30)

                    if isinstance(msg, types.Message):
                        try:
                            await msg.delete()
                        except RPCError:
                            pass

                    if delay < 30:
                        should_unban = True
                        keyboard = None

            if chat_id:
                await client.delete_messages(chat_id=self.token.chat.id,
                                             message_ids=self.token.message_id)

                await client.send_message(
                    chat_id,
                    group_text,
                    reply_to_message_id=self.token.reply_to_message_id,
                    disable_web_page_preview=True,
                    reply_markup=keyboard)
            else:
                msg = await message.reply_text(
                    group_text,
                    reply_to_message_id=reply_to_message_id,
                    disable_web_page_preview=True,
                    reply_markup=keyboard)

            if should_unban:
                await client.unban_chat_member(chat_id=captcha.chat.id,
                                               user_id=captcha.user.id)

            return msg