diff --git a/pyrogram/client/chat_action.py b/pyrogram/client/chat_action.py index 95bffb8e..2296bcac 100644 --- a/pyrogram/client/chat_action.py +++ b/pyrogram/client/chat_action.py @@ -16,12 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from enum import Enum + from pyrogram.api import types -class ChatAction: - """This class provides a convenient access to all Chat Actions available. - Chat Actions are intended to be used with :meth:`send_chat_action() `. +class ChatAction(Enum): + """This enumeration provides a convenient access to all Chat Actions available. + Chat Actions are intended to be used with + :meth:`send_chat_action() `. """ CANCEL = types.SendMessageCancelAction @@ -62,3 +65,13 @@ class ChatAction: UPLOAD_VIDEO_NOTE = types.SendMessageUploadRoundAction """User is uploading a round video note.""" + + @classmethod + def from_string(cls, action: str) -> "ChatAction": + for a in iter(ChatAction): + if a.name.lower() == action.lower(): + return a + + raise ValueError("Invalid ChatAction: '{}'. Possible types are {}".format( + action, [x.name.lower() for x in iter(ChatAction)] + )) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 19121222..bbb476f9 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -51,7 +51,7 @@ from pyrogram.crypto import AES from pyrogram.session import Auth, Session from pyrogram.session.internals import MsgId from . import types as pyrogram_types -from . import utils +from . import utils, ChatAction from .dispatcher import Dispatcher from .style import Markdown, HTML from .syncer import Syncer @@ -2596,7 +2596,7 @@ class Client: def send_chat_action(self, chat_id: int or str, - action: callable, + action: ChatAction or str, progress: int = 0): """Use this method when you need to tell the other party that something is happening on your side. @@ -2607,10 +2607,11 @@ class Client: For a contact that exists in your Telegram address book you can use his phone number (str). For a private channel/supergroup you can use its *t.me/joinchat/* link. - action (``callable``): + action (``ChatAction`` | ``str``): Type of action to broadcast. - Choose one from the :class:`ChatAction ` class, + Choose one from the :class:`ChatAction ` enumeration, depending on what the user is about to receive. + You can also provide a string (e.g. "typing", "upload_photo", "record_audio", ...). progress (``int``, *optional*): Progress of the upload process. @@ -2620,7 +2621,15 @@ class Client: Raises: :class:`Error ` + :class:`ValueError`: If the provided string is not a valid ChatAction """ + + # Resolve Enum type + if isinstance(action, str): + action = ChatAction.from_string(action).value + elif isinstance(action, ChatAction): + action = action.value + if "Upload" in action.__name__: action = action(progress) else: