GeezerVison is showing github.com — Overhaul of Filters (#2759) · MSutt/python-telegram-bot@145d042
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/source/conf.py +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | # If extensions (or modules to document with autodoc) are in another directory, | 20 | 20 | # add these directories to sys.path here. If the directory is relative to the | 21 | 21 | # documentation root, use os.path.abspath to make it absolute, like shown here. | | 22 | +import inspect | 22 | 23 | from docutils.nodes import Element | 23 | 24 | from sphinx.application import Sphinx | 24 | 25 | from sphinx.domains.python import PyXRefRole | @@ -358,7 +359,23 @@ def process_link(self, env: BuildEnvironment, refnode: Element, | 358 | 359 |
| 359 | 360 |
| 360 | 361 | def autodoc_skip_member(app, what, name, obj, skip, options): | 361 | | - pass | | 362 | + """We use this to not document certain members like filter() or check_update() for filters. | | 363 | + See https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#skipping-members""" | | 364 | + | | 365 | + included = {'MessageFilter', 'UpdateFilter'} # filter() and check_update() only for these. | | 366 | + included_in_obj = any(inc in repr(obj) for inc in included) | | 367 | + | | 368 | + if included_in_obj: # it's difficult to see if check_update is from an inherited-member or not | | 369 | + for frame in inspect.stack(): # From https://github.com/sphinx-doc/sphinx/issues/9533 | | 370 | + if frame.function == "filter_members": | | 371 | + docobj = frame.frame.f_locals["self"].object | | 372 | + if not any(inc in str(docobj) for inc in included) and name == 'check_update': | | 373 | + return True | | 374 | + break | | 375 | + | | 376 | + if name == 'filter' and obj.__module__ == 'telegram.ext.filters': | | 377 | + if not included_in_obj: | | 378 | + return True # return True to exclude from docs. | 362 | 379 |
| 363 | 380 |
| 364 | 381 | def setup(app: Sphinx): | |
Collapse file Copy file name to clipboardExpand all lines: docs/source/telegram.ext.filters.rst +5Lines changed: 5 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | telegram.ext.filters Module | 4 | 4 | =========================== | 5 | 5 |
| | 6 | +.. :bysource: since e.g filters.CHAT is much above filters.Chat() in the docs when it shouldn't. | | 7 | + The classes in `filters.py` are sorted alphabetically such that :bysource: still is readable | | 8 | +
| 6 | 9 | .. automodule:: telegram.ext.filters | | 10 | + :inherited-members: | 7 | 11 | :members: | 8 | 12 | :show-inheritance: | | 13 | + :member-order: bysource |
Collapse file Copy file name to clipboardExpand all lines: examples/conversationbot.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | from telegram.ext import ( | 21 | 21 | CommandHandler, | 22 | 22 | MessageHandler, | 23 | | - Filters, | | 23 | + filters, | 24 | 24 | ConversationHandler, | 25 | 25 | Updater, | 26 | 26 | CallbackContext, | @@ -146,13 +146,13 @@ def main() -> None: | 146 | 146 | conv_handler = ConversationHandler( | 147 | 147 | entry_points=[CommandHandler('start', start)], | 148 | 148 | states={ | 149 | | - GENDER: [MessageHandler(Filters.regex('^(Boy|Girl|Other)$'), gender)], | 150 | | - PHOTO: [MessageHandler(Filters.photo, photo), CommandHandler('skip', skip_photo)], | | 149 | + GENDER: [MessageHandler(filters.Regex('^(Boy|Girl|Other)$'), gender)], | | 150 | + PHOTO: [MessageHandler(filters.PHOTO, photo), CommandHandler('skip', skip_photo)], | 151 | 151 | LOCATION: [ | 152 | | - MessageHandler(Filters.location, location), | | 152 | + MessageHandler(filters.LOCATION, location), | 153 | 153 | CommandHandler('skip', skip_location), | 154 | 154 | ], | 155 | | - BIO: [MessageHandler(Filters.text & ~Filters.command, bio)], | | 155 | + BIO: [MessageHandler(filters.TEXT & ~filters.COMMAND, bio)], | 156 | 156 | }, | 157 | 157 | fallbacks=[CommandHandler('cancel', cancel)], | 158 | 158 | ) | |
Collapse file Copy file name to clipboardExpand all lines: examples/conversationbot2.py +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | from telegram.ext import ( | 22 | 22 | CommandHandler, | 23 | 23 | MessageHandler, | 24 | | - Filters, | | 24 | + filters, | 25 | 25 | ConversationHandler, | 26 | 26 | Updater, | 27 | 27 | CallbackContext, | @@ -126,23 +126,23 @@ def main() -> None: | 126 | 126 | states={ | 127 | 127 | CHOOSING: [ | 128 | 128 | MessageHandler( | 129 | | - Filters.regex('^(Age|Favourite colour|Number of siblings)$'), regular_choice | | 129 | + filters.Regex('^(Age|Favourite colour|Number of siblings)$'), regular_choice | 130 | 130 | ), | 131 | | - MessageHandler(Filters.regex('^Something else...$'), custom_choice), | | 131 | + MessageHandler(filters.Regex('^Something else...$'), custom_choice), | 132 | 132 | ], | 133 | 133 | TYPING_CHOICE: [ | 134 | 134 | MessageHandler( | 135 | | - Filters.text & ~(Filters.command | Filters.regex('^Done$')), regular_choice | | 135 | + filters.TEXT & ~(filters.COMMAND | filters.Regex('^Done$')), regular_choice | 136 | 136 | ) | 137 | 137 | ], | 138 | 138 | TYPING_REPLY: [ | 139 | 139 | MessageHandler( | 140 | | - Filters.text & ~(Filters.command | Filters.regex('^Done$')), | | 140 | + filters.TEXT & ~(filters.COMMAND | filters.Regex('^Done$')), | 141 | 141 | received_information, | 142 | 142 | ) | 143 | 143 | ], | 144 | 144 | }, | 145 | | - fallbacks=[MessageHandler(Filters.regex('^Done$'), done)], | | 145 | + fallbacks=[MessageHandler(filters.Regex('^Done$'), done)], | 146 | 146 | ) | 147 | 147 |
| 148 | 148 | dispatcher.add_handler(conv_handler) | |
Collapse file Copy file name to clipboardExpand all lines: examples/deeplinking.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | from telegram.ext import ( | 26 | 26 | CommandHandler, | 27 | 27 | CallbackQueryHandler, | 28 | | - Filters, | | 28 | + filters, | 29 | 29 | Updater, | 30 | 30 | CallbackContext, | 31 | 31 | ) | @@ -115,20 +115,20 @@ def main() -> None: | 115 | 115 |
| 116 | 116 | # Register a deep-linking handler | 117 | 117 | dispatcher.add_handler( | 118 | | - CommandHandler("start", deep_linked_level_1, Filters.regex(CHECK_THIS_OUT)) | | 118 | + CommandHandler("start", deep_linked_level_1, filters.Regex(CHECK_THIS_OUT)) | 119 | 119 | ) | 120 | 120 |
| 121 | 121 | # This one works with a textual link instead of an URL | 122 | | - dispatcher.add_handler(CommandHandler("start", deep_linked_level_2, Filters.regex(SO_COOL))) | | 122 | + dispatcher.add_handler(CommandHandler("start", deep_linked_level_2, filters.Regex(SO_COOL))) | 123 | 123 |
| 124 | 124 | # We can also pass on the deep-linking payload | 125 | 125 | dispatcher.add_handler( | 126 | | - CommandHandler("start", deep_linked_level_3, Filters.regex(USING_ENTITIES)) | | 126 | + CommandHandler("start", deep_linked_level_3, filters.Regex(USING_ENTITIES)) | 127 | 127 | ) | 128 | 128 |
| 129 | 129 | # Possible with inline keyboard buttons as well | 130 | 130 | dispatcher.add_handler( | 131 | | - CommandHandler("start", deep_linked_level_4, Filters.regex(USING_KEYBOARD)) | | 131 | + CommandHandler("start", deep_linked_level_4, filters.Regex(USING_KEYBOARD)) | 132 | 132 | ) | 133 | 133 |
| 134 | 134 | # register callback handler for inline keyboard button | |
Collapse file Copy file name to clipboardExpand all lines: examples/echobot.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | from telegram.ext import ( | 22 | 22 | CommandHandler, | 23 | 23 | MessageHandler, | 24 | | - Filters, | | 24 | + filters, | 25 | 25 | Updater, | 26 | 26 | CallbackContext, | 27 | 27 | ) | @@ -68,7 +68,7 @@ def main() -> None: | 68 | 68 | dispatcher.add_handler(CommandHandler("help", help_command)) | 69 | 69 |
| 70 | 70 | # on non command i.e message - echo the message on Telegram | 71 | | - dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo)) | | 71 | + dispatcher.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo)) | 72 | 72 |
| 73 | 73 | # Start the Bot | 74 | 74 | updater.start_polling() | |
Collapse file Copy file name to clipboardExpand all lines: examples/nestedconversationbot.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | from telegram.ext import ( | 22 | 22 | CommandHandler, | 23 | 23 | MessageHandler, | 24 | | - Filters, | | 24 | + filters, | 25 | 25 | ConversationHandler, | 26 | 26 | CallbackQueryHandler, | 27 | 27 | Updater, | @@ -319,7 +319,7 @@ def main() -> None: | 319 | 319 | SELECTING_FEATURE: [ | 320 | 320 | CallbackQueryHandler(ask_for_input, pattern='^(?!' + str(END) + ').*$') | 321 | 321 | ], | 322 | | - TYPING: [MessageHandler(Filters.text & ~Filters.command, save_input)], | | 322 | + TYPING: [MessageHandler(filters.TEXT & ~filters.COMMAND, save_input)], | 323 | 323 | }, | 324 | 324 | fallbacks=[ | 325 | 325 | CallbackQueryHandler(end_describing, pattern='^' + str(END) + '$'), | |
Collapse file Copy file name to clipboardExpand all lines: examples/passportbot.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | from pathlib import Path | 15 | 15 |
| 16 | 16 | from telegram import Update | 17 | | -from telegram.ext import MessageHandler, Filters, Updater, CallbackContext | | 17 | +from telegram.ext import MessageHandler, filters, Updater, CallbackContext | 18 | 18 |
| 19 | 19 | # Enable logging | 20 | 20 |
| @@ -110,7 +110,7 @@ def main() -> None: | 110 | 110 | dispatcher = updater.dispatcher | 111 | 111 |
| 112 | 112 | # On messages that include passport data call msg | 113 | | - dispatcher.add_handler(MessageHandler(Filters.passport_data, msg)) | | 113 | + dispatcher.add_handler(MessageHandler(filters.PASSPORT_DATA, msg)) | 114 | 114 |
| 115 | 115 | # Start the Bot | 116 | 116 | updater.start_polling() | |
Collapse file Copy file name to clipboardExpand all lines: examples/paymentbot.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | from telegram.ext import ( | 11 | 11 | CommandHandler, | 12 | 12 | MessageHandler, | 13 | | - Filters, | | 13 | + filters, | 14 | 14 | PreCheckoutQueryHandler, | 15 | 15 | ShippingQueryHandler, | 16 | 16 | Updater, | @@ -149,7 +149,7 @@ def main() -> None: | 149 | 149 | dispatcher.add_handler(PreCheckoutQueryHandler(precheckout_callback)) | 150 | 150 |
| 151 | 151 | # Success! Notify your user! | 152 | | - dispatcher.add_handler(MessageHandler(Filters.successful_payment, successful_payment_callback)) | | 152 | + dispatcher.add_handler(MessageHandler(filters.SUCCESSFUL_PAYMENT, successful_payment_callback)) | 153 | 153 |
| 154 | 154 | # Start the Bot | 155 | 155 | updater.start_polling() | |
Collapse file Copy file name to clipboardExpand all lines: examples/persistentconversationbot.py +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | from telegram.ext import ( | 22 | 22 | CommandHandler, | 23 | 23 | MessageHandler, | 24 | | - Filters, | | 24 | + filters, | 25 | 25 | ConversationHandler, | 26 | 26 | PicklePersistence, | 27 | 27 | Updater, | @@ -144,23 +144,23 @@ def main() -> None: | 144 | 144 | states={ | 145 | 145 | CHOOSING: [ | 146 | 146 | MessageHandler( | 147 | | - Filters.regex('^(Age|Favourite colour|Number of siblings)$'), regular_choice | | 147 | + filters.Regex('^(Age|Favourite colour|Number of siblings)$'), regular_choice | 148 | 148 | ), | 149 | | - MessageHandler(Filters.regex('^Something else...$'), custom_choice), | | 149 | + MessageHandler(filters.Regex('^Something else...$'), custom_choice), | 150 | 150 | ], | 151 | 151 | TYPING_CHOICE: [ | 152 | 152 | MessageHandler( | 153 | | - Filters.text & ~(Filters.command | Filters.regex('^Done$')), regular_choice | | 153 | + filters.TEXT & ~(filters.COMMAND | filters.Regex('^Done$')), regular_choice | 154 | 154 | ) | 155 | 155 | ], | 156 | 156 | TYPING_REPLY: [ | 157 | 157 | MessageHandler( | 158 | | - Filters.text & ~(Filters.command | Filters.regex('^Done$')), | | 158 | + filters.TEXT & ~(filters.COMMAND | filters.Regex('^Done$')), | 159 | 159 | received_information, | 160 | 160 | ) | 161 | 161 | ], | 162 | 162 | }, | 163 | | - fallbacks=[MessageHandler(Filters.regex('^Done$'), done)], | | 163 | + fallbacks=[MessageHandler(filters.Regex('^Done$'), done)], | 164 | 164 | name="my_conversation", | 165 | 165 | persistent=True, | 166 | 166 | ) | |
|