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] 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