Updated Frequently requested design patterns (markdown)

Bibo-Joshi 2024-07-26 21:11:26 +02:00
parent c11cf467e2
commit 03347ff5d4

@ -106,7 +106,8 @@ async def callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
raise ApplicationHandlerStop
```
Here, it should be noted that this approach blocks your bot entirely for a set of users. If all you need is to block a specific functionality, like a special command or privilege, then it will be wise to use [filters.Chat](https://python-telegram-bot.readthedocs.io/telegram.ext.filters.html#telegram.ext.filters.Chat), [filters.User](https://python-telegram-bot.readthedocs.io/telegram.ext.filters.html#telegram.ext.filters.User).
> [!TIP]
> Here, it should be noted that this approach blocks your bot entirely for a set of users. If all you need is to block a specific functionality, like a special command or privilege, then it will be wise to use [filters.Chat](https://python-telegram-bot.readthedocs.io/telegram.ext.filters.html#telegram.ext.filters.Chat), [filters.User](https://python-telegram-bot.readthedocs.io/telegram.ext.filters.html#telegram.ext.filters.User).
Don't forget that you can also use [decorators](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#restrict-access-to-a-handler-decorator) or a simple `if-else` check.
If you want a more streamlined style of managing permissions (like superuser, admin, users) then [ptbcontrib/roles](https://github.com/python-telegram-bot/ptbcontrib/tree/main/ptbcontrib/roles) is worth checking out.
@ -145,6 +146,9 @@ async def callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
The approach we used is dead lazy. We keep a count of updates from the user and when it reaches maximum limit, we note the time. We proceed to stop handling the updates of that user for 5 minutes. Your effective flood limit strategy and punishment may vary. But the logic remains same.
> [!TIP]
> If you don't need to reply to the update directly and/or only want to delay the updates before being processed, you can also do that *before* any of the handlers are triggered. PTB allows customizing how the `Application` processes updates via the [`BaseUpdateProcessor`](https://docs.python-telegram-bot.org/telegram.ext.baseupdateprocessor.html) class. Please also have a look at [this wiki page](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Concurrency#applicationconcurrent_updates).
### Conclusion
We have seen how `TypeHandler` can be used to give a fluent experience without messing up our code-base. Now you would be able to solve complex use cases from the given examples. But please note that `TypeHandler` **is not** the only option.