Skip to content

Commit d7e13e7

Browse files
KurimuzonAkumawulan17
authored andcommitted
Add ExternalReplyInfo
Signed-off-by: wulan17 <wulan17@nusantararom.org>
1 parent 5b9081c commit d7e13e7

File tree

5 files changed

+348
-12
lines changed

5 files changed

+348
-12
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ def get_title_list(s: str) -> list:
524524
Audio
525525
AvailableEffect
526526
Document
527+
ExternalReplyInfo
527528
AlternativeVideo
528529
Animation
529530
Video

pyrogram/types/messages_and_media/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .contact_registered import ContactRegistered
2828
from .dice import Dice
2929
from .document import Document
30+
from .external_reply_info import ExternalReplyInfo
3031
from .game import Game
3132
from .giveaway import Giveaway
3233
from .giveaway_launched import GiveawayLaunched
@@ -90,6 +91,7 @@
9091
"Contact",
9192
"ContactRegistered",
9293
"Document",
94+
"ExternalReplyInfo",
9395
"Game",
9496
"Giveaway",
9597
"GiveawayLaunched",
Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Dict, Optional
20+
21+
import pyrogram
22+
from pyrogram import enums, raw, types, utils
23+
24+
from ..object import Object
25+
26+
27+
class ExternalReplyInfo(Object):
28+
"""This object contains information about a message that is being replied to, which may come from another chat or forum topic.
29+
30+
Parameters:
31+
origin (:obj:`~pyrogram.types.MessageOrigin`, *optional*):
32+
Origin of the message replied to by the given message.
33+
34+
chat (:obj:`~pyrogram.types.Chat`, *optional*):
35+
Chat the original message belongs to.
36+
Available only if the chat is a supergroup or a channel.
37+
38+
message_id (``int``, *optional*):
39+
Unique message identifier inside the original chat.
40+
Available only if the original chat is a supergroup or a channel.
41+
42+
link_preview_options (:obj:`~pyrogram.types.LinkPreviewOptions`, *optional*):
43+
Options used for link preview generation for the original message, if it is a text message.
44+
45+
media (:obj:`~pyrogram.enums.MessageMediaType`, *optional*):
46+
The message is a media message.
47+
This field will contain the enumeration type of the media message.
48+
You can use ``media = getattr(message, message.media.value)`` to access the media message.
49+
50+
animation (:obj:`~pyrogram.types.Animation`, *optional*):
51+
Message is an animation, information about the animation.
52+
53+
audio (:obj:`~pyrogram.types.Audio`, *optional*):
54+
Message is an audio file, information about the file.
55+
56+
document (:obj:`~pyrogram.types.Document`, *optional*):
57+
Message is a general file, information about the file.
58+
59+
paid_media (:obj:`~pyrogram.types.PaidMediaInfo`, *optional*):
60+
Message contains paid media; information about the paid media.
61+
62+
photo (:obj:`~pyrogram.types.Photo`, *optional*):
63+
Message is a photo, information about the photo.
64+
65+
sticker (:obj:`~pyrogram.types.Sticker`, *optional*):
66+
Message is a sticker, information about the sticker.
67+
68+
story (:obj:`~pyrogram.types.Story`, *optional*):
69+
Message is a forwarded story.
70+
71+
video (:obj:`~pyrogram.types.Video`, *optional*):
72+
Message is a video, information about the video.
73+
74+
video_note (:obj:`~pyrogram.types.VideoNote`, *optional*):
75+
Message is a video note, information about the video message.
76+
77+
voice (:obj:`~pyrogram.types.Voice`, *optional*):
78+
Message is a voice message, information about the file.
79+
80+
has_media_spoiler (``bool``, *optional*):
81+
True, if the message media is covered by a spoiler animation.
82+
83+
contact (:obj:`~pyrogram.types.Contact`, *optional*):
84+
Message is a shared contact, information about the contact.
85+
86+
dice (:obj:`~pyrogram.types.Dice`, *optional*):
87+
A dice containing a value that is randomly generated by Telegram.
88+
89+
game (:obj:`~pyrogram.types.Game`, *optional*):
90+
Message is a game, information about the game.
91+
92+
giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*):
93+
Message is a scheduled giveaway, information about the giveaway.
94+
95+
giveaway_winners (:obj:`~pyrogram.types.GiveawayWinners`, *optional*):
96+
A giveaway with public winners was completed
97+
98+
invoice (:obj:`~pyrogram.types.Invoice`, *optional*):
99+
Message is a invoice, information about the invoice.
100+
`More about payments » <https://core.telegram.org/bots/api#payments>`_
101+
102+
location (:obj:`~pyrogram.types.Location`, *optional*):
103+
Message is a shared location, information about the location.
104+
105+
poll (:obj:`~pyrogram.types.Poll`, *optional*):
106+
Message is a native poll, information about the poll.
107+
108+
venue (:obj:`~pyrogram.types.Venue`, *optional*):
109+
Message is a venue, information about the venue.
110+
"""
111+
112+
def __init__(
113+
self,
114+
*,
115+
client: "pyrogram.Client" = None,
116+
origin: "types.MessageOrigin" = None,
117+
chat: "types.Chat" = None,
118+
message_id: int,
119+
link_preview_options: Optional["types.LinkPreviewOptions"] = None,
120+
media: Optional["enums.MessageMediaType"] = None,
121+
animation: Optional["types.Animation"] = None,
122+
audio: Optional["types.Audio"] = None,
123+
document: Optional["types.Document"] = None,
124+
paid_media: Optional["types.PaidMediaInfo"] = None,
125+
photo: Optional["types.Photo"] = None,
126+
sticker: Optional["types.Sticker"] = None,
127+
story: Optional["types.Story"] = None,
128+
video: Optional["types.Video"] = None,
129+
video_note: Optional["types.VideoNote"] = None,
130+
voice: Optional["types.Voice"] = None,
131+
has_media_spoiler: Optional[bool] = None,
132+
contact: Optional["types.Contact"] = None,
133+
dice: Optional["types.Dice"] = None,
134+
game: Optional["types.Game"] = None,
135+
giveaway: Optional["types.Giveaway"] = None,
136+
giveaway_winners: Optional["types.GiveawayWinners"] = None,
137+
invoice: Optional["types.Invoice"] = None,
138+
location: Optional["types.Location"] = None,
139+
poll: Optional["types.Poll"] = None,
140+
venue: Optional["types.Venue"] = None,
141+
):
142+
super().__init__(client)
143+
144+
self.origin = origin
145+
self.chat = chat
146+
self.message_id = message_id
147+
self.link_preview_options = link_preview_options
148+
self.media = media
149+
self.animation = animation
150+
self.audio = audio
151+
self.document = document
152+
self.paid_media = paid_media
153+
self.photo = photo
154+
self.sticker = sticker
155+
self.story = story
156+
self.video = video
157+
self.video_note = video_note
158+
self.voice = voice
159+
self.has_media_spoiler = has_media_spoiler
160+
self.contact = contact
161+
self.dice = dice
162+
self.game = game
163+
self.giveaway = giveaway
164+
self.giveaway_winners = giveaway_winners
165+
self.invoice = invoice
166+
self.location = location
167+
self.poll = poll
168+
self.venue = venue
169+
170+
@staticmethod
171+
async def _parse(
172+
client,
173+
reply: "raw.types.MessageReplyHeader",
174+
users: Dict[int, "raw.types.User"],
175+
chats: Dict[int, "raw.types.Chat"],
176+
) -> Optional["ExternalReplyInfo"]:
177+
if not isinstance(reply, raw.types.MessageReplyHeader):
178+
return None
179+
180+
if not reply.reply_from:
181+
return None
182+
183+
animation = None
184+
audio = None
185+
document = None
186+
paid_media = None
187+
photo = None
188+
sticker = None
189+
story = None
190+
video = None
191+
video_note = None
192+
voice = None
193+
contact = None
194+
dice = None
195+
game = None
196+
giveaway = None
197+
giveaway_winners = None
198+
invoice = None
199+
location = None
200+
poll = None
201+
venue = None
202+
203+
media = reply.reply_media
204+
media_type = None
205+
has_media_spoiler = None
206+
207+
if media:
208+
if isinstance(media, raw.types.MessageMediaPhoto):
209+
photo = types.Photo._parse(client, media.photo, media.ttl_seconds)
210+
media_type = enums.MessageMediaType.PHOTO
211+
has_media_spoiler = media.spoiler
212+
elif isinstance(media, raw.types.MessageMediaGeo):
213+
location = types.Location._parse(client, media.geo)
214+
media_type = enums.MessageMediaType.LOCATION
215+
elif isinstance(media, raw.types.MessageMediaContact):
216+
contact = types.Contact._parse(client, media)
217+
media_type = enums.MessageMediaType.CONTACT
218+
elif isinstance(media, raw.types.MessageMediaVenue):
219+
venue = types.Venue._parse(client, media)
220+
media_type = enums.MessageMediaType.VENUE
221+
elif isinstance(media, raw.types.MessageMediaGame):
222+
game = types.Game._parse(client, media)
223+
media_type = enums.MessageMediaType.GAME
224+
elif isinstance(media, raw.types.MessageMediaGiveaway):
225+
giveaway = types.Giveaway._parse(client, media, chats)
226+
media_type = enums.MessageMediaType.GIVEAWAY
227+
elif isinstance(media, raw.types.MessageMediaGiveawayResults):
228+
giveaway_winners = await types.GiveawayWinners._parse(client, media, users, chats)
229+
media_type = enums.MessageMediaType.GIVEAWAY_WINNERS
230+
elif isinstance(media, raw.types.MessageMediaInvoice):
231+
invoice = types.Invoice._parse(client, media)
232+
media_type = enums.MessageMediaType.INVOICE
233+
elif isinstance(media, raw.types.MessageMediaStory):
234+
story = await types.Story._parse(client, media, media.peer, users, chats)
235+
media_type = enums.MessageMediaType.STORY
236+
elif isinstance(media, raw.types.MessageMediaDocument):
237+
doc = media.document
238+
239+
if isinstance(doc, raw.types.Document):
240+
attributes = {type(i): i for i in doc.attributes}
241+
242+
file_name = getattr(
243+
attributes.get(
244+
raw.types.DocumentAttributeFilename, None
245+
), "file_name", None
246+
)
247+
248+
if raw.types.DocumentAttributeAnimated in attributes:
249+
video_attributes = attributes.get(raw.types.DocumentAttributeVideo, None)
250+
animation = types.Animation._parse(client, doc, video_attributes, file_name)
251+
media_type = enums.MessageMediaType.ANIMATION
252+
has_media_spoiler = media.spoiler
253+
elif raw.types.DocumentAttributeSticker in attributes:
254+
sticker = await types.Sticker._parse(client, doc, attributes)
255+
media_type = enums.MessageMediaType.STICKER
256+
elif raw.types.DocumentAttributeVideo in attributes:
257+
video_attributes = attributes[raw.types.DocumentAttributeVideo]
258+
259+
if video_attributes.round_message:
260+
video_note = types.VideoNote._parse(client, doc, video_attributes, media.ttl_seconds)
261+
media_type = enums.MessageMediaType.VIDEO_NOTE
262+
else:
263+
video = types.Video._parse(client, doc, video_attributes, file_name, media.ttl_seconds, media.video_cover, media.video_timestamp, media.alt_documents)
264+
media_type = enums.MessageMediaType.VIDEO
265+
has_media_spoiler = media.spoiler
266+
elif raw.types.DocumentAttributeAudio in attributes:
267+
audio_attributes = attributes[raw.types.DocumentAttributeAudio]
268+
269+
if audio_attributes.voice:
270+
voice = types.Voice._parse(client, doc, audio_attributes, media.ttl_seconds)
271+
media_type = enums.MessageMediaType.VOICE
272+
else:
273+
audio = types.Audio._parse(client, doc, audio_attributes, file_name)
274+
media_type = enums.MessageMediaType.AUDIO
275+
else:
276+
document = types.Document._parse(client, doc, file_name)
277+
media_type = enums.MessageMediaType.DOCUMENT
278+
elif isinstance(media, raw.types.MessageMediaPoll):
279+
poll = types.Poll._parse(client, media)
280+
media_type = enums.MessageMediaType.POLL
281+
elif isinstance(media, raw.types.MessageMediaDice):
282+
dice = types.Dice._parse(client, media)
283+
media_type = enums.MessageMediaType.DICE
284+
elif isinstance(media, raw.types.MessageMediaPaidMedia):
285+
paid_media = types.PaidMediaInfo._parse(client, media)
286+
media_type = enums.MessageMediaType.PAID_MEDIA
287+
else:
288+
media = None
289+
290+
return ExternalReplyInfo(
291+
origin=types.MessageOrigin._parse(
292+
client,
293+
reply.reply_from,
294+
users,
295+
chats,
296+
),
297+
chat=types.Chat._parse_chat(
298+
client,
299+
chats.get(utils.get_raw_peer_id(reply.reply_to_peer_id)),
300+
),
301+
message_id=reply.reply_to_msg_id,
302+
link_preview_options=types.LinkPreviewOptions._parse(reply.reply_media),
303+
media=media_type,
304+
animation=animation,
305+
audio=audio,
306+
document=document,
307+
paid_media=paid_media,
308+
photo=photo,
309+
sticker=sticker,
310+
story=story,
311+
video=video,
312+
video_note=video_note,
313+
voice=voice,
314+
has_media_spoiler=has_media_spoiler,
315+
contact=contact,
316+
dice=dice,
317+
game=game,
318+
giveaway=giveaway,
319+
giveaway_winners=giveaway_winners,
320+
invoice=invoice,
321+
location=location,
322+
poll=poll,
323+
venue=venue
324+
)

pyrogram/types/messages_and_media/game.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ def __init__(
6969
self.animation = animation
7070

7171
@staticmethod
72-
def _parse(client, message: "raw.types.Message") -> "Game":
73-
game: "raw.types.Game" = message.media.game
72+
def _parse(client, media: "raw.types.MessageMediaGame") -> "Game":
7473
animation = None
7574

76-
if game.document:
77-
attributes = {type(i): i for i in game.document.attributes}
75+
if media.game.document:
76+
attributes = {type(i): i for i in media.game.document.attributes}
7877

7978
file_name = getattr(
8079
attributes.get(
@@ -84,17 +83,17 @@ def _parse(client, message: "raw.types.Message") -> "Game":
8483

8584
animation = types.Animation._parse(
8685
client,
87-
game.document,
86+
media.game.document,
8887
attributes.get(raw.types.DocumentAttributeVideo, None),
8988
file_name
9089
)
9190

9291
return Game(
93-
id=game.id,
94-
title=game.title,
95-
short_name=game.short_name,
96-
description=game.description,
97-
photo=types.Photo._parse(client, game.photo),
92+
id=media.game.id,
93+
title=media.game.title,
94+
short_name=media.game.short_name,
95+
description=media.game.description,
96+
photo=types.Photo._parse(client, media.game.photo),
9897
animation=animation,
9998
client=client
10099
)

0 commit comments

Comments
 (0)