Skip to content
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

Adding parameter test_env to bot #3972

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/inclusions/bot_methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@
- The last name of the bot
* - :attr:`~telegram.Bot.local_mode`
- Whether the bot is running in local mode
* - :attr:`~telegram.Bot.test_env`
- Whether the bot should use the test endpoints
* - :attr:`~telegram.Bot.username`
- The username of the bot, without leading ``@``
* - :attr:`~telegram.Bot.link`
Expand Down
17 changes: 15 additions & 2 deletions telegram/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@
this is that files are uploaded using their local path in the
`file URI scheme <https://en.wikipedia.org/wiki/File_URI_scheme>`_.
Defaults to :obj:`False`.

test_env (:obj:`bool`, optional): If set to :obj:`True` appends /test to api route for method
calling. This should be set if you want to test your Mini Apps in the test environment.
See `Testing Mini Apps <https://core.telegram.org/bots/webapps#testing-mini-apps>`_.
.. versionadded:: 20.0.

.. include:: inclusions/bot_methods.rst
Expand All @@ -230,6 +232,7 @@
"_request",
"_initialized",
"_local_mode",
"_test_env",
)

def __init__(
Expand All @@ -242,6 +245,7 @@
private_key: Optional[bytes] = None,
private_key_password: Optional[bytes] = None,
local_mode: bool = False,
test_env: bool = False,
):
super().__init__(api_kwargs=None)
if not token:
Expand All @@ -251,6 +255,7 @@
self._base_url: str = base_url + self._token
self._base_file_url: str = base_file_url + self._token
self._local_mode: bool = local_mode
self._test_env: bool = test_env
self._bot_user: Optional[User] = None
self._private_key: Optional[bytes] = None
self._initialized: bool = False
Expand Down Expand Up @@ -408,6 +413,14 @@
"""
return self._local_mode

@property
def test_env(self) -> bool:
""":obj:`bool`: Whether this bot is using the test endpoint.

.. versionadded:: ??
"""
return self._test_env

Check warning on line 422 in telegram/_bot.py

View check run for this annotation

Codecov / codecov/patch

telegram/_bot.py#L422

Added line #L422 was not covered by tests

# Proper type hints are difficult because:
# 1. cryptography doesn't have a nice base class, so it would get lengthy
# 2. we can't import cryptography if it's not installed
Expand Down Expand Up @@ -639,7 +652,7 @@
request = self._request[0] if endpoint == "getUpdates" else self._request[1]

return await request.post(
url=f"{self._base_url}/{endpoint}",
url=f"{self._base_url}" +("/test" if self._test_env else "") + f"/{endpoint}",
request_data=request_data,
read_timeout=read_timeout,
write_timeout=write_timeout,
Expand Down
22 changes: 22 additions & 0 deletions telegram/ext/_applicationbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
("private_key", "private_key"),
("rate_limiter", "rate_limiter instance"),
("local_mode", "local_mode setting"),
("test_env", "test_env setting"),
]

_TWO_ARGS_REQ = "The parameter `{}` may only be set, if no {} was set."
Expand Down Expand Up @@ -167,6 +168,7 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]):
"_updater",
"_write_timeout",
"_local_mode",
"_test_env",
"_http_version",
)

Expand Down Expand Up @@ -196,6 +198,7 @@ def __init__(self: "InitApplicationBuilder"):
self._defaults: ODVInput[Defaults] = DEFAULT_NONE
self._arbitrary_callback_data: Union[DefaultValue[bool], int] = DEFAULT_FALSE
self._local_mode: DVType[bool] = DEFAULT_FALSE
self._test_env: DVType[bool] = DEFAULT_FALSE
self._bot: DVInput[Bot] = DEFAULT_NONE
self._update_queue: DVType[Queue] = DefaultValue(Queue())

Expand Down Expand Up @@ -273,6 +276,7 @@ def _build_ext_bot(self) -> ExtBot:
get_updates_request=self._build_request(get_updates=True),
rate_limiter=DefaultValue.get_value(self._rate_limiter),
local_mode=DefaultValue.get_value(self._local_mode),
test_env=DefaultValue.get_value(self._test_env),
)

def _bot_check(self, name: str) -> None:
Expand Down Expand Up @@ -1006,6 +1010,24 @@ def local_mode(self: BuilderType, local_mode: bool) -> BuilderType:
self._local_mode = local_mode
return self

def test_env(self: BuilderType, test_env: bool) -> BuilderType:
"""Specifies the value for :paramref:`~telegram.Bot.test_env` for the
:attr:`telegram.ext.Application.bot`.
If not called, will default to :obj:`False`.

.. seealso:: See `Testing Mini Apps <https://core.telegram.org/bots/webapps#testing-mini-apps>`_.

Args:
test_env (:obj:`bool`): Whether the bot should use the test routes for test environment

Returns:
:class:`ApplicationBuilder`: The same builder with the updated argument.
"""
self._bot_check("test_env")
self._updater_check("test_env")
self._test_env = test_env
return self

def bot(
self: "ApplicationBuilder[BT, CCT, UD, CD, BD, JQ]",
bot: InBT,
Expand Down
4 changes: 4 additions & 0 deletions telegram/ext/_extbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def __init__(
defaults: Optional["Defaults"] = None,
arbitrary_callback_data: Union[bool, int] = False,
local_mode: bool = False,
test_env: bool = False,
):
...

Expand All @@ -196,6 +197,7 @@ def __init__(
defaults: Optional["Defaults"] = None,
arbitrary_callback_data: Union[bool, int] = False,
local_mode: bool = False,
test_env: bool = False,
rate_limiter: Optional["BaseRateLimiter[RLARGS]"] = None,
):
...
Expand All @@ -212,6 +214,7 @@ def __init__(
defaults: Optional["Defaults"] = None,
arbitrary_callback_data: Union[bool, int] = False,
local_mode: bool = False,
test_env: bool = False,
rate_limiter: Optional["BaseRateLimiter[RLARGS]"] = None,
):
super().__init__(
Expand All @@ -223,6 +226,7 @@ def __init__(
private_key=private_key,
private_key_password=private_key_password,
local_mode=local_mode,
test_env=test_env,
)
with self._unfrozen():
self._defaults: Optional[Defaults] = defaults
Expand Down