2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +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): class ChatAction(Enum):
"""This enumeration provides a convenient access to all Chat Actions available. """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>`. Chat Actions are intended to be used with
:meth:`send_chat_action() <pyrogram.Client.send_chat_action>`.
""" """
CANCEL = types.SendMessageCancelAction CANCEL = types.SendMessageCancelAction
@ -63,3 +64,12 @@ class ChatAction(Enum):
UPLOAD_VIDEO_NOTE = types.SendMessageUploadRoundAction UPLOAD_VIDEO_NOTE = types.SendMessageUploadRoundAction
"""User is uploading a round video note.""" """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, def send_chat_action(self,
chat_id: int or str, chat_id: int or str,
action: ChatAction, action: ChatAction or str,
progress: int = 0): progress: int = 0):
"""Use this method when you need to tell the other party that something is happening on your side. """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: Raises:
:class:`Error <pyrogram.Error>` :class:`Error <pyrogram.Error>`
""" """
# Resolve Enum type # 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__: if "Upload" in action.__name__:
action = action(progress) action = action(progress)