René's URL Explorer Experiment


Title: Add decorators for routing definitions · Issue #899 · python-telegram-bot/python-telegram-bot · GitHub

Open Graph Title: Add decorators for routing definitions · Issue #899 · python-telegram-bot/python-telegram-bot

X Title: Add decorators for routing definitions · Issue #899 · python-telegram-bot/python-telegram-bot

Description: 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 it...

Open Graph Description: 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 ...

X Description: 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 optio...

Opengraph URL: https://github.com/python-telegram-bot/python-telegram-bot/issues/899

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add decorators for routing definitions","articleBody":"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:\r\n\r\nAdd 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:\r\n```\r\nfrom mybot.dispatcher import command_handler, message_handler, callback_handler\r\n\r\n\r\n@message_handler(Filters.group \u0026 Filters.reply)\r\ndef reply_in_group(bot, update):\r\n    pass\r\n\r\n\r\n@command_handler(\"start\")\r\n@command_handler(\"help\")\r\ndef help(bot, update):\r\n    # Multiple handlers for a callback\r\n    update.message.reply_text(\"Help message\")\r\n\r\n\r\n@callback_handler(pattern='my-cb')\r\ndef button_callback(bot, update):\r\n    pass\r\n```\r\n\r\nThis was the tl;dr part, for some deeper examination, read on.\r\n\r\n#### Current state\r\nNow 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..._\r\n\r\nWhat 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.\r\n\r\n\u003e In this philosophy, the routing happens outside of the handlers, in that they're decoupled from the place where they're used. I'm not sure if this was a concious design decision, or a construct that established over time... @jh0ker?\r\nHowever, @Eldinnie made me aware that we are already breaking this convention by defining flow actions inside callbacks with the return values of `ConversationHandlers`, which is another reason I'm not holding back anymore with this enhancement proposal.\r\n\r\nSo far, so good. This philosophy works great, especially for larger projects, so I wouldn't wanna drop or change it.\r\n\r\n#### With decorators\r\nThe 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.\r\n\r\nThis 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 corresponding `dispatcher.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.\r\n\r\nA 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).\r\n\r\nI 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.\r\n\r\nI'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](https://github.com/eternnoir/pyTelegramBotAPI#message-handlers) who use this method to their advantage, providing a lot of utility and readability.\r\n\r\nWe're also gonna have to think about border cases like the conversation handler, perhaps we can come up with a nice solution for it.\r\n\r\nThis 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...","author":{"url":"https://github.com/JosXa","@type":"Person","name":"JosXa"},"datePublished":"2017-11-02T02:21:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/899/python-telegram-bot/issues/899"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:0d37f337-ce82-70f3-4cc8-86e6e575fa83
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idC504:50E3E:59EF04:7A2DC2:6A612023
html-safe-nonce4535777f7505ef9cb5dd712149281bd3c189bf1f3f6f15890320fdbb585042a2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTA0OjUwRTNFOjU5RUYwNDo3QTJEQzI6NkE2MTIwMjMiLCJ2aXNpdG9yX2lkIjoiMzI0NjI2NjA5NTE5MzEwNDQxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac26e232b31f26ab0099e3051e75b6978891f95a09b578e7ec96d073157224f96b
hovercard-subject-tagissue:270511511
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-telegram-bot/python-telegram-bot/899/issue_layout
twitter:imagehttps://opengraph.githubassets.com/9785d28ffda2faf423500352ee33cc1595d4bbf8893e761281a3dfafcb014cdc/python-telegram-bot/python-telegram-bot/issues/899
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/9785d28ffda2faf423500352ee33cc1595d4bbf8893e761281a3dfafcb014cdc/python-telegram-bot/python-telegram-bot/issues/899
og:image:altSo 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 ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameJosXa
hostnamegithub.com
expected-hostnamegithub.com
None7d9987c85f924dc5597480f737e023a6e985b0316e672f00513eb4ba440d87ba
turbo-cache-controlno-preview
go-importgithub.com/python-telegram-bot/python-telegram-bot git https://github.com/python-telegram-bot/python-telegram-bot.git
octolytics-dimension-user_id16178365
octolytics-dimension-user_loginpython-telegram-bot
octolytics-dimension-repository_id38696925
octolytics-dimension-repository_nwopython-telegram-bot/python-telegram-bot
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id38696925
octolytics-dimension-repository_network_root_nwopython-telegram-bot/python-telegram-bot
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
released38dc1dac7b5a5d0788e068e080820de6de90a28
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python-telegram-bot/python-telegram-bot/issues/899#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-telegram-bot%2Fpython-telegram-bot%2Fissues%2F899
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-telegram-bot%2Fpython-telegram-bot%2Fissues%2F899
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python-telegram-bot%2Fpython-telegram-bot
Reloadhttps://github.com/python-telegram-bot/python-telegram-bot/issues/899
Reloadhttps://github.com/python-telegram-bot/python-telegram-bot/issues/899
Reloadhttps://github.com/python-telegram-bot/python-telegram-bot/issues/899
Please reload this pagehttps://github.com/python-telegram-bot/python-telegram-bot/issues/899
python-telegram-bot https://github.com/python-telegram-bot
python-telegram-bothttps://github.com/python-telegram-bot/python-telegram-bot
Notifications https://github.com/login?return_to=%2Fpython-telegram-bot%2Fpython-telegram-bot
Fork 6.1k https://github.com/login?return_to=%2Fpython-telegram-bot%2Fpython-telegram-bot
Star 29.3k https://github.com/login?return_to=%2Fpython-telegram-bot%2Fpython-telegram-bot
Code https://github.com/python-telegram-bot/python-telegram-bot
Issues 24 https://github.com/python-telegram-bot/python-telegram-bot/issues
Pull requests 8 https://github.com/python-telegram-bot/python-telegram-bot/pulls
Discussions https://github.com/python-telegram-bot/python-telegram-bot/discussions
Actions https://github.com/python-telegram-bot/python-telegram-bot/actions
Projects https://github.com/python-telegram-bot/python-telegram-bot/projects
Wiki https://github.com/python-telegram-bot/python-telegram-bot/wiki
Security and quality 0 https://github.com/python-telegram-bot/python-telegram-bot/security
Insights https://github.com/python-telegram-bot/python-telegram-bot/pulse
Code https://github.com/python-telegram-bot/python-telegram-bot
Issues https://github.com/python-telegram-bot/python-telegram-bot/issues
Pull requests https://github.com/python-telegram-bot/python-telegram-bot/pulls
Discussions https://github.com/python-telegram-bot/python-telegram-bot/discussions
Actions https://github.com/python-telegram-bot/python-telegram-bot/actions
Projects https://github.com/python-telegram-bot/python-telegram-bot/projects
Wiki https://github.com/python-telegram-bot/python-telegram-bot/wiki
Security and quality https://github.com/python-telegram-bot/python-telegram-bot/security
Insights https://github.com/python-telegram-bot/python-telegram-bot/pulse
💡 featurehttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=type:"💡 feature"
Add decorators for routing definitionshttps://github.com/python-telegram-bot/python-telegram-bot/issues/899#top
📋 help-wantedwork status: help-wantedhttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=state%3Aopen%20label%3A%22%F0%9F%93%8B%20help-wanted%22
https://github.com/JosXa
JosXahttps://github.com/JosXa
on Nov 2, 2017https://github.com/python-telegram-bot/python-telegram-bot/issues/899#issue-270511511
@jh0kerhttps://github.com/jh0ker
@Eldinniehttps://github.com/Eldinnie
@tsnoamhttps://github.com/tsnoam
pyTelegramBotAPIhttps://github.com/eternnoir/pyTelegramBotAPI#message-handlers
#859https://github.com/python-telegram-bot/python-telegram-bot/pull/859
📋 help-wantedwork status: help-wantedhttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=state%3Aopen%20label%3A%22%F0%9F%93%8B%20help-wanted%22
💡 featurehttps://github.com/python-telegram-bot/python-telegram-bot/issues?q=type:"💡 feature"
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.