From 851f7a2a37987fe8af0adfa9056b5063dafde7de Mon Sep 17 00:00:00 2001 From: ShiroNoHaga <95978403+ShiroNoHaga@users.noreply.github.com> Date: Sat, 9 Jul 2022 23:47:11 +0300 Subject: [PATCH] Add class helper ChatAction (#803) * Add class helper ChatAction * Change using helper to using enum.Enum * Add test for class ChatAction * Use black formatting * Add pull request description to CHANGES * Add test coverage * Use AutoName class for enum values * Move `AutoName` to separate file * Move inheritance from `str` * Fix failing mypy * Delete old actions Co-authored-by: Evgen Fil --- CHANGES/803.feature | 3 ++ aiogram/types/__init__.py | 2 ++ aiogram/types/chat_action.py | 33 +++++++++++++++++++ aiogram/utils/enum.py | 8 +++++ .../test_methods/test_send_chat_action.py | 10 ++++++ 5 files changed, 56 insertions(+) create mode 100644 CHANGES/803.feature create mode 100644 aiogram/types/chat_action.py create mode 100644 aiogram/utils/enum.py diff --git a/CHANGES/803.feature b/CHANGES/803.feature new file mode 100644 index 00000000..59ee6725 --- /dev/null +++ b/CHANGES/803.feature @@ -0,0 +1,3 @@ +Add class helper ChatAction for constants that Telegram BotAPI uses in sendChatAction request. +In my opinion, this will help users and will also improve compatibility with 2.x version +where similar class was called "ChatActions". diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index ec04b855..e4a1819b 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -13,6 +13,7 @@ from .bot_command_scope_default import BotCommandScopeDefault from .callback_game import CallbackGame from .callback_query import CallbackQuery from .chat import Chat +from .chat_action import ChatAction from .chat_administrator_rights import ChatAdministratorRights from .chat_invite_link import ChatInviteLink from .chat_join_request import ChatJoinRequest @@ -275,6 +276,7 @@ __all__ = ( "Game", "CallbackGame", "GameHighScore", + "ChatAction", ) # Load typing forward refs for every TelegramObject diff --git a/aiogram/types/chat_action.py b/aiogram/types/chat_action.py new file mode 100644 index 00000000..619a0dc5 --- /dev/null +++ b/aiogram/types/chat_action.py @@ -0,0 +1,33 @@ +import enum + +from ..utils.enum import AutoName + + +class ChatAction(AutoName): + """ + This object represents bot actions. + + Choose one, depending on what the user is about to receive: + • typing for text messages, + • upload_photo for photos, + • record_video or upload_video for videos, + • record_voice or upload_voice for voice notes, + • upload_document for general files, + • choose_sticker for stickers, + • find_location for location data, + • record_video_note or upload_video_note for video notes. + + Source: https://core.telegram.org/bots/api#sendchataction + """ + + TYPING = enum.auto() + UPLOAD_PHOTO = enum.auto() + RECORD_VIDEO = enum.auto() + UPLOAD_VIDEO = enum.auto() + RECORD_VOICE = enum.auto() + UPLOAD_VOICE = enum.auto() + UPLOAD_DOCUMENT = enum.auto() + CHOOSE_STICKER = enum.auto() + FIND_LOCATION = enum.auto() + RECORD_VIDEO_NOTE = enum.auto() + UPLOAD_VIDEO_NOTE = enum.auto() diff --git a/aiogram/utils/enum.py b/aiogram/utils/enum.py new file mode 100644 index 00000000..b239ebc0 --- /dev/null +++ b/aiogram/utils/enum.py @@ -0,0 +1,8 @@ +import enum +from typing import Any, List + + +class AutoName(str, enum.Enum): + @staticmethod + def _generate_next_value_(name: str, start: int, count: int, last_values: List[Any]) -> str: + return name.lower() diff --git a/tests/test_api/test_methods/test_send_chat_action.py b/tests/test_api/test_methods/test_send_chat_action.py index 6b6454ae..98c9d016 100644 --- a/tests/test_api/test_methods/test_send_chat_action.py +++ b/tests/test_api/test_methods/test_send_chat_action.py @@ -1,6 +1,7 @@ import pytest from aiogram.methods import Request, SendChatAction +from aiogram.types import ChatAction from tests.mocked_bot import MockedBot pytestmark = pytest.mark.asyncio @@ -22,3 +23,12 @@ class TestSendChatAction: request: Request = bot.get_request() assert request.method == "sendChatAction" assert response == prepare_result.result + + async def test_chat_action_class(self, bot: MockedBot): + prepare_result = bot.add_result_for(SendChatAction, ok=True, result=True) + + response: bool = await bot.send_chat_action(chat_id=42, action=ChatAction.TYPING) + request: Request = bot.get_request() + assert request.method == "sendChatAction" + assert request.data["action"] == "typing" + assert response == prepare_result.result