2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Merge pull request #67 from JosXa/chataction-enum

Make ChatAction an Enum
This commit is contained in:
Dan 2018-04-30 19:39:33 +02:00 committed by GitHub
commit a32e496fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 7 deletions

View File

@ -16,12 +16,15 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from enum import Enum
from pyrogram.api import types from pyrogram.api import types
class ChatAction: class ChatAction(Enum):
"""This 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() <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
@ -62,3 +65,13 @@ class ChatAction:
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

@ -51,7 +51,7 @@ from pyrogram.crypto import AES
from pyrogram.session import Auth, Session from pyrogram.session import Auth, Session
from pyrogram.session.internals import MsgId from pyrogram.session.internals import MsgId
from . import types as pyrogram_types from . import types as pyrogram_types
from . import utils from . import utils, ChatAction
from .dispatcher import Dispatcher from .dispatcher import Dispatcher
from .style import Markdown, HTML from .style import Markdown, HTML
from .syncer import Syncer from .syncer import Syncer
@ -2596,7 +2596,7 @@ class Client:
def send_chat_action(self, def send_chat_action(self,
chat_id: int or str, chat_id: int or str,
action: callable, 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.
@ -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 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. For a private channel/supergroup you can use its *t.me/joinchat/* link.
action (``callable``): action (``ChatAction`` | ``str``):
Type of action to broadcast. Type of action to broadcast.
Choose one from the :class:`ChatAction <pyrogram.ChatAction>` class, Choose one from the :class:`ChatAction <pyrogram.ChatAction>` enumeration,
depending on what the user is about to receive. 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 (``int``, *optional*):
Progress of the upload process. Progress of the upload process.
@ -2620,7 +2621,15 @@ class Client:
Raises: Raises:
:class:`Error <pyrogram.Error>` :class:`Error <pyrogram.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__: if "Upload" in action.__name__:
action = action(progress) action = action(progress)
else: else: