2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
|
|
|
# Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
|
2018-05-07 12:30:55 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# This file is part of Pyrogram.
|
2018-05-07 12:30:55 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# 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.
|
2018-05-07 12:30:55 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# 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.
|
2018-05-07 12:30:55 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-07 12:30:55 +00:00
|
|
|
|
2019-05-30 12:57:52 +00:00
|
|
|
import json
|
2018-12-19 13:50:23 +00:00
|
|
|
from typing import Union
|
|
|
|
|
2019-05-09 02:58:55 +00:00
|
|
|
from pyrogram.api import functions, types
|
|
|
|
from pyrogram.client.ext import BaseClient
|
|
|
|
|
|
|
|
|
|
|
|
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())))
|
2018-05-07 12:30:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SendChatAction(BaseClient):
|
2019-05-25 00:02:37 +00:00
|
|
|
async def send_chat_action(self, chat_id: Union[int, str], action: str) -> bool:
|
2019-05-12 17:49:06 +00:00
|
|
|
"""Tell the other party that something is happening on your side.
|
2018-05-07 12:30:55 +00:00
|
|
|
|
2019-05-09 02:28:46 +00:00
|
|
|
Parameters:
|
2018-05-07 12:30:55 +00:00
|
|
|
chat_id (``int`` | ``str``):
|
|
|
|
Unique identifier (int) or username (str) of the target chat.
|
|
|
|
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).
|
|
|
|
|
2019-05-09 02:58:55 +00:00
|
|
|
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.
|
2018-05-07 12:30:55 +00:00
|
|
|
|
|
|
|
Returns:
|
2019-05-09 02:28:46 +00:00
|
|
|
``bool``: On success, True is returned.
|
2018-05-07 12:30:55 +00:00
|
|
|
|
|
|
|
Raises:
|
2019-07-25 09:22:14 +00:00
|
|
|
ValueError: In case the provided string is not a valid chat action.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Send "typing" chat action
|
|
|
|
app.send_chat_action(chat_id, "typing")
|
|
|
|
|
|
|
|
# Send "upload_video" chat action
|
|
|
|
app.send_chat_action(chat_id, "upload_video")
|
|
|
|
|
|
|
|
# Send "playing" chat action
|
|
|
|
app.send_chat_action(chat_id, "playing")
|
|
|
|
|
|
|
|
# Cancel any current chat action
|
|
|
|
app.send_chat_action(chat_id, "cancel")
|
2018-05-07 12:30:55 +00:00
|
|
|
"""
|
|
|
|
|
2019-05-09 02:58:55 +00:00
|
|
|
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
|
2018-05-07 12:30:55 +00:00
|
|
|
|
|
|
|
if "Upload" in action.__name__:
|
2019-05-09 02:58:55 +00:00
|
|
|
action = action(progress=0)
|
2018-05-07 12:30:55 +00:00
|
|
|
else:
|
|
|
|
action = action()
|
|
|
|
|
2018-06-18 19:30:13 +00:00
|
|
|
return await self.send(
|
2018-05-07 12:30:55 +00:00
|
|
|
functions.messages.SetTyping(
|
2018-06-18 19:30:13 +00:00
|
|
|
peer=await self.resolve_peer(chat_id),
|
2018-05-07 12:30:55 +00:00
|
|
|
action=action
|
|
|
|
)
|
|
|
|
)
|