-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add decorators for routing definitions #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Regarding: @message_handler(Filters.group & Filters.reply) I feel like accepting @message_handler(Filters.group, Filters.reply)
# or
@message_handler((Filters.group, Filters.reply)) Using Being able to use this syntax: @command_handler("help")
def help(bot, update):
... Seems just perfect and convenient. It has a Flask-like aesthethic: @app.route('/hello')
def hello():
return 'Hello world!' And it could even infer the name of the command to use just based on the method name if left empty. Or even use the returning value as the reply! I vote yes for this proposal. |
Right, I just made that up. It should be the same signature as the normal handler at first. |
@Lonami Using Edit: BTW we are using |
Good point I didn't notice about the feature of it having to be this and something else. I was thinking about more types of updates. I also didn't know you couldn't override |
I think this issue can be solved without retrocompatibility issues using non obtrusive decorators, i.e. that only annotate the functions (or methods). This way, after annotated, it's only a matter of run a processor to make the real calls to the dispatcher. |
So you have a "global" collection of routing definitions that are added when the bot starts, sounds reasonable. |
@JosXa it's no global if we have something like that: from telegram import BotDecorator
from telegram.ext import Updater
deco = BotDecorator()
@deco.command("help")
def help(bot, update):
...
@deco.command("start")
def start(bot, update):
...
@deco.error()
def on_error(bot, update, exc):
...
updater = Updater("token")
updater.dispatcher.add_handlers(deco) About the order, it's the order of the decorated. |
If we go for this, we might aswell look into doing something similar to flask blueprints. |
What about something like |
We discussed this issue in the dev chat and realized that most of us are not comfortable with it (anymore), including @JosXa as OP. Therefore, I'll close this issue. |
So I've had this idea brewing in my head and was also approached by someone about it, therefore it's that time again where you need to file an Issue. The idea is the following:
Add an optional way of decorating a callback handler with its routing definitions. All our
CommandHandlers
,MessageHandlers
etc. would get their corresponding decorators that can be used on top of callbacks, i.e. like this:This was the tl;dr part, for some deeper examination, read on.
Current state
Now traditionally, we've had all our handlers+callback setup in a routing block which is, for most users anyway, located near the instantiation of updater and dispatcher. Some might have gone so far as to put all the
dispatcher.add_handler
calls inside its own file (e.g.routing.py
), providing a convenient top-down/outside-in access to all the handlers. The workflow with this is something like need to change the name of a command or add some synonyms? No problem, know where to look, it's all right there...What I usually do is to have logically related components that are actually reusable, but this only makes sense on larger scale and is not efficient for smaller bots.
So far, so good. This philosophy works great, especially for larger projects, so I wouldn't wanna drop or change it.
With decorators
The second option allows for a setup where you can tell from a single glance at a callback signature how it is used and which bot functionalities are affected by this particular handler, increasing readability for you and discoverability for others reading your code, as well as code locality.
This also fixes the reusability of individual handlers in small bot environments. Say you have a command
/developer
(dev info) that you want to reuse in a couple of projects, then it will be cumbersome and inefficient to copy-paste the handler, and also search for its correspondingdispatcher.add_handler
line. With a decorator, everything is in the same spot: Easy to give advice in the Telegram group or update our snippets with routing examples that don't feel out of place.A point to consider is that is that by coupling the routing logic with a handler, you'd actually be decoupling the dispatcher object away from a "single point of use", forcing its distribution across all of your handler modules, making all these modules dependent on a dispatcher object. In the components modularization I described above, it makes sense to have a method
register(dispatcher)
within the module that takes a dispatcher as argument and preserves the traditional direction of dependencies. With routing, you need to actually import the dispatcher (which might, or might not be a good idea, again depending on project size).I would like to offer both options to our users. I don't see a reason why not to, but please come forward if you see problems with this method other that the ones I mentioned, and let me know what you think about it overall.
I'm aware that @tsnoam, one of the core devs who will genuinely hate me for bringing up this idea, is categorically against decorators, but I know a lot of people that like and see value in them. It is common practice in many other bot and web frameworks, particularly the folks at pyTelegramBotAPI who use this method to their advantage, providing a lot of utility and readability.
We're also gonna have to think about border cases like the conversation handler, perhaps we can come up with a nice solution for it.
This would also be a good opportunity to rethink #859, because all the
pass_*
denotations are going to be cumbersome, make the line very long, and add no value in terms of readability since the associated part where they're actually required is just one line below in the method signature (and not miles away in the routing block). Perhaps a hybrid approach would be suitable, where only the "already inherently magical" decorators are capable of autowiring, but not the traditional handler classes...The text was updated successfully, but these errors were encountered: