Title: [FEATURE] ConversationHandler: Allow special handlers/callbacks for entering states · Issue #2390 · python-telegram-bot/python-telegram-bot · GitHub
Open Graph Title: [FEATURE] ConversationHandler: Allow special handlers/callbacks for entering states · Issue #2390 · python-telegram-bot/python-telegram-bot
X Title: [FEATURE] ConversationHandler: Allow special handlers/callbacks for entering states · Issue #2390 · python-telegram-bot/python-telegram-bot
Description: Originally proposed in #2388, rewriting here to summarize. Motivation ConversationHandler currently reacts to updates that happen while in a state. This means that a callback needs to "prepare" everything that the next state expects to h...
Open Graph Description: Originally proposed in #2388, rewriting here to summarize. Motivation ConversationHandler currently reacts to updates that happen while in a state. This means that a callback needs to "prepare" eve...
X Description: Originally proposed in #2388, rewriting here to summarize. Motivation ConversationHandler currently reacts to updates that happen while in a state. This means that a callback needs to "prepare...
Opengraph URL: https://github.com/python-telegram-bot/python-telegram-bot/issues/2390
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[FEATURE] ConversationHandler: Allow special handlers/callbacks for entering states","articleBody":"Originally proposed in #2388, rewriting here to summarize.\r\n\r\n## Motivation\r\n\r\n`ConversationHandler` currently reacts to updates that happen __while__ in a state. This means that a callback needs to \"prepare\" everything that the next state expects to have happend (e.g. send a message the user needs to react to). This can sometimes make it a bit hard to follow the logic of \"what needs to be done where\" and can also lead to code duplication in the cases where\r\n\r\n* multiple states lead to the same next state, thus they all need to do the same preparations\r\n* nested conversations are used that lead back to a parents state thats also reached directly by parent states. Here the child conversation needs to do the same preparations as the parent conversation.\r\n\r\nIn the setting of nested conversations this also leads to the parent conversation and the child conversation being dependent on each other, making child conversations less reusable for different parents.\r\n\r\n## Proposed feature\r\n\r\nAllow to run code when \"entering\" a state. This could be either a simple `callback(update, context)` or (which I would prefer) a list of handlers, just like currently for `states`. The logic would then be something along the lines:\r\n\r\n```python\r\ndef handle_update(self, update):\r\n ...\r\n new_state = selected_handler(update, context)\r\n if for handler in self.state_entries.get(new_state, []):\r\n if handler.check_update(update):\r\n handler.handle_update(update, context)\r\n break\r\n```\r\nThe entry handlers could be specified via a `entries` keyword (or whatever naming) that accepts a dictionary just like `states` does. \r\n\r\nOf course it should be purely optional, because a) backwards compatibility and b) simple conversations will still do fine without it.\r\n\r\n## Integration with `run_async`\r\n\r\nWhen a state-handler runs asynchronously, we only get a promise and hence don't know the next state. what we could do is add a method `add_done_callback` to `ext.utils.Promise` that makes sure the corresponding callback is run when `Promise.run` is done.\r\nThis is exactly what's provided by [`asyncio.Task/Future`](https://docs.python.org/3/library/asyncio-task.html#asyncio.Task.add_done_callback), so this has a high chance of not being an issue in the context of #2288.\r\n\r\n## Current alternatives\r\n\r\nWhat one can currently do to reduce code duplication is ofc to extract the duplicated code to a standalone function and call that wherever needed. In the setting of child conversations that should be reusable by different parents, one can use a factory pattern, i.e. have a method\r\n\r\n```python\r\ndef build_child_conversation(extracted_function_1, extracted_function_2, …) -> ConversationHandler:\r\n …\r\n```\r\nthat inserts the passed functions into the logic of the child conversation. This allows for the highest level of encapsulation. If that's not needed, one can also just directly use the corresponding functions of the parent conversation directly.\r\n\r\nWhile those soltions a viable, they require some manual work and still lead to some extend of code duplication.\r\n\r\n## Summary & Things to consider\r\n\r\nOne can surely argue that `ConversationHandler` is already complex enough and that the added benefit is rather small and mostly interesting for very advanced users who like to design their code as clean as possible. On the other hand I can imagine that this can help some users follow the logic of `ConversationHandler` more easily and promoting clean code structure is nothing to be ashamed of.\r\n\r\nBasically I think this is a reasonable feature to have, but would not be opposed to disregarding it in favor of maintainability.\r\n\r\nRagarding the integration with `run_async`, it may be worth to just wait for v14 to be out, before tackling this (if we decide to do so).","author":{"url":"https://github.com/Bibo-Joshi","@type":"Person","name":"Bibo-Joshi"},"datePublished":"2021-02-16T17:17:57Z","url":"https://github.com/python-telegram-bot/python-telegram-bot/issues/2390"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:59418f18-efb9-1922-63a0-7a43896d57e5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E914:1F92:33007C:405790:6B027DA0 |
| html-safe-nonce | 23454624d9c87697b4b9bcbaee9882b39ce5ddc3f449f95189e0d64ba827ef88 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTE0OjFGOTI6MzMwMDdDOjQwNTc5MDo2QjAyN0RBMCIsInZpc2l0b3JfaWQiOiI0NDAxMzA1ODg2MjcwODQ4NDE2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e32f21e143708f189348d5c56040d52d5bb8407acf77ad046b727726c0bb5b7e |
| hovercard-subject-tag | issue:809501263 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-telegram-bot/python-telegram-bot/2390/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3384ef368b287312e653f007dc5ffb7001bc418cedc40f2a1ab30b4619b8d22e/python-telegram-bot/python-telegram-bot/issues/2390 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3384ef368b287312e653f007dc5ffb7001bc418cedc40f2a1ab30b4619b8d22e/python-telegram-bot/python-telegram-bot/issues/2390 |
| og:image:alt | Originally proposed in #2388, rewriting here to summarize. Motivation ConversationHandler currently reacts to updates that happen while in a state. This means that a callback needs to "prepare" eve... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Bibo-Joshi |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-telegram-bot/python-telegram-bot git https://github.com/python-telegram-bot/python-telegram-bot.git |
| octolytics-dimension-user_id | 16178365 |
| octolytics-dimension-user_login | python-telegram-bot |
| octolytics-dimension-repository_id | 38696925 |
| octolytics-dimension-repository_nwo | python-telegram-bot/python-telegram-bot |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38696925 |
| octolytics-dimension-repository_network_root_nwo | python-telegram-bot/python-telegram-bot |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width