Merge pull request #333 from gabbhack/dev-3.x-aliases

Answer aliases
This commit is contained in:
Alex Root Junior 2020-05-24 21:03:28 +03:00 committed by GitHub
commit c262cc0ce6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 257 additions and 2 deletions

View file

@ -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,
)

View file

@ -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,
)

View file

@ -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,
)

View file

@ -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,
)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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 `shipping_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)

View file

@ -0,0 +1,19 @@
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"
)
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

View file

@ -0,0 +1,22 @@
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="",
)
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

View file

@ -0,0 +1,23 @@
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",
)
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

View file

@ -0,0 +1,32 @@
from aiogram.api.methods import AnswerShippingQuery
from aiogram.api.types import ShippingAddress, ShippingQuery, User, ShippingOption, LabeledPrice
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",
),
)
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