forked from TelegramPlayground/PyroTGFork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft_message.py
More file actions
220 lines (182 loc) · 8.64 KB
/
Copy pathdraft_message.py
File metadata and controls
220 lines (182 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
from datetime import datetime
from typing import Optional, Union
import pyrogram
from pyrogram import raw, types, utils, enums
from ..object import Object
from ..messages_and_media.message import Str
class DraftMessage(Object):
"""Contains information about a message draft.
Parameters:
reply_to_message_id (``int``, *optional*):
The id of the message which this draft directly replied to.
reply_to_message (:obj:`~pyrogram.types.Message`, *optional*):
Information about the message to be replied.
date (:py:obj:`~datetime.datetime`, *optional*):
Date the message was sent.
text (``str``, *optional*):
For text messages, the actual UTF-8 text of the message, 0-4096 characters.
If the message contains entities (bold, italic, ...) you can access *text.markdown* or
*text.html* to get the marked up message text. In case there is no entity, the fields
will contain the same text as *text*.
entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text.
link_preview_options (:obj:`~pyrogram.types.LinkPreviewOptions`, *optional*):
Options used for link preview generation for the draft message, if it is a text message and link preview options were changed.
effect_id (``str``, *optional*):
Unique identifier of the message effect added to the message. Use :meth:`~pyrogram.Client.get_message_effects` to get the list of available message effect ids.
video_note (:obj:`~pyrogram.types.VideoNote`, *optional*):
Message is a video note, information about the video message.
voice (:obj:`~pyrogram.types.Voice`, *optional*):
Message is a voice message, information about the file.
show_caption_above_media (``bool``, *optional*):
True, if the caption must be shown above the message media.
media (:obj:`~pyrogram.enums.MessageMediaType`, *optional*):
The message is a media message.
This field will contain the enumeration type of the media message.
You can use ``media = getattr(message, message.media.value)`` to access the media message.
empty (``bool``, *optional*):
The message is empty.
A message can be empty in case it was deleted or you tried to retrieve a message that doesn't exist yet.
chat (:obj:`~pyrogram.types.Chat`, *optional*):
Conversation the message belongs to. Can be None if unknown.
"""
def __init__(
self,
*,
reply_to_message_id: int = None,
reply_to_message: "types.Message" = None,
date: datetime = None,
text: Str = None,
entities: list["types.MessageEntity"] = None,
link_preview_options: "types.LinkPreviewOptions" = None,
effect_id: str = None,
video_note: "types.VideoNote" = None,
voice: "types.Voice" = None,
show_caption_above_media: bool = None,
media: "enums.MessageMediaType" = None,
empty: bool = None,
chat: "types.Chat" = None,
_raw: "raw.types.DraftMessage" = None
):
super().__init__()
self.reply_to_message_id = reply_to_message_id
self.reply_to_message = reply_to_message
self.date = date
self.text = text
self.entities = entities
self.link_preview_options = link_preview_options
self.effect_id = effect_id
self.video_note = video_note
self.voice = voice
self.show_caption_above_media = show_caption_above_media
self.media = media
self.empty = empty
self.chat = chat
self._raw = _raw
@staticmethod
def _parse(
client: "pyrogram.Client",
raw_draft_message: "raw.base.DraftMessage",
users: dict, # raw
chats: dict, # raw
) -> "DraftMessage":
if not raw_draft_message:
return None
if isinstance(raw_draft_message, raw.types.DraftMessageEmpty):
return DraftMessage(
date=utils.timestamp_to_datetime(raw_draft_message.date),
empty=True,
_raw=raw_draft_message
)
entities = [types.MessageEntity._parse(client, entity, users) for entity in raw_draft_message.entities]
entities = types.List(filter(lambda x: x is not None, entities))
voice = None
video_note = None
link_preview_options = None
web_page_url = None
media = raw_draft_message.media
media_type = None
if media:
if isinstance(media, raw.types.MessageMediaDocument):
doc = media.document
if isinstance(doc, raw.types.Document):
attributes = {type(i): i for i in doc.attributes}
file_name = getattr(
attributes.get(
raw.types.DocumentAttributeFilename, None
), "file_name", None
)
if raw.types.DocumentAttributeVideo in attributes:
video_attributes = attributes[raw.types.DocumentAttributeVideo]
if video_attributes.round_message:
video_note = types.VideoNote._parse(client, doc, video_attributes, media.ttl_seconds)
media_type = enums.MessageMediaType.VIDEO_NOTE
elif raw.types.DocumentAttributeAudio in attributes:
audio_attributes = attributes[raw.types.DocumentAttributeAudio]
if audio_attributes.voice:
voice = types.Voice._parse(client, doc, audio_attributes, media.ttl_seconds)
media_type = enums.MessageMediaType.VOICE
elif isinstance(media, raw.types.MessageMediaWebPage):
if isinstance(media.webpage, raw.types.WebPage):
media_type = None
web_page_url = media.webpage.url
elif isinstance(media.webpage, raw.types.WebPageEmpty):
media_type = None
web_page_url = getattr(media.webpage, "url", None)
else:
media_type = None
web_page_url = utils.get_first_url(raw_draft_message)
link_preview_options = types.LinkPreviewOptions._parse(
client,
media,
web_page_url,
getattr(raw_draft_message, "invert_media", False)
)
if (
not link_preview_options and
web_page_url
):
# TODO: no_webpage:flags.1?true
link_preview_options = types.LinkPreviewOptions._parse(
client,
None,
web_page_url,
getattr(raw_draft_message, "invert_media", False)
)
draft_message = DraftMessage(
date=utils.timestamp_to_datetime(raw_draft_message.date),
text=(
Str(raw_draft_message.message).init(entities) or None
),
entities=entities or None,
link_preview_options=link_preview_options,
effect_id=raw_draft_message.effect,
video_note=video_note,
voice=voice,
show_caption_above_media=getattr(raw_draft_message, "invert_media", False),
media=media_type,
_raw=raw_draft_message
)
# if raw_draft_message.reply_to:
# # TODO reply_to:flags.4?InputReplyTo
# draft_message.reply_to_message_id
# draft_message.reply_to_message
# TODO: suggested_post:flags.8?SuggestedPost
return draft_message