From e7c44933c20e586e03a49e553276d4275d03cb4e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 12 Dec 2017 14:51:02 +0100 Subject: [PATCH] Move chat actions to a separate class --- pyrogram/__init__.py | 1 + pyrogram/client/__init__.py | 1 + pyrogram/client/chat_action.py | 35 ++++++++++++++++++++++++++++++++++ pyrogram/client/client.py | 23 ++-------------------- 4 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 pyrogram/client/chat_action.py diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index 5cdec741..462633bd 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -20,4 +20,5 @@ __copyright__ = "Copyright (C) 2017 Dan Tès " __license__ = "GNU Lesser General Public License v3 or later (LGPLv3+)" __version__ = "0.1.2" +from .client import ChatAction from .client import Client diff --git a/pyrogram/client/__init__.py b/pyrogram/client/__init__.py index 9828292b..f1dc05b9 100644 --- a/pyrogram/client/__init__.py +++ b/pyrogram/client/__init__.py @@ -16,4 +16,5 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from .chat_action import ChatAction from .client import Client diff --git a/pyrogram/client/chat_action.py b/pyrogram/client/chat_action.py new file mode 100644 index 00000000..902cd996 --- /dev/null +++ b/pyrogram/client/chat_action.py @@ -0,0 +1,35 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 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 pyrogram.api import types + + +class ChatAction: + CANCEL = types.SendMessageCancelAction + TYPING = types.SendMessageTypingAction + PLAYING = types.SendMessageGamePlayAction + CHOOSE_CONTACT = types.SendMessageChooseContactAction + 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 diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 81693d70..8367b9db 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -49,22 +49,6 @@ Config = namedtuple("Config", ["api_id", "api_hash"]) class Client: DIALOGS_AT_ONCE = 100 - CHAT_ACTIONS = { - "cancel": types.SendMessageCancelAction, - "typing": types.SendMessageTypingAction, - "playing": types.SendMessageGamePlayAction, - "choose_contact": types.SendMessageChooseContactAction, - "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, - } - def __init__(self, session_name: str, test_mode: bool = False): self.session_name = session_name self.test_mode = test_mode @@ -477,15 +461,12 @@ class Client: def send_chat_action(self, chat_id: int or str, - action: str, + action: callable, progress: int = 0): return self.send( functions.messages.SetTyping( peer=self.resolve_peer(chat_id), - action=self.CHAT_ACTIONS.get( - action.lower(), - types.SendMessageTypingAction - )(progress=progress) + action=action(progress=progress) ) )