2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Also allow strings for send_chat_action

This commit is contained in:
JosXa 2018-04-30 16:28:43 +02:00
parent e2d80a6087
commit abf89688ed
2 changed files with 18 additions and 4 deletions

View File

@ -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() <pyrogram.Client.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() <pyrogram.Client.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)]
))

View File

@ -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 <pyrogram.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)