Add CallbackQuery.answer

This commit is contained in:
Gabben 2020-05-17 15:42:04 +05:00
parent 8e54cce58e
commit 6bba2da814
4 changed files with 56 additions and 0 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

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

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

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