From 72e95fd322f4efc525e39913a1c04e6737c1b0ab Mon Sep 17 00:00:00 2001 From: JosXa Date: Mon, 30 Apr 2018 16:03:18 +0200 Subject: [PATCH 1/7] Made ChatAction an Enum --- pyrogram/client/chat_action.py | 5 +++-- pyrogram/client/client.py | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/chat_action.py b/pyrogram/client/chat_action.py index 95bffb8e..86661f00 100644 --- a/pyrogram/client/chat_action.py +++ b/pyrogram/client/chat_action.py @@ -15,12 +15,13 @@ # # 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. +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() `. """ diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 31bb0e01..e1f218ca 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -38,6 +38,7 @@ from signal import signal, SIGINT, SIGTERM, SIGABRT from threading import Event, Thread import pyrogram +from pyrogram import ChatAction from pyrogram.api import functions, types from pyrogram.api.core import Object from pyrogram.api.errors import ( @@ -2502,7 +2503,7 @@ class Client: def send_chat_action(self, chat_id: int or str, - action: callable, + action: ChatAction, progress: int = 0): """Use this method when you need to tell the other party that something is happening on your side. @@ -2527,6 +2528,9 @@ class Client: Raises: :class:`Error ` """ + # Resolve Enum type + action = action.value if isinstance(action, ChatAction) else action + if "Upload" in action.__name__: action = action(progress) else: From e2d80a6087893f9cb54f98429e02c3fa4ffe0836 Mon Sep 17 00:00:00 2001 From: JosXa Date: Mon, 30 Apr 2018 16:11:50 +0200 Subject: [PATCH 2/7] Made ChatAction an Enum --- pyrogram/client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index e1f218ca..e0f0b4e7 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -38,9 +38,9 @@ from signal import signal, SIGINT, SIGTERM, SIGABRT from threading import Event, Thread import pyrogram -from pyrogram import ChatAction from pyrogram.api import functions, types from pyrogram.api.core import Object +from pyrogram.client import ChatAction from pyrogram.api.errors import ( PhoneMigrate, NetworkMigrate, PhoneNumberInvalid, PhoneNumberUnoccupied, PhoneCodeInvalid, PhoneCodeHashEmpty, From abf89688eda140b6b5998965118fd3ab3ab4ab5c Mon Sep 17 00:00:00 2001 From: JosXa Date: Mon, 30 Apr 2018 16:28:43 +0200 Subject: [PATCH 3/7] 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) From 5b492bae9de427b921a10e8e74fa8aed77282e26 Mon Sep 17 00:00:00 2001 From: JosXa Date: Mon, 30 Apr 2018 16:32:21 +0200 Subject: [PATCH 4/7] Docstring --- pyrogram/client/client.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index b40d9881..e1a4c9a4 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -2514,10 +2514,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"). progress (``int``, optional): Progress of the upload process. @@ -2527,6 +2528,7 @@ class Client: Raises: :class:`Error ` + :class:`ValueError`: If the provided string is not a valid ChatAction """ # Resolve Enum type From 33dd70883192b60f663bff83ff0c7ee4fcf39044 Mon Sep 17 00:00:00 2001 From: JosXa Date: Mon, 30 Apr 2018 16:36:18 +0200 Subject: [PATCH 5/7] enumeration "class" --- pyrogram/client/chat_action.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/client/chat_action.py b/pyrogram/client/chat_action.py index 8cb677e7..020b0424 100644 --- a/pyrogram/client/chat_action.py +++ b/pyrogram/client/chat_action.py @@ -21,7 +21,7 @@ from pyrogram.api import types class ChatAction(Enum): - """This enumeration class provides a convenient access to all Chat Actions available. + """This enumeration provides a convenient access to all Chat Actions available. Chat Actions are intended to be used with :meth:`send_chat_action() `. """ From 9b9c7ced7725dde3d4d24e396bcbda9057e9f156 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 30 Apr 2018 19:34:59 +0200 Subject: [PATCH 6/7] Update chat_action.py --- pyrogram/client/chat_action.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/chat_action.py b/pyrogram/client/chat_action.py index 020b0424..2296bcac 100644 --- a/pyrogram/client/chat_action.py +++ b/pyrogram/client/chat_action.py @@ -15,6 +15,7 @@ # # 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 @@ -66,10 +67,11 @@ class ChatAction(Enum): """User is uploading a round video note.""" @classmethod - def from_string(cls, action: str) -> 'ChatAction': + 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)] )) From fcb4c6d07be6a8e212fbd022756af7a6a28a1dc8 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 30 Apr 2018 19:37:59 +0200 Subject: [PATCH 7/7] Update client.py --- pyrogram/client/client.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index e1a4c9a4..403f08c8 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -40,7 +40,6 @@ from threading import Event, Thread import pyrogram from pyrogram.api import functions, types from pyrogram.api.core import Object -from pyrogram.client import ChatAction from pyrogram.api.errors import ( PhoneMigrate, NetworkMigrate, PhoneNumberInvalid, PhoneNumberUnoccupied, PhoneCodeInvalid, PhoneCodeHashEmpty, @@ -52,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 @@ -2518,7 +2517,7 @@ class Client: Type of action to broadcast. 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"). + You can also provide a string (e.g. "typing", "upload_photo", "record_audio", ...). progress (``int``, optional): Progress of the upload process.