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
Add invoice_payload
filtering
#4005
Changes from 10 commits
96715df
1259f69
954167d
576f9d5
e7153cd
5324ab1
3c8b054
84fd314
75b05fa
c21e86d
25276a9
f093609
8b677e1
07492ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,9 +19,16 @@ | |||||||||||
"""This module contains the PreCheckoutQueryHandler class.""" | ||||||||||||
|
||||||||||||
|
||||||||||||
import re | ||||||||||||
from typing import Optional, Pattern, TypeVar, Union | ||||||||||||
|
||||||||||||
from telegram import Update | ||||||||||||
from telegram._utils.defaultvalue import DEFAULT_TRUE | ||||||||||||
from telegram._utils.types import DVType | ||||||||||||
from telegram.ext._basehandler import BaseHandler | ||||||||||||
from telegram.ext._utils.types import CCT | ||||||||||||
from telegram.ext._utils.types import CCT, HandlerCallback | ||||||||||||
|
||||||||||||
RT = TypeVar("RT") | ||||||||||||
|
||||||||||||
|
||||||||||||
class PreCheckoutQueryHandler(BaseHandler[Update, CCT]): | ||||||||||||
|
@@ -43,6 +50,10 @@ async def callback(update: Update, context: CallbackContext) | |||||||||||
|
||||||||||||
The return value of the callback is usually ignored except for the special case of | ||||||||||||
:class:`telegram.ext.ConversationHandler`. | ||||||||||||
pattern (:obj:`str` | :func:`re.Pattern <re.compile>`, optional): Optional. Regex pattern | ||||||||||||
to test :attr:`telegram.PreCheckoutQuery.invoice_payload` against. | ||||||||||||
|
||||||||||||
.. 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`. | ||||||||||||
|
@@ -51,11 +62,28 @@ async def callback(update: Update, context: CallbackContext) | |||||||||||
|
||||||||||||
Attributes: | ||||||||||||
callback (:term:`coroutine function`): The callback function for this handler. | ||||||||||||
pattern (:obj:`str` | :func:`re.Pattern <re.compile>`, optional): Optional. Regex pattern | ||||||||||||
to test :attr:`telegram.PreCheckoutQuery.invoice_payload` against. | ||||||||||||
|
||||||||||||
.. versionadded:: NEXT.VERSION | ||||||||||||
block (:obj:`bool`): Determines whether the callback will run in a blocking way.. | ||||||||||||
|
||||||||||||
""" | ||||||||||||
|
||||||||||||
__slots__ = () | ||||||||||||
__slots__ = ("pattern",) | ||||||||||||
|
||||||||||||
def __init__( | ||||||||||||
self, | ||||||||||||
callback: HandlerCallback[Update, CCT, RT], | ||||||||||||
pattern: Optional[Union[str, Pattern[str]]] = None, | ||||||||||||
block: DVType[bool] = DEFAULT_TRUE, | ||||||||||||
): | ||||||||||||
super().__init__(callback, block=block) | ||||||||||||
|
||||||||||||
if isinstance(pattern, str): | ||||||||||||
pattern = re.compile(pattern) | ||||||||||||
|
||||||||||||
self.pattern: Optional[Union[str, Pattern[str]]] = pattern | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Just a bit shorter and also gives the attribute a more narrow type. |
||||||||||||
|
||||||||||||
def check_update(self, update: object) -> bool: | ||||||||||||
"""Determines whether an update should be passed to this handler's :attr:`callback`. | ||||||||||||
|
@@ -67,4 +95,11 @@ def check_update(self, update: object) -> bool: | |||||||||||
:obj:`bool` | ||||||||||||
|
||||||||||||
""" | ||||||||||||
return isinstance(update, Update) and bool(update.pre_checkout_query) | ||||||||||||
if isinstance(update, Update) and update.pre_checkout_query: | ||||||||||||
invoice_payload = update.pre_checkout_query.invoice_payload | ||||||||||||
if self.pattern: | ||||||||||||
if re.match(self.pattern, invoice_payload): | ||||||||||||
return True | ||||||||||||
else: | ||||||||||||
return True | ||||||||||||
return False |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,12 +69,15 @@ def false_update(request): | |
|
||
@pytest.fixture(scope="class") | ||
def pre_checkout_query(): | ||
return Update( | ||
update = Update( | ||
1, | ||
pre_checkout_query=PreCheckoutQuery( | ||
"id", User(1, "test user", False), "EUR", 223, "invoice_payload" | ||
), | ||
) | ||
update._unfreeze() | ||
update.pre_checkout_query._unfreeze() | ||
return update | ||
|
||
|
||
class TestPreCheckoutQueryHandler: | ||
|
@@ -103,6 +106,14 @@ async def callback(self, update, context): | |
and isinstance(update.pre_checkout_query, PreCheckoutQuery) | ||
) | ||
|
||
def test_with_pattern(self, pre_checkout_query): | ||
handler = PreCheckoutQueryHandler(self.callback, pattern=".*voice.*") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also test passing a compiled pattern :) |
||
|
||
assert handler.check_update(pre_checkout_query) | ||
|
||
pre_checkout_query.pre_checkout_query.invoice_payload = "nothing here" | ||
assert not handler.check_update(pre_checkout_query) | ||
|
||
def test_other_update_types(self, false_update): | ||
handler = PreCheckoutQueryHandler(self.callback) | ||
assert not handler.check_update(false_update) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please move the argument to the end of the list. Otherwise this would be a breaking change as
PCQH(callback, False)
would no longer work as expected. Please also move the docstring entries for argument and attribute above.