From abf89688eda140b6b5998965118fd3ab3ab4ab5c Mon Sep 17 00:00:00 2001 From: JosXa Date: Mon, 30 Apr 2018 16:28:43 +0200 Subject: [PATCH] Also allow strings for send_chat_action --- pyrogram/client/chat_action.py | 14 ++++++++++++-- pyrogram/client/client.py | 8 ++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pyrogram/client/chat_action.py b/pyrogram/client/chat_action.py index 86661f00..8cb677e7 100644 --- a/pyrogram/client/chat_action.py +++ b/pyrogram/client/chat_action.py @@ -21,8 +21,9 @@ from pyrogram.api import types 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() `. + """This enumeration class provides a convenient access to all Chat Actions available. + Chat Actions are intended to be used with + :meth:`send_chat_action() `. """ CANCEL = types.SendMessageCancelAction @@ -63,3 +64,12 @@ class ChatAction(Enum): 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 e0f0b4e7..b40d9881 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -2503,7 +2503,7 @@ class Client: def send_chat_action(self, chat_id: int or str, - action: ChatAction, + 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. @@ -2528,8 +2528,12 @@ class Client: Raises: :class:`Error ` """ + # Resolve Enum type - action = action.value if isinstance(action, ChatAction) else action + 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)