|
| 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 | + ) |
0 commit comments