Skip to content

Filtering and discarding messages in a RegexHandler #432

@FutureSharks

Description

@FutureSharks

Hi,
I would like to only allow a few specific people to send commands to my bot. I was going to just filter messages and commands based on the chat_id. Is there a way I can have a RegexHandler filter out some commands or messages based on chat_id and discard them?

Something like this:

def filter_chat_id(bot, update):
        if update.message.chat_id not in allowed_chat_ids:
        # Stop here and skip all other handlers

updater = Updater(token)
dp = updater.dispatcher
dp.add_handler(RegexHandler('.*', filter_chat_id), group=1)
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("status", status))

Or is there a better way to achieve this? I know I could just add a conditional inside every single handler that compares the chat_id but I think this is a bit messy and hoping there is a cleaner way.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions