Skip to content

Add stop propagation logic #666

@s-chizhik

Description

@s-chizhik

Hi! I see that dispatcher can take handlers to different groups:

for group in self.groups:
    for handler in self.handlers[group]:
        try:
            if handler.check_update(update):
                handler.handle_update(update, self)
                break
        # Dispatch any errors
        except TelegramError as te:

Such realization doesn't allow us to cleanly stop handling from a group with a lower number. How do you think about some logic, which will make it? Ex, action handler can raise some exception, like StopPropagationExeption:

def custom_handler(bot: Bot, update: Update):
    # handling stuff...
    raise StopPropagationException # or maybe wrap it to bot.updater.stop_propagation()

dispatcher.add(custom_handler)
for group in self.groups:
    for handler in self.handlers[group]:
        try:
            if handler.check_update(update):
                handler.handle_update(update, self)
                break
        # Dispatch any errors
        except TelegramError as te:
    except StopPropagationExeption:
        pass

I've just noticed, that group mechanism allows us to create something like middleware, to handle such things as authorization or smth else. I suppose this logic would be useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions