From 6530c7e293b6f088d2db14ef3e2443bd79b6922b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 9 May 2019 04:58:55 +0200 Subject: [PATCH] Remove ChatAction module too It's pretty much useless, better just use strings --- docs/source/pyrogram/ChatAction.rst | 5 -- docs/source/pyrogram/index.rst | 1 - pyrogram/client/__init__.py | 4 +- pyrogram/client/ext/__init__.py | 1 - pyrogram/client/ext/chat_action.py | 77 ------------------- .../methods/messages/send_chat_action.py | 59 ++++++++------ .../types/messages_and_media/message.py | 30 +++----- 7 files changed, 49 insertions(+), 128 deletions(-) delete mode 100644 docs/source/pyrogram/ChatAction.rst delete mode 100644 pyrogram/client/ext/chat_action.py diff --git a/docs/source/pyrogram/ChatAction.rst b/docs/source/pyrogram/ChatAction.rst deleted file mode 100644 index dfa56945..00000000 --- a/docs/source/pyrogram/ChatAction.rst +++ /dev/null @@ -1,5 +0,0 @@ -ChatAction -========== - -.. autoclass:: pyrogram.ChatAction - :members: diff --git a/docs/source/pyrogram/index.rst b/docs/source/pyrogram/index.rst index 84471e89..5546070d 100644 --- a/docs/source/pyrogram/index.rst +++ b/docs/source/pyrogram/index.rst @@ -14,7 +14,6 @@ after the well established Telegram Bot API methods, thus offering a familiar lo Handlers Decorators Filters - ChatAction Errors diff --git a/pyrogram/client/__init__.py b/pyrogram/client/__init__.py index 584449c7..f4a954c6 100644 --- a/pyrogram/client/__init__.py +++ b/pyrogram/client/__init__.py @@ -17,9 +17,9 @@ # along with Pyrogram. If not, see . from .client import Client -from .ext import BaseClient, ChatAction, Emoji +from .ext import BaseClient, Emoji from .filters import Filters __all__ = [ - "Client", "BaseClient", "ChatAction", "Emoji", "Filters", + "Client", "BaseClient", "Emoji", "Filters", ] diff --git a/pyrogram/client/ext/__init__.py b/pyrogram/client/ext/__init__.py index a8e7c794..917c9e62 100644 --- a/pyrogram/client/ext/__init__.py +++ b/pyrogram/client/ext/__init__.py @@ -17,7 +17,6 @@ # along with Pyrogram. If not, see . from .base_client import BaseClient -from .chat_action import ChatAction from .dispatcher import Dispatcher from .emoji import Emoji from .syncer import Syncer diff --git a/pyrogram/client/ext/chat_action.py b/pyrogram/client/ext/chat_action.py deleted file mode 100644 index c0ee0585..00000000 --- a/pyrogram/client/ext/chat_action.py +++ /dev/null @@ -1,77 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2019 Dan Tès -# -# This file is part of Pyrogram. -# -# Pyrogram is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pyrogram is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# 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(Enum): - """This enumeration provides a convenient access to all Chat Actions available. - Chat Actions are intended to be used with - :meth:`send_chat_action() `. - """ - - CANCEL = types.SendMessageCancelAction - """Cancels any chat action currently displayed.""" - - TYPING = types.SendMessageTypingAction - """User is typing a text message.""" - - PLAYING = types.SendMessageGamePlayAction - """User is playing a game.""" - - CHOOSE_CONTACT = types.SendMessageChooseContactAction - """User is choosing a contact to share.""" - - UPLOAD_PHOTO = types.SendMessageUploadPhotoAction - """User is uploading a photo.""" - - RECORD_VIDEO = types.SendMessageRecordVideoAction - """User is recording a video.""" - - UPLOAD_VIDEO = types.SendMessageUploadVideoAction - """User is uploading a video.""" - - RECORD_AUDIO = types.SendMessageRecordAudioAction - """User is recording an audio message.""" - - UPLOAD_AUDIO = types.SendMessageUploadAudioAction - """User is uploading an audio message.""" - - UPLOAD_DOCUMENT = types.SendMessageUploadDocumentAction - """User is uploading a generic document.""" - - FIND_LOCATION = types.SendMessageGeoLocationAction - """User is searching for a location on the map.""" - - RECORD_VIDEO_NOTE = types.SendMessageRecordRoundAction - """User is recording a round video note.""" - - UPLOAD_VIDEO_NOTE = types.SendMessageUploadRoundAction - """User is uploading a round video note.""" - - @staticmethod - def from_string(action: str) -> "ChatAction": - for a in ChatAction: - if a.name.lower() == action.lower(): - return a - - raise ValueError("Invalid ChatAction: '{}'. Possible types are {}".format( - action, [x.name.lower() for x in ChatAction] - )) diff --git a/pyrogram/client/methods/messages/send_chat_action.py b/pyrogram/client/methods/messages/send_chat_action.py index 5c50c2cf..1d819747 100644 --- a/pyrogram/client/methods/messages/send_chat_action.py +++ b/pyrogram/client/methods/messages/send_chat_action.py @@ -18,17 +18,32 @@ from typing import Union -from pyrogram.api import functions -from pyrogram.client.ext import BaseClient, ChatAction +from pyrogram.api import functions, types +from pyrogram.client.ext import BaseClient +import json + + +class ChatAction: + TYPING = types.SendMessageTypingAction + UPLOAD_PHOTO = types.SendMessageUploadPhotoAction + RECORD_VIDEO = types.SendMessageRecordVideoAction + UPLOAD_VIDEO = types.SendMessageUploadVideoAction + RECORD_AUDIO = types.SendMessageRecordAudioAction + UPLOAD_AUDIO = types.SendMessageUploadAudioAction + UPLOAD_DOCUMENT = types.SendMessageUploadDocumentAction + FIND_LOCATION = types.SendMessageGeoLocationAction + RECORD_VIDEO_NOTE = types.SendMessageRecordRoundAction + UPLOAD_VIDEO_NOTE = types.SendMessageUploadRoundAction + PLAYING = types.SendMessageGamePlayAction + CHOOSE_CONTACT = types.SendMessageChooseContactAction + CANCEL = types.SendMessageCancelAction + + +POSSIBLE_VALUES = list(map(lambda x: x.lower(), filter(lambda x: not x.startswith("__"), ChatAction.__dict__.keys()))) class SendChatAction(BaseClient): - def send_chat_action( - self, - chat_id: Union[int, str], - action: Union[ChatAction, str], - progress: int = 0 - ): + def send_chat_action(self, chat_id: Union[int, str], action: str) -> bool: """Use this method when you need to tell the other party that something is happening on your side. Parameters: @@ -37,15 +52,13 @@ class SendChatAction(BaseClient): For your personal cloud (Saved Messages) you can simply use "me" or "self". For a contact that exists in your Telegram address book you can use his phone number (str). - action (:obj:`ChatAction` | ``str``): - 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", "upload_photo", "record_audio", ...). - - progress (``int``, *optional*): - Progress of the upload process. - Currently useless because official clients don't seem to be handling this. + action (``str``): + Type of action to broadcast. Choose one, depending on what the user is about to receive: *"typing"* for + text messages, *"upload_photo"* for photos, *"record_video"* or *"upload_video"* for videos, + *"record_audio"* or *"upload_audio"* for audio files, *"upload_document"* for general files, + *"find_location"* for location data, *"record_video_note"* or *"upload_video_note"* for video notes, + *"choose_contact"* for contacts, *"playing"* for games or *"cancel"* to cancel any chat action currently + displayed. Returns: ``bool``: On success, True is returned. @@ -55,14 +68,14 @@ class SendChatAction(BaseClient): ValueError: In case 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 + try: + action = ChatAction.__dict__[action.upper()] + except KeyError: + raise ValueError("Invalid chat action '{}'. Possible values are: {}".format( + action, json.dumps(POSSIBLE_VALUES, indent=4))) from None if "Upload" in action.__name__: - action = action(progress=progress) + action = action(progress=0) else: action = action() diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 648177d4..05ff94f3 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -22,7 +22,6 @@ from typing import List, Match, Union import pyrogram from pyrogram.api import types from pyrogram.errors import MessageIdsEmpty -from pyrogram.client.ext import ChatAction from pyrogram.client.types.input_media import InputMedia from .contact import Contact from .location import Location @@ -1077,11 +1076,7 @@ class Message(PyrogramType, Update): reply_markup=reply_markup ) - def reply_chat_action( - self, - action: Union[ChatAction, str], - progress: int = 0 - ) -> "Message": + def reply_chat_action(self, action: str) -> bool: """Bound method *reply_chat_action* of :obj:`Message`. Use as a shortcut for: @@ -1099,27 +1094,24 @@ class Message(PyrogramType, Update): message.reply_chat_action("typing") Parameters: - action (:obj:`ChatAction ` | ``str``): - 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", "upload_photo", "record_audio", ...). - - progress (``int``, *optional*): - Progress of the upload process. - Currently useless because official clients don't seem to be handling this. + action (``str``): + Type of action to broadcast. Choose one, depending on what the user is about to receive: *"typing"* for + text messages, *"upload_photo"* for photos, *"record_video"* or *"upload_video"* for videos, + *"record_audio"* or *"upload_audio"* for audio files, *"upload_document"* for general files, + *"find_location"* for location data, *"record_video_note"* or *"upload_video_note"* for video notes, + *"choose_contact"* for contacts, *"playing"* for games or *"cancel"* to cancel any chat action currently + displayed. Returns: - On success, True is returned. + ``bool``: On success, True is returned. Raises: RPCError: In case of a Telegram RPC error. - ``ValueError`` if the provided string is not a valid ChatAction. + ValueError: In case the provided string is not a valid chat action. """ return self._client.send_chat_action( chat_id=self.chat.id, - action=action, - progress=progress + action=action ) def reply_contact(