- Notifications
You must be signed in to change notification settings - Fork5.7k
Implement game_pattern support in CQH#4353
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Bibo-Joshi merged 28 commits intopython-telegram-bot:masterfromjainamoswal:jainamoswal-patch-1Jul 13, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
28 commits Select commitHold shift + click to select a range
55dba23
Added game_pattern to CQH
jainamoswald78e1d5
Added myself in AUTHORS.rst
jainamoswalb3869a5
Fixed a typo in Docstring
jainamoswalf897e3a
Updated handling logic
jainamoswal5bfe727
Added ".. versionadded:: NEXT.VERSION" directive
jainamoswal413823f
Update callbackqueryhandler.py
jainamoswal5cac778
Fixed for tests failing.
jainamoswal2253883
Updated according to pylint recommendations
jainamoswal4193b41
Merge branch 'python-telegram-bot:master' into jainamoswal-patch-1
jainamoswal034f623
Removed callable from game_pattern
f524f3c
Fix type hinting
jainamoswalb0910dc
Updated file
jainamoswal39e1b37
Merge branch 'python-telegram-bot:master' into jainamoswal-patch-1
jainamoswal05e9fd0
Added CSI-style comments & reduced indent
jainamoswal21f4b3b
Merge branch 'jainamoswal-patch-1' of github.com:jainamoswal/python-t…
jainamoswal7759df7
Added tests for game_short_name
jainamoswala60f8a0
pylint fixes
jainamoswala17b042
Merge branch 'master' into jainamoswal-patch-1
jainamoswal1f781cb
Refine tests, update docs & some refactor to code
jainamoswala821f82
Refined tests
jainamoswalbbc9864
Refactor
e96042e
pylint fixes
jainamoswalc19eb0a
ahh, again pylint fixes
jainamoswal1deaff1
pylint pylint pylint
jainamoswal6fd56b5
fixed E121
jainamoswal51b3f82
pylint
jainamoswaldc8db09
Merge branch 'master' into jainamoswal-patch-1
jainamoswal57db295
Fixed tests
jainamoswalFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsAUTHORS.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
72 changes: 54 additions & 18 deletionstelegram/ext/_handlers/callbackqueryhandler.py
jainamoswal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -51,6 +51,15 @@ class CallbackQueryHandler(BaseHandler[Update, CCT]): | ||
.. versionadded:: 13.6 | ||
* If neither :paramref:`pattern` nor :paramref:`game_pattern` is set, `any` | ||
``CallbackQuery`` will be handled. If only :paramref:`pattern` is set, queries with | ||
:attr:`~telegram.CallbackQuery.game_short_name` will `not` be considered and vice versa. | ||
If both patterns are set, queries with either :attr: | ||
`~telegram.CallbackQuery.game_short_name` or :attr:`~telegram.CallbackQuery.data` | ||
matching the defined pattern will be handled | ||
.. versionadded:: NEXT.VERSION | ||
Warning: | ||
When setting :paramref:`block` to :obj:`False`, you cannot rely on adding custom | ||
attributes to :class:`telegram.ext.CallbackContext`. See its docs for more info. | ||
@@ -85,6 +94,13 @@ async def callback(update: Update, context: CallbackContext) | ||
.. versionchanged:: 13.6 | ||
Added support for arbitrary callback data. | ||
game_pattern (:obj:`str` | :func:`re.Pattern <re.compile>` | optional) | ||
Pattern to test :attr:`telegram.CallbackQuery.game_short_name` against. If a string or | ||
a regex pattern is passed, :func:`re.match` is used on | ||
:attr:`telegram.CallbackQuery.game_short_name` to determine if an update should be | ||
handled by this handler. | ||
jainamoswal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
.. versionadded:: NEXT.VERSION | ||
block (:obj:`bool`, optional): Determines whether the return value of the callback should | ||
be awaited before processing the next handler in | ||
:meth:`telegram.ext.Application.process_update`. Defaults to :obj:`True`. | ||
@@ -98,20 +114,23 @@ async def callback(update: Update, context: CallbackContext) | ||
.. versionchanged:: 13.6 | ||
Added support for arbitrary callback data. | ||
game_pattern (:func:`re.Pattern <re.compile>`): Optional. | ||
Regex pattern to test :attr:`telegram.CallbackQuery.game_short_name` | ||
block (:obj:`bool`): Determines whether the return value of the callback should be | ||
awaited before processing the next handler in | ||
:meth:`telegram.ext.Application.process_update`. | ||
""" | ||
__slots__ = ("game_pattern", "pattern") | ||
def __init__( | ||
self, | ||
callback: HandlerCallback[Update, CCT, RT], | ||
pattern: Optional[ | ||
Union[str, Pattern[str], type, Callable[[object], Optional[bool]]] | ||
] = None, | ||
game_pattern: Optional[Union[str, Pattern[str]]] = None, | ||
block: DVType[bool] = DEFAULT_TRUE, | ||
): | ||
super().__init__(callback, block=block) | ||
@@ -120,13 +139,15 @@ def __init__( | ||
raise TypeError( | ||
"The `pattern` must not be a coroutine function! Use an ordinary function instead." | ||
) | ||
if isinstance(pattern, str): | ||
pattern = re.compile(pattern) | ||
if isinstance(game_pattern, str): | ||
game_pattern = re.compile(game_pattern) | ||
self.pattern: Optional[ | ||
Union[str, Pattern[str], type, Callable[[object], Optional[bool]]] | ||
] = pattern | ||
self.game_pattern: Optional[Union[str, Pattern[str]]] = game_pattern | ||
def check_update(self, update: object) -> Optional[Union[bool, object]]: | ||
"""Determines whether an update should be passed to this handler's :attr:`callback`. | ||
@@ -139,22 +160,37 @@ def check_update(self, update: object) -> Optional[Union[bool, object]]: | ||
""" | ||
# pylint: disable=too-many-return-statements | ||
if not (isinstance(update, Update) and update.callback_query): | ||
return None | ||
callback_data = update.callback_query.data | ||
game_short_name = update.callback_query.game_short_name | ||
if not any([self.pattern, self.game_pattern]): | ||
return True | ||
# we check for .data or .game_short_name from update to filter based on whats coming | ||
# this gives xor-like behavior | ||
if callback_data: | ||
if not self.pattern: | ||
return False | ||
if isinstance(self.pattern, type): | ||
return isinstance(callback_data, self.pattern) | ||
if callable(self.pattern): | ||
return self.pattern(callback_data) | ||
if not isinstance(callback_data, str): | ||
return False | ||
if match := re.match(self.pattern, callback_data): | ||
return match | ||
elif game_short_name: | ||
if not self.game_pattern: | ||
return False | ||
if match := re.match(self.game_pattern, game_short_name): | ||
return match | ||
else: | ||
return True | ||
return False | ||
def collect_additional_context( | ||
self, | ||
44 changes: 44 additions & 0 deletionstests/ext/test_callbackqueryhandler.py
Bibo-Joshi marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.