From 6bba2da814aeab1eaf4253014234fb6bc79457b3 Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 15:42:04 +0500 Subject: [PATCH 01/10] Add CallbackQuery.answer --- aiogram/api/types/callback_query.py | 27 +++++++++++++++++++ docs/api/methods/answer_callback_query.md | 2 ++ docs/api/types/callback_query.md | 13 +++++++++ .../test_types/test_callback_query.py | 14 ++++++++++ 4 files changed, 56 insertions(+) create mode 100644 tests/test_api/test_types/test_callback_query.py diff --git a/aiogram/api/types/callback_query.py b/aiogram/api/types/callback_query.py index 87f1ad42..1991ca8d 100644 --- a/aiogram/api/types/callback_query.py +++ b/aiogram/api/types/callback_query.py @@ -9,6 +9,7 @@ from .base import TelegramObject if TYPE_CHECKING: # pragma: no cover from .message import Message from .user import User + from ..methods import AnswerCallbackQuery class CallbackQuery(TelegramObject): @@ -43,3 +44,29 @@ class CallbackQuery(TelegramObject): data in this field.""" game_short_name: Optional[str] = None """Short name of a Game to be returned, serves as the unique identifier for the game""" + + def answer( + self, + text: Optional[str] = None, + show_alert: Optional[bool] = None, + url: Optional[str] = None, + cache_time: Optional[int] = None, + ) -> AnswerCallbackQuery: + """ + Answer to callback query + + :param text: + :param show_alert: + :param url: + :param cache_time: + :return: + """ + from ..methods import AnswerCallbackQuery + + return AnswerCallbackQuery( + callback_query_id=self.id, + text=text, + show_alert=show_alert, + url=url, + cache_time=cache_time, + ) diff --git a/docs/api/methods/answer_callback_query.md b/docs/api/methods/answer_callback_query.md index 63afe5df..8bc4160b 100644 --- a/docs/api/methods/answer_callback_query.md +++ b/docs/api/methods/answer_callback_query.md @@ -60,3 +60,5 @@ return AnswerCallbackQuery(...) ## Related pages: - [Official documentation](https://core.telegram.org/bots/api#answercallbackquery) +- [aiogram.types.CallbackQuery](../types/callback_query.md) +- [Aliases](../types/callback_query.md#aliases) diff --git a/docs/api/types/callback_query.md b/docs/api/types/callback_query.md index 1a7c5312..556e4cb2 100644 --- a/docs/api/types/callback_query.md +++ b/docs/api/types/callback_query.md @@ -27,8 +27,21 @@ NOTE: After the user presses a callback button, Telegram clients will display a - `from aiogram.api.types import CallbackQuery` - `from aiogram.api.types.callback_query import CallbackQuery` +## Aliases + +Aliases is always returns related API method (Awaitable) and can be used directly or as answer's into webhook. + +### Answer + +This method has the same specification with the API but without `callback_query_id` argument. + +| Answer method | Alias for | Description | +| - | - | - | +| `answer` | [Bot.answer_callback_query](../methods/answer_callback_query.md) | Answer to callback query | + ## Related pages: - [Official documentation](https://core.telegram.org/bots/api#callbackquery) - [aiogram.types.Message](../types/message.md) - [aiogram.types.User](../types/user.md) +- [aiogram.methods.AnswerCallbackQuery](../methods/answer_callback_query.md) diff --git a/tests/test_api/test_types/test_callback_query.py b/tests/test_api/test_types/test_callback_query.py new file mode 100644 index 00000000..f76c9be9 --- /dev/null +++ b/tests/test_api/test_types/test_callback_query.py @@ -0,0 +1,14 @@ +from aiogram.api.methods import AnswerCallbackQuery +from aiogram.api.types import CallbackQuery, User + + +class TestCallbackQuery: + def test_answer_alias(self): + callback_query = CallbackQuery( + id="id", from_user=User(id=42, is_bot=False, first_name="name"), chat_instance="chat" + ) + + api_method = callback_query.answer() + + assert isinstance(api_method, AnswerCallbackQuery) + assert api_method.callback_query_id == callback_query.id From 65002b9280b5f29e6893d308609c9c71858b4d5e Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 16:24:42 +0500 Subject: [PATCH 02/10] Add InlineQuery.answer --- aiogram/api/types/inline_query.py | 34 ++++++++++++++++++- docs/api/methods/answer_inline_query.md | 2 ++ docs/api/types/inline_query.md | 15 ++++++++ .../test_api/test_types/test_inline_query.py | 17 ++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/test_api/test_types/test_inline_query.py diff --git a/aiogram/api/types/inline_query.py b/aiogram/api/types/inline_query.py index f86ecc7a..29373d39 100644 --- a/aiogram/api/types/inline_query.py +++ b/aiogram/api/types/inline_query.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, List, Optional from pydantic import Field @@ -9,6 +9,8 @@ from .base import TelegramObject if TYPE_CHECKING: # pragma: no cover from .location import Location from .user import User + from .inline_query_result import InlineQueryResult + from ..methods import AnswerInlineQuery class InlineQuery(TelegramObject): @@ -29,3 +31,33 @@ class InlineQuery(TelegramObject): """Offset of the results to be returned, can be controlled by the bot""" location: Optional[Location] = None """Sender location, only for bots that request user location""" + + def answer( + self, + results: List[InlineQueryResult], + cache_time: Optional[int] = None, + is_personal: Optional[bool] = None, + next_offset: Optional[str] = None, + switch_pm_text: Optional[str] = None, + switch_pm_parameter: Optional[str] = None, + ) -> AnswerInlineQuery: + """ + :param results: + :param cache_time: + :param is_personal: + :param next_offset: + :param switch_pm_text: + :param switch_pm_parameter: + :return: + """ + from ..methods import AnswerInlineQuery + + return AnswerInlineQuery( + inline_query_id=self.id, + results=results, + cache_time=cache_time, + is_personal=is_personal, + next_offset=next_offset, + switch_pm_text=switch_pm_text, + switch_pm_parameter=switch_pm_parameter, + ) diff --git a/docs/api/methods/answer_inline_query.md b/docs/api/methods/answer_inline_query.md index 88add63d..215b5037 100644 --- a/docs/api/methods/answer_inline_query.md +++ b/docs/api/methods/answer_inline_query.md @@ -62,4 +62,6 @@ return AnswerInlineQuery(...) ## Related pages: - [Official documentation](https://core.telegram.org/bots/api#answerinlinequery) +- [aiogram.types.InlineQuery](../types/inline_query.md) - [aiogram.types.InlineQueryResult](../types/inline_query_result.md) +- [Aliases](../types/inline_query.md#aliases) diff --git a/docs/api/types/inline_query.md b/docs/api/types/inline_query.md index cd3c7545..9d3094b8 100644 --- a/docs/api/types/inline_query.md +++ b/docs/api/types/inline_query.md @@ -23,8 +23,23 @@ This object represents an incoming inline query. When the user sends an empty qu - `from aiogram.api.types import InlineQuery` - `from aiogram.api.types.inline_query import InlineQuery` +## Aliases + +Aliases is always returns related API method (Awaitable) and can be used directly or as answer's into webhook. + +### Answer + +This method has the same specification with the API but without `inline_query_id` argument. + +| Answer method | Alias for | Description | +| - | - | - | +| `answer` | [Bot.answer_inline_query](../methods/answer_inline_query.md) | Answer to inline query | + + ## Related pages: - [Official documentation](https://core.telegram.org/bots/api#inlinequery) - [aiogram.types.Location](../types/location.md) - [aiogram.types.User](../types/user.md) +- [aiogram.methods.AnswerInlineQuery](../methods/answer_inline_query.md) + diff --git a/tests/test_api/test_types/test_inline_query.py b/tests/test_api/test_types/test_inline_query.py new file mode 100644 index 00000000..24657b74 --- /dev/null +++ b/tests/test_api/test_types/test_inline_query.py @@ -0,0 +1,17 @@ +from aiogram.api.methods import AnswerInlineQuery +from aiogram.api.types import InlineQuery, User + + +class TestInlineQuery: + def test_answer_alias(self): + inline_query = InlineQuery( + id="id", + from_user=User(id=42, is_bot=False, first_name="name"), + query="query", + offset="", + ) + + api_method = inline_query.answer([]) + + assert isinstance(api_method, AnswerInlineQuery) + assert api_method.inline_query_id == inline_query.id From cf12da0c4aa098fff4b2c2c5faa79ac8ad80e048 Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 16:40:26 +0500 Subject: [PATCH 03/10] Add PreCheckoutQuery.answer --- aiogram/api/types/pre_checkout_query.py | 13 +++++++++++++ docs/api/methods/answer_pre_checkout_query.md | 2 ++ docs/api/types/pre_checkout_query.md | 14 ++++++++++++++ .../test_types/test_pre_checkout_query.py | 18 ++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 tests/test_api/test_types/test_pre_checkout_query.py diff --git a/aiogram/api/types/pre_checkout_query.py b/aiogram/api/types/pre_checkout_query.py index b3daab57..e99f4378 100644 --- a/aiogram/api/types/pre_checkout_query.py +++ b/aiogram/api/types/pre_checkout_query.py @@ -9,6 +9,7 @@ from .base import TelegramObject if TYPE_CHECKING: # pragma: no cover from .order_info import OrderInfo from .user import User + from ..methods import AnswerPreCheckoutQuery class PreCheckoutQuery(TelegramObject): @@ -35,3 +36,15 @@ class PreCheckoutQuery(TelegramObject): """Identifier of the shipping option chosen by the user""" order_info: Optional[OrderInfo] = None """Order info provided by the user""" + + def answer(self, ok: bool, error_message: Optional[str] = None) -> AnswerPreCheckoutQuery: + """ + :param ok: + :param error_message: + :return: + """ + from ..methods import AnswerPreCheckoutQuery + + return AnswerPreCheckoutQuery( + pre_checkout_query_id=self.id, ok=ok, error_message=error_message, + ) diff --git a/docs/api/methods/answer_pre_checkout_query.md b/docs/api/methods/answer_pre_checkout_query.md index e836fe96..fced3e20 100644 --- a/docs/api/methods/answer_pre_checkout_query.md +++ b/docs/api/methods/answer_pre_checkout_query.md @@ -56,3 +56,5 @@ return AnswerPreCheckoutQuery(...) ## Related pages: - [Official documentation](https://core.telegram.org/bots/api#answerprecheckoutquery) +- [aiogram.types.PreCheckoutQuery](../types/pre_checkout_query.md) +- [Aliases](../types/pre_checkout_query.md#aliases) diff --git a/docs/api/types/pre_checkout_query.md b/docs/api/types/pre_checkout_query.md index 3b6d105b..0fddb155 100644 --- a/docs/api/types/pre_checkout_query.md +++ b/docs/api/types/pre_checkout_query.md @@ -25,8 +25,22 @@ This object contains information about an incoming pre-checkout query. - `from aiogram.api.types import PreCheckoutQuery` - `from aiogram.api.types.pre_checkout_query import PreCheckoutQuery` +## Aliases + +Aliases is always returns related API method (Awaitable) and can be used directly or as answer's into webhook. + +### Answer + +This method has the same specification with the API but without `pre_checkout_query_id` argument. + +| Answer method | Alias for | Description | +| - | - | - | +| `answer` | [Bot.answer_pre_checkout_query](../methods/answer_pre_checkout_query.md) | Answer to pre checkout query | + + ## Related pages: - [Official documentation](https://core.telegram.org/bots/api#precheckoutquery) - [aiogram.types.OrderInfo](../types/order_info.md) - [aiogram.types.User](../types/user.md) +- [aiogram.methods.AnswerPreCheckoutQuery](../methods/answer_pre_checkout_query.md) diff --git a/tests/test_api/test_types/test_pre_checkout_query.py b/tests/test_api/test_types/test_pre_checkout_query.py new file mode 100644 index 00000000..0e79cc0f --- /dev/null +++ b/tests/test_api/test_types/test_pre_checkout_query.py @@ -0,0 +1,18 @@ +from aiogram.api.methods import AnswerPreCheckoutQuery +from aiogram.api.types import PreCheckoutQuery, User + + +class TestPreCheckoutQuery: + def test_answer_alias(self): + pre_checkout_query = PreCheckoutQuery( + id="id", + from_user=User(id=42, is_bot=False, first_name="name"), + currency="currency", + total_amount=123, + invoice_payload="payload", + ) + + api_method = pre_checkout_query.answer(True) + + assert isinstance(api_method, AnswerPreCheckoutQuery) + assert api_method.pre_checkout_query_id == pre_checkout_query.id From 83730276bcf0c2e02975d5cd18887ae5533f886a Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 16:50:49 +0500 Subject: [PATCH 04/10] Add ShippingQuery.answer --- aiogram/api/types/shipping_query.py | 25 ++++++++++++++++++- docs/api/methods/answer_shipping_query.md | 2 ++ docs/api/types/shipping_query.md | 14 +++++++++++ .../test_types/test_shipping_query.py | 24 ++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/test_api/test_types/test_shipping_query.py diff --git a/aiogram/api/types/shipping_query.py b/aiogram/api/types/shipping_query.py index a219be7c..b8228ce1 100644 --- a/aiogram/api/types/shipping_query.py +++ b/aiogram/api/types/shipping_query.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, List, Optional from pydantic import Field @@ -9,6 +9,8 @@ from .base import TelegramObject if TYPE_CHECKING: # pragma: no cover from .shipping_address import ShippingAddress from .user import User + from ..methods import AnswerShippingQuery + from ..types import ShippingOption class ShippingQuery(TelegramObject): @@ -26,3 +28,24 @@ class ShippingQuery(TelegramObject): """Bot specified invoice payload""" shipping_address: ShippingAddress """User specified shipping address""" + + def answer( + self, + ok: bool, + shipping_options: Optional[List[ShippingOption]] = None, + error_message: Optional[str] = None, + ) -> AnswerShippingQuery: + """ + :param ok: + :param shipping_options: + :param error_message: + :return: + """ + from ..methods import AnswerShippingQuery + + return AnswerShippingQuery( + shipping_query_id=self.id, + ok=ok, + shipping_options=shipping_options, + error_message=error_message, + ) diff --git a/docs/api/methods/answer_shipping_query.md b/docs/api/methods/answer_shipping_query.md index 6ac4948b..40437c42 100644 --- a/docs/api/methods/answer_shipping_query.md +++ b/docs/api/methods/answer_shipping_query.md @@ -58,3 +58,5 @@ return AnswerShippingQuery(...) - [Official documentation](https://core.telegram.org/bots/api#answershippingquery) - [aiogram.types.ShippingOption](../types/shipping_option.md) +- [aiogram.types.ShippingQuery](../types/shipping_query.md) +- [Aliases](../types/shipping_query.md#aliases) diff --git a/docs/api/types/shipping_query.md b/docs/api/types/shipping_query.md index 5b65a13d..95673673 100644 --- a/docs/api/types/shipping_query.md +++ b/docs/api/types/shipping_query.md @@ -22,8 +22,22 @@ This object contains information about an incoming shipping query. - `from aiogram.api.types import ShippingQuery` - `from aiogram.api.types.shipping_query import ShippingQuery` +## Aliases + +Aliases is always returns related API method (Awaitable) and can be used directly or as answer's into webhook. + +### Answer + +This method has the same specification with the API but without `callback_query_id` argument. + +| Answer method | Alias for | Description | +| - | - | - | +| `answer` | [Bot.answer_shipping_query](../methods/answer_shipping_query.md) | Answer to shipping query | + + ## Related pages: - [Official documentation](https://core.telegram.org/bots/api#shippingquery) - [aiogram.types.ShippingAddress](../types/shipping_address.md) - [aiogram.types.User](../types/user.md) +- [aiogram.methods.AnswerShippingQuery](../methods/answer_shipping_query.md) diff --git a/tests/test_api/test_types/test_shipping_query.py b/tests/test_api/test_types/test_shipping_query.py new file mode 100644 index 00000000..0d1eda8a --- /dev/null +++ b/tests/test_api/test_types/test_shipping_query.py @@ -0,0 +1,24 @@ +from aiogram.api.methods import AnswerShippingQuery +from aiogram.api.types import ShippingAddress, ShippingQuery, User + + +class TestInlineQuery: + def test_answer_alias(self): + shipping_query = ShippingQuery( + id="id", + from_user=User(id=42, is_bot=False, first_name="name"), + invoice_payload="payload", + shipping_address=ShippingAddress( + country_code="foo", + state="foo", + city="foo", + street_line1="foo", + street_line2="foo", + post_code="foo", + ), + ) + + api_method = shipping_query.answer(True) + + assert isinstance(api_method, AnswerShippingQuery) + assert api_method.shipping_query_id == shipping_query.id From c5e0742337f62402a2465e83b25ae6b0d54ccd7f Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 16:58:15 +0500 Subject: [PATCH 05/10] Improving test_shipping_query --- tests/test_api/test_types/test_shipping_query.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_api/test_types/test_shipping_query.py b/tests/test_api/test_types/test_shipping_query.py index 0d1eda8a..822e6b83 100644 --- a/tests/test_api/test_types/test_shipping_query.py +++ b/tests/test_api/test_types/test_shipping_query.py @@ -1,5 +1,5 @@ from aiogram.api.methods import AnswerShippingQuery -from aiogram.api.types import ShippingAddress, ShippingQuery, User +from aiogram.api.types import ShippingAddress, ShippingQuery, User, ShippingOption, LabeledPrice class TestInlineQuery: @@ -18,7 +18,14 @@ class TestInlineQuery: ), ) - api_method = shipping_query.answer(True) + shipping_options = [ + ShippingOption(id="id", title="foo", prices=[LabeledPrice(label="foo", amount=123)])] + + kwargs = dict(ok=True, shipping_options=shipping_options, error_message="foo") + + api_method = shipping_query.answer(**kwargs) assert isinstance(api_method, AnswerShippingQuery) - assert api_method.shipping_query_id == shipping_query.id + + for key, value in kwargs.items(): + assert getattr(api_method, key) == value From 56df3f0ba8a3a939830f6b4caee472e146ee2c2a Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 17:00:25 +0500 Subject: [PATCH 06/10] Update test_shipping_query.py --- tests/test_api/test_types/test_shipping_query.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_api/test_types/test_shipping_query.py b/tests/test_api/test_types/test_shipping_query.py index 822e6b83..94e60640 100644 --- a/tests/test_api/test_types/test_shipping_query.py +++ b/tests/test_api/test_types/test_shipping_query.py @@ -26,6 +26,7 @@ class TestInlineQuery: api_method = shipping_query.answer(**kwargs) assert isinstance(api_method, AnswerShippingQuery) + assert api_method.shipping_query_id == shipping_query.id for key, value in kwargs.items(): assert getattr(api_method, key) == value From 66ecc6996dd7bf5fa7561ee1ff9d2985bf4422e3 Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 17:01:06 +0500 Subject: [PATCH 07/10] Improving test_callback_query --- tests/test_api/test_types/test_callback_query.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_api/test_types/test_callback_query.py b/tests/test_api/test_types/test_callback_query.py index f76c9be9..792cca68 100644 --- a/tests/test_api/test_types/test_callback_query.py +++ b/tests/test_api/test_types/test_callback_query.py @@ -8,7 +8,12 @@ class TestCallbackQuery: id="id", from_user=User(id=42, is_bot=False, first_name="name"), chat_instance="chat" ) - api_method = callback_query.answer() + kwargs = dict(text="foo", show_alert=True, url="https://foo.bar/", cache_time=123) + + api_method = callback_query.answer(**kwargs) assert isinstance(api_method, AnswerCallbackQuery) assert api_method.callback_query_id == callback_query.id + + for key, value in kwargs.items(): + assert getattr(api_method, key) == value From a6f83599c8c5ce9adf0ab550b44a29fe06be68d6 Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 17:03:35 +0500 Subject: [PATCH 08/10] Improving test_inline_query --- tests/test_api/test_types/test_inline_query.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_api/test_types/test_inline_query.py b/tests/test_api/test_types/test_inline_query.py index 24657b74..2ef76685 100644 --- a/tests/test_api/test_types/test_inline_query.py +++ b/tests/test_api/test_types/test_inline_query.py @@ -11,7 +11,12 @@ class TestInlineQuery: offset="", ) - api_method = inline_query.answer([]) + kwargs = dict(results=[], cache_time=123, next_offset="123", switch_pm_text="foo", switch_pm_parameter="foo") + + api_method = inline_query.answer(**kwargs) assert isinstance(api_method, AnswerInlineQuery) assert api_method.inline_query_id == inline_query.id + + for key, value in kwargs.items(): + assert getattr(api_method, key) == value From b04a14072ec2a94e8d82299769ac2639735ef1d1 Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 17:05:30 +0500 Subject: [PATCH 09/10] Improving test_pre_checkout_query --- tests/test_api/test_types/test_pre_checkout_query.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_api/test_types/test_pre_checkout_query.py b/tests/test_api/test_types/test_pre_checkout_query.py index 0e79cc0f..1bef6cff 100644 --- a/tests/test_api/test_types/test_pre_checkout_query.py +++ b/tests/test_api/test_types/test_pre_checkout_query.py @@ -12,7 +12,12 @@ class TestPreCheckoutQuery: invoice_payload="payload", ) - api_method = pre_checkout_query.answer(True) + kwargs = dict(ok=True, error_message="foo") + + api_method = pre_checkout_query.answer(**kwargs) assert isinstance(api_method, AnswerPreCheckoutQuery) assert api_method.pre_checkout_query_id == pre_checkout_query.id + + for key, value in kwargs.items(): + assert getattr(api_method, key) == value From 3029614dd1b51b75da03f899f358ed83597aa93d Mon Sep 17 00:00:00 2001 From: Gabben <43146729+gabbhack@users.noreply.github.com> Date: Sun, 17 May 2020 17:10:42 +0500 Subject: [PATCH 10/10] Typo fix --- docs/api/types/shipping_query.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/types/shipping_query.md b/docs/api/types/shipping_query.md index 95673673..74615d37 100644 --- a/docs/api/types/shipping_query.md +++ b/docs/api/types/shipping_query.md @@ -28,7 +28,7 @@ Aliases is always returns related API method (Awaitable) and can be used directl ### Answer -This method has the same specification with the API but without `callback_query_id` argument. +This method has the same specification with the API but without `shipping_query_id` argument. | Answer method | Alias for | Description | | - | - | - |