eternnoir / pyTelegramBotAPI

Python Telegram bot api.
GNU General Public License v2.0
8.02k stars 2.02k forks source link

SkipHandler() not Working in middleware #2341

Closed ranjitsingha closed 2 months ago

ranjitsingha commented 2 months ago

Please answer these questions before submitting your issue. Thanks!

  1. What version of pyTelegramBotAPI are you using? Latest

  2. What OS are you using? Ubuntu

  3. What version of python are you using? 3.10

class AntiSpamMiddleware(BaseMiddleware):
    def __init__(self, limit, window) -> None:
        self.message_count = {}
        self.limit = limit
        self.window = window
        self.update_types = ['message']
    async def pre_process(self, message, data):
        current_time = time.time()
        user_id = message.from_user.id
        if user_id not in self.message_count:
            self.message_count[user_id] = {'count': 1, 'start_time': current_time}
            return
        elif current_time - self.message_count[user_id]['start_time'] < self.window:
            self.message_count[user_id]['count'] += 1
            if self.message_count[user_id]['count'] > self.limit:
                await bot.send_message(message.chat.id, 'You have been blocked temporarily for spamming')
                return SkipHandler()
        else:
            self.message_count[user_id] = {'count': 1, 'start_time': current_time}
    async def post_process(self, message, data, exception):
        pass        
bot.setup_middleware(AntiSpamMiddleware(15, 30))

return SkipHandler() isn't working. It reaches the handlers too

coder2020official commented 2 months ago

You sure you are importing skiphandler from asyncio_handler_backends?

ranjitsingha commented 2 months ago

You sure you are importing skiphandler from asyncio_handler_backends?

Sorry my mistake i didn't import. You can close this issue 😊