- Notifications
You must be signed in to change notification settings - Fork5.7k
Bot API 8.0:PreparedInlineMessage
andBot.save_prepared_inline_message
#4574
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
1c99096
Add `PreparedInlineMessage`
Bibo-Joshi5a0b72f
Make `PreparedInlineMessage.expiraten_date` Required
Bibo-Joshi5739854
Add `Bot.save_prepared_inline_message`
Bibo-Joshib359aa4
Fix tests
Bibo-Joshib0209c3
Fix module docstring
Bibo-Joshidb12cca
Merge branch 'api-8.0' into api-8.0-preparedinlinemessage
Bibo-JoshiFile 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
2 changes: 2 additions & 0 deletionsdocs/source/inclusions/bot_methods.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
1 change: 1 addition & 0 deletionsdocs/source/telegram.inline-tree.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
6 changes: 6 additions & 0 deletionsdocs/source/telegram.preparedinlinemessage.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
PreparedInlineMessage | ||
===================== | ||
.. autoclass:: telegram.PreparedInlineMessage | ||
:members: | ||
:show-inheritance: |
2 changes: 2 additions & 0 deletionstelegram/__init__.py
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
62 changes: 62 additions & 0 deletionstelegram/_bot.py
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
83 changes: 83 additions & 0 deletionstelegram/_inline/preparedinlinemessage.py
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#!/usr/bin/env python | ||
# | ||
# A library that provides a Python interface to the Telegram Bot API | ||
# Copyright (C) 2015-2024 | ||
# Leandro Toledo de Souza <devs@python-telegram-bot.org> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser Public License | ||
# along with this program. If not, see [http://www.gnu.org/licenses/]. | ||
"""This module contains an object that represents a Telegram Prepared inline Message.""" | ||
import datetime as dtm | ||
from typing import TYPE_CHECKING, Optional | ||
from telegram._telegramobject import TelegramObject | ||
from telegram._utils.datetime import extract_tzinfo_from_defaults, from_timestamp | ||
from telegram._utils.types import JSONDict | ||
if TYPE_CHECKING: | ||
from telegram import Bot | ||
class PreparedInlineMessage(TelegramObject): | ||
"""Describes an inline message to be sent by a user of a Mini App. | ||
Objects of this class are comparable in terms of equality. Two objects of this class are | ||
considered equal, if their :attr:`id` is equal. | ||
.. versionadded:: NEXT.VERSION | ||
Args: | ||
id (:obj:`str`): Unique identifier of the prepared message | ||
expiration_date (:class:`datetime.datetime`): Expiration date of the prepared message. | ||
Expired prepared messages can no longer be used. | ||
|datetime_localization| | ||
Attributes: | ||
id (:obj:`str`): Unique identifier of the prepared message | ||
expiration_date (:class:`datetime.datetime`): Expiration date of the prepared message. | ||
Expired prepared messages can no longer be used. | ||
|datetime_localization| | ||
""" | ||
__slots__ = ("expiration_date", "id") | ||
def __init__( | ||
self, | ||
id: str, # pylint: disable=redefined-builtin | ||
expiration_date: dtm.datetime, | ||
*, | ||
api_kwargs: Optional[JSONDict] = None, | ||
): | ||
super().__init__(api_kwargs=api_kwargs) | ||
self.id: str = id | ||
self.expiration_date: dtm.datetime = expiration_date | ||
self._id_attrs = (self.id,) | ||
self._freeze() | ||
@classmethod | ||
def de_json( | ||
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None | ||
) -> Optional["PreparedInlineMessage"]: | ||
"""See :meth:`telegram.TelegramObject.de_json`.""" | ||
data = cls._parse_data(data) | ||
if data is None: | ||
return None | ||
# Get the local timezone from the bot if it has defaults | ||
loc_tzinfo = extract_tzinfo_from_defaults(bot) | ||
data["expiration_date"] = from_timestamp(data.get("expiration_date"), tzinfo=loc_tzinfo) | ||
return super().de_json(data=data, bot=bot) |
32 changes: 32 additions & 0 deletionstelegram/ext/_extbot.py
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
108 changes: 108 additions & 0 deletionstests/_inline/test_preparedinlinemessage.py
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#!/usr/bin/env python | ||
# | ||
# A library that provides a Python interface to the Telegram Bot API | ||
# Copyright (C) 2015-2024 | ||
# Leandro Toledo de Souza <devs@python-telegram-bot.org> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser Public License | ||
# along with this program. If not, see [http://www.gnu.org/licenses/]. | ||
import datetime as dtm | ||
import pytest | ||
from telegram import Location, PreparedInlineMessage | ||
from telegram._utils.datetime import UTC, to_timestamp | ||
from tests.auxil.slots import mro_slots | ||
@pytest.fixture(scope="module") | ||
def prepared_inline_message(): | ||
return PreparedInlineMessage( | ||
PreparedInlineMessageTestBase.id, | ||
PreparedInlineMessageTestBase.expiration_date, | ||
) | ||
class PreparedInlineMessageTestBase: | ||
id = "some_uid" | ||
expiration_date = dtm.datetime.now(tz=UTC).replace(microsecond=0) | ||
class TestPreparedInlineMessageWithoutRequest(PreparedInlineMessageTestBase): | ||
def test_slot_behaviour(self, prepared_inline_message): | ||
inst = prepared_inline_message | ||
for attr in inst.__slots__: | ||
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'" | ||
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot" | ||
def test_expected_values(self, prepared_inline_message): | ||
assert prepared_inline_message.id == self.id | ||
assert prepared_inline_message.expiration_date == self.expiration_date | ||
def test_de_json(self, prepared_inline_message): | ||
json_dict = { | ||
"id": self.id, | ||
"expiration_date": to_timestamp(self.expiration_date), | ||
} | ||
new_prepared_inline_message = PreparedInlineMessage.de_json(json_dict, None) | ||
assert isinstance(new_prepared_inline_message, PreparedInlineMessage) | ||
assert new_prepared_inline_message.id == prepared_inline_message.id | ||
assert ( | ||
new_prepared_inline_message.expiration_date == prepared_inline_message.expiration_date | ||
) | ||
def test_de_json_localization(self, offline_bot, raw_bot, tz_bot): | ||
json_dict = { | ||
"id": "some_uid", | ||
"expiration_date": to_timestamp(self.expiration_date), | ||
} | ||
pim = PreparedInlineMessage.de_json(json_dict, offline_bot) | ||
pim_raw = PreparedInlineMessage.de_json(json_dict, raw_bot) | ||
pim_tz = PreparedInlineMessage.de_json(json_dict, tz_bot) | ||
# comparing utcoffset because comparing tzinfo objects is not reliable | ||
offset = pim_tz.expiration_date.utcoffset() | ||
offset_tz = tz_bot.defaults.tzinfo.utcoffset(pim_tz.expiration_date.replace(tzinfo=None)) | ||
assert pim.expiration_date.tzinfo == UTC | ||
assert pim_raw.expiration_date.tzinfo == UTC | ||
assert offset_tz == offset | ||
def test_to_dict(self, prepared_inline_message): | ||
prepared_inline_message_dict = prepared_inline_message.to_dict() | ||
assert isinstance(prepared_inline_message_dict, dict) | ||
assert prepared_inline_message_dict["id"] == prepared_inline_message.id | ||
assert prepared_inline_message_dict["expiration_date"] == to_timestamp( | ||
self.expiration_date | ||
) | ||
def test_equality(self, prepared_inline_message): | ||
a = prepared_inline_message | ||
b = PreparedInlineMessage(self.id, self.expiration_date) | ||
c = PreparedInlineMessage(self.id, self.expiration_date + dtm.timedelta(seconds=1)) | ||
d = PreparedInlineMessage("other_uid", self.expiration_date) | ||
e = Location(123, 456) | ||
assert a == b | ||
assert hash(a) == hash(b) | ||
assert a == c | ||
assert hash(a) == hash(c) | ||
assert a != d | ||
assert hash(a) != hash(d) | ||
assert a != e | ||
assert hash(a) != hash(e) |
10 changes: 10 additions & 0 deletionstests/test_bot.py
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.