/
_message.py
4825 lines (4146 loc) · 198 KB
/
_message.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# pylint: disable=too-many-instance-attributes, too-many-arguments
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2024
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Message."""
import datetime
import re
from html import escape
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, TypedDict, Union
from telegram._chat import Chat
from telegram._chatboost import ChatBoostAdded
from telegram._dice import Dice
from telegram._files.animation import Animation
from telegram._files.audio import Audio
from telegram._files.contact import Contact
from telegram._files.document import Document
from telegram._files.location import Location
from telegram._files.photosize import PhotoSize
from telegram._files.sticker import Sticker
from telegram._files.venue import Venue
from telegram._files.video import Video
from telegram._files.videonote import VideoNote
from telegram._files.voice import Voice
from telegram._forumtopic import (
ForumTopicClosed,
ForumTopicCreated,
ForumTopicEdited,
ForumTopicReopened,
GeneralForumTopicHidden,
GeneralForumTopicUnhidden,
)
from telegram._games.game import Game
from telegram._inline.inlinekeyboardmarkup import InlineKeyboardMarkup
from telegram._linkpreviewoptions import LinkPreviewOptions
from telegram._messageautodeletetimerchanged import MessageAutoDeleteTimerChanged
from telegram._messageentity import MessageEntity
from telegram._passport.passportdata import PassportData
from telegram._payment.invoice import Invoice
from telegram._payment.successfulpayment import SuccessfulPayment
from telegram._poll import Poll
from telegram._proximityalerttriggered import ProximityAlertTriggered
from telegram._reply import ReplyParameters
from telegram._shared import ChatShared, UsersShared
from telegram._story import Story
from telegram._telegramobject import TelegramObject
from telegram._user import User
from telegram._utils.argumentparsing import parse_sequence_arg
from telegram._utils.datetime import extract_tzinfo_from_defaults, from_timestamp
from telegram._utils.defaultvalue import DEFAULT_NONE, DefaultValue
from telegram._utils.types import (
CorrectOptionID,
FileInput,
JSONDict,
MarkdownVersion,
ODVInput,
ReplyMarkup,
)
from telegram._utils.warnings import warn
from telegram._videochat import (
VideoChatEnded,
VideoChatParticipantsInvited,
VideoChatScheduled,
VideoChatStarted,
)
from telegram._webappdata import WebAppData
from telegram._writeaccessallowed import WriteAccessAllowed
from telegram.constants import ZERO_DATE, MessageAttachmentType, ParseMode
from telegram.helpers import escape_markdown
from telegram.warnings import PTBDeprecationWarning
if TYPE_CHECKING:
from telegram import (
Bot,
ExternalReplyInfo,
GameHighScore,
Giveaway,
GiveawayCompleted,
GiveawayCreated,
GiveawayWinners,
InputMedia,
InputMediaAudio,
InputMediaDocument,
InputMediaPhoto,
InputMediaVideo,
LabeledPrice,
MessageId,
MessageOrigin,
ReactionType,
TextQuote,
)
class _ReplyKwargs(TypedDict):
__slots__ = ("chat_id", "reply_parameters") # type: ignore[misc]
chat_id: Union[str, int]
reply_parameters: ReplyParameters
class MaybeInaccessibleMessage(TelegramObject):
"""Base class for Telegram Message Objects.
Currently, that includes :class:`telegram.Message` and :class:`telegram.InaccessibleMessage`.
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`message_id` and :attr:`chat` are equal
.. versionchanged:: 21.0
``__bool__`` is no longer overriden and defaults to Pythons standard implementation.
.. versionadded:: 20.8
Args:
message_id (:obj:`int`): Unique message identifier.
date (:class:`datetime.datetime`): Date the message was sent in Unix time or 0 in Unix
time. Converted to :class:`datetime.datetime`
|datetime_localization|
chat (:class:`telegram.Chat`): Conversation the message belongs to.
Attributes:
message_id (:obj:`int`): Unique message identifier.
date (:class:`datetime.datetime`): Date the message was sent in Unix time or 0 in Unix
time. Converted to :class:`datetime.datetime`
|datetime_localization|
chat (:class:`telegram.Chat`): Conversation the message belongs to.
"""
__slots__ = ("chat", "date", "message_id")
def __init__(
self,
chat: Chat,
message_id: int,
date: datetime.datetime,
*,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.chat: Chat = chat
self.message_id: int = message_id
self.date: datetime.datetime = date
self._id_attrs = (self.message_id, self.chat)
self._freeze()
@property
def is_accessible(self) -> bool:
"""Convenience attribute. :obj:`True`, if the date is not 0 in Unix time.
.. versionadded:: 20.8
"""
# Once we drop support for python 3.9, this can be made a TypeGuard function:
# def is_accessible(self) -> TypeGuard[Message]:
return self.date != ZERO_DATE
@classmethod
def _de_json(
cls, data: Optional[JSONDict], bot: "Bot", api_kwargs: Optional[JSONDict] = None
) -> Optional["MaybeInaccessibleMessage"]:
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)
if not data:
return None
if cls is MaybeInaccessibleMessage:
if data["date"] == 0:
return InaccessibleMessage.de_json(data=data, bot=bot)
return Message.de_json(data=data, bot=bot)
# Get the local timezone from the bot if it has defaults
loc_tzinfo = extract_tzinfo_from_defaults(bot)
# this is to include the Literal from InaccessibleMessage
if data["date"] == 0:
data["date"] = ZERO_DATE
else:
data["date"] = from_timestamp(data["date"], tzinfo=loc_tzinfo)
data["chat"] = Chat.de_json(data.get("chat"), bot)
return super()._de_json(data=data, bot=bot)
class InaccessibleMessage(MaybeInaccessibleMessage):
"""This object represents an inaccessible message.
These are messages that are e.g. deleted.
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`message_id` and :attr:`chat` are equal
.. versionadded:: 20.8
Args:
message_id (:obj:`int`): Unique message identifier.
chat (:class:`telegram.Chat`): Chat the message belongs to.
Attributes:
message_id (:obj:`int`): Unique message identifier.
date (:class:`constants.ZERO_DATE`): Always :tg-const:`telegram.constants.ZERO_DATE`.
The field can be used to differentiate regular and inaccessible messages.
chat (:class:`telegram.Chat`): Chat the message belongs to.
"""
__slots__ = ()
def __init__(
self,
chat: Chat,
message_id: int,
*,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(chat=chat, message_id=message_id, date=ZERO_DATE, api_kwargs=api_kwargs)
self._freeze()
class Message(MaybeInaccessibleMessage):
# fmt: off
"""This object represents a message.
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`message_id` and :attr:`chat` are equal.
Note:
In Python :keyword:`from` is a reserved word. Use :paramref:`from_user` instead.
.. versionchanged:: 21.0
Removed deprecated arguments and attributes ``user_shared``, ``forward_from``,
``forward_from_chat``, ``forward_from_message_id``, ``forward_signature``,
``forward_sender_name`` and ``forward_date``.
.. versionchanged:: 20.8
* This class is now a subclass of :class:`telegram.MaybeInaccessibleMessage`.
* The :paramref:`pinned_message` now can be either class:`telegram.Message` or
class:`telegram.InaccessibleMessage`.
.. versionchanged:: 20.0
* The arguments and attributes ``voice_chat_scheduled``, ``voice_chat_started`` and
``voice_chat_ended``, ``voice_chat_participants_invited`` were renamed to
:paramref:`video_chat_scheduled`/:attr:`video_chat_scheduled`,
:paramref:`video_chat_started`/:attr:`video_chat_started`,
:paramref:`video_chat_ended`/:attr:`video_chat_ended` and
:paramref:`video_chat_participants_invited`/:attr:`video_chat_participants_invited`,
respectively, in accordance to Bot API 6.0.
* The following are now keyword-only arguments in Bot methods:
``{read, write, connect, pool}_timeout``, ``api_kwargs``, ``contact``, ``quote``,
``filename``, ``loaction``, ``venue``. Use a named argument for those,
and notice that some positional arguments changed position as a result.
Args:
message_id (:obj:`int`): Unique message identifier inside this chat.
from_user (:class:`telegram.User`, optional): Sender of the message; empty for messages
sent to channels. For backward compatibility, this will contain a fake sender user in
non-channel chats, if the message was sent on behalf of a chat.
sender_chat (:class:`telegram.Chat`, optional): Sender of the message, sent on behalf of a
chat. For example, the channel itself for channel posts, the supergroup itself for
messages from anonymous group administrators, the linked channel for messages
automatically forwarded to the discussion group. For backward compatibility,
:attr:`from_user` contains a fake sender user in non-channel chats, if the message was
sent on behalf of a chat.
date (:class:`datetime.datetime`): Date the message was sent in Unix time. Converted to
:class:`datetime.datetime`.
.. versionchanged:: 20.3
|datetime_localization|
chat (:class:`telegram.Chat`): Conversation the message belongs to.
is_automatic_forward (:obj:`bool`, optional): :obj:`True`, if the message is a channel
post that was automatically forwarded to the connected discussion group.
.. versionadded:: 13.9
reply_to_message (:class:`telegram.Message`, optional): For replies, the original message.
Note that the Message object in this field will not contain further
``reply_to_message`` fields even if it itself is a reply.
edit_date (:class:`datetime.datetime`, optional): Date the message was last edited in Unix
time. Converted to :class:`datetime.datetime`.
.. versionchanged:: 20.3
|datetime_localization|
has_protected_content (:obj:`bool`, optional): :obj:`True`, if the message can't be
forwarded.
.. versionadded:: 13.9
is_from_offline (:obj:`bool`, optional): :obj:`True`, if the message was sent
by an implicit action, for example, as an away or a greeting business message,
or as a scheduled message.
.. versionadded:: 21.1
media_group_id (:obj:`str`, optional): The unique identifier of a media message group this
message belongs to.
text (:obj:`str`, optional): For text messages, the actual UTF-8 text of the message,
0-:tg-const:`telegram.constants.MessageLimit.MAX_TEXT_LENGTH` characters.
entities (Sequence[:class:`telegram.MessageEntity`], optional): For text messages, special
entities like usernames, URLs, bot commands, etc. that appear in the text. See
:attr:`parse_entity` and :attr:`parse_entities` methods for how to use properly.
This list is empty if the message does not contain entities.
.. versionchanged:: 20.0
|sequenceclassargs|
link_preview_options (:class:`telegram.LinkPreviewOptions`, optional): Options used for
link preview generation for the message, if it is a text message and link preview
options were changed.
.. versionadded:: 20.8
caption_entities (Sequence[:class:`telegram.MessageEntity`], optional): For messages with a
Caption. Special entities like usernames, URLs, bot commands, etc. that appear in the
caption. See :attr:`Message.parse_caption_entity` and :attr:`parse_caption_entities`
methods for how to use properly. This list is empty if the message does not contain
caption entities.
.. versionchanged:: 20.0
|sequenceclassargs|
audio (:class:`telegram.Audio`, optional): Message is an audio file, information
about the file.
document (:class:`telegram.Document`, optional): Message is a general file, information
about the file.
animation (:class:`telegram.Animation`, optional): Message is an animation, information
about the animation. For backward compatibility, when this field is set, the document
field will also be set.
game (:class:`telegram.Game`, optional): Message is a game, information about the game.
photo (Sequence[:class:`telegram.PhotoSize`], optional): Message is a photo, available
sizes of the photo. This list is empty if the message does not contain a photo.
.. versionchanged:: 20.0
|sequenceclassargs|
sticker (:class:`telegram.Sticker`, optional): Message is a sticker, information
about the sticker.
story (:class:`telegram.Story`, optional): Message is a forwarded story.
.. versionadded:: 20.5
video (:class:`telegram.Video`, optional): Message is a video, information about the
video.
voice (:class:`telegram.Voice`, optional): Message is a voice message, information about
the file.
video_note (:class:`telegram.VideoNote`, optional): Message is a video note, information
about the video message.
new_chat_members (Sequence[:class:`telegram.User`], optional): New members that were added
to the group or supergroup and information about them (the bot itself may be one of
these members). This list is empty if the message does not contain new chat members.
.. versionchanged:: 20.0
|sequenceclassargs|
caption (:obj:`str`, optional): Caption for the animation, audio, document, photo, video
or voice, 0-:tg-const:`telegram.constants.MessageLimit.CAPTION_LENGTH` characters.
contact (:class:`telegram.Contact`, optional): Message is a shared contact, information
about the contact.
location (:class:`telegram.Location`, optional): Message is a shared location, information
about the location.
venue (:class:`telegram.Venue`, optional): Message is a venue, information about the
venue. For backward compatibility, when this field is set, the location field will
also be set.
left_chat_member (:class:`telegram.User`, optional): A member was removed from the group,
information about them (this member may be the bot itself).
new_chat_title (:obj:`str`, optional): A chat title was changed to this value.
new_chat_photo (Sequence[:class:`telegram.PhotoSize`], optional): A chat photo was changed
to this value. This list is empty if the message does not contain a new chat photo.
.. versionchanged:: 20.0
|sequenceclassargs|
delete_chat_photo (:obj:`bool`, optional): Service message: The chat photo was deleted.
group_chat_created (:obj:`bool`, optional): Service message: The group has been created.
supergroup_chat_created (:obj:`bool`, optional): Service message: The supergroup has been
created. This field can't be received in a message coming through updates, because bot
can't be a member of a supergroup when it is created. It can only be found in
:attr:`reply_to_message` if someone replies to a very first message in a directly
created supergroup.
channel_chat_created (:obj:`bool`, optional): Service message: The channel has been
created. This field can't be received in a message coming through updates, because bot
can't be a member of a channel when it is created. It can only be found in
:attr:`reply_to_message` if someone replies to a very first message in a channel.
message_auto_delete_timer_changed (:class:`telegram.MessageAutoDeleteTimerChanged`, \
optional): Service message: auto-delete timer settings changed in the chat.
.. versionadded:: 13.4
migrate_to_chat_id (:obj:`int`, optional): The group has been migrated to a supergroup
with the specified identifier.
migrate_from_chat_id (:obj:`int`, optional): The supergroup has been migrated from a group
with the specified identifier.
pinned_message (:class:`telegram.MaybeInaccessibleMessage`, optional): Specified message
was pinned. Note that the Message object in this field will not contain further
:attr:`reply_to_message` fields even if it is itself a reply.
.. versionchanged:: 20.8
This attribute now is either class:`telegram.Message` or
class:`telegram.InaccessibleMessage`.
invoice (:class:`telegram.Invoice`, optional): Message is an invoice for a payment,
information about the invoice.
successful_payment (:class:`telegram.SuccessfulPayment`, optional): Message is a service
message about a successful payment, information about the payment.
connected_website (:obj:`str`, optional): The domain name of the website on which the user
has logged in.
author_signature (:obj:`str`, optional): Signature of the post author for messages in
channels, or the custom title of an anonymous group administrator.
passport_data (:class:`telegram.PassportData`, optional): Telegram Passport data.
poll (:class:`telegram.Poll`, optional): Message is a native poll,
information about the poll.
dice (:class:`telegram.Dice`, optional): Message is a dice with random value.
via_bot (:class:`telegram.User`, optional): Bot through which message was sent.
proximity_alert_triggered (:class:`telegram.ProximityAlertTriggered`, optional): Service
message. A user in the chat triggered another user's proximity alert while sharing
Live Location.
video_chat_scheduled (:class:`telegram.VideoChatScheduled`, optional): Service message:
video chat scheduled.
.. versionadded:: 20.0
video_chat_started (:class:`telegram.VideoChatStarted`, optional): Service message: video
chat started.
.. versionadded:: 20.0
video_chat_ended (:class:`telegram.VideoChatEnded`, optional): Service message: video chat
ended.
.. versionadded:: 20.0
video_chat_participants_invited (:class:`telegram.VideoChatParticipantsInvited` optional):
Service message: new participants invited to a video chat.
.. versionadded:: 20.0
web_app_data (:class:`telegram.WebAppData`, optional): Service message: data sent by a Web
App.
.. versionadded:: 20.0
reply_markup (:class:`telegram.InlineKeyboardMarkup`, optional): Inline keyboard attached
to the message. :paramref:`~telegram.InlineKeyboardButton.login_url` buttons are
represented as ordinary url buttons.
is_topic_message (:obj:`bool`, optional): :obj:`True`, if the message is sent to a forum
topic.
.. versionadded:: 20.0
message_thread_id (:obj:`int`, optional): Unique identifier of a message thread to which
the message belongs; for supergroups only.
.. versionadded:: 20.0
forum_topic_created (:class:`telegram.ForumTopicCreated`, optional): Service message:
forum topic created.
.. versionadded:: 20.0
forum_topic_closed (:class:`telegram.ForumTopicClosed`, optional): Service message:
forum topic closed.
.. versionadded:: 20.0
forum_topic_reopened (:class:`telegram.ForumTopicReopened`, optional): Service message:
forum topic reopened.
.. versionadded:: 20.0
forum_topic_edited (:class:`telegram.ForumTopicEdited`, optional): Service message:
forum topic edited.
.. versionadded:: 20.0
general_forum_topic_hidden (:class:`telegram.GeneralForumTopicHidden`, optional):
Service message: General forum topic hidden.
.. versionadded:: 20.0
general_forum_topic_unhidden (:class:`telegram.GeneralForumTopicUnhidden`, optional):
Service message: General forum topic unhidden.
.. versionadded:: 20.0
write_access_allowed (:class:`telegram.WriteAccessAllowed`, optional): Service message:
the user allowed the bot to write messages after adding it to the attachment or side
menu, launching a Web App from a link, or accepting an explicit request from a Web App
sent by the method
`requestWriteAccess <https://core.telegram.org/bots/webapps#initializing-mini-apps>`_.
.. versionadded:: 20.0
has_media_spoiler (:obj:`bool`, optional): :obj:`True`, if the message media is covered
by a spoiler animation.
.. versionadded:: 20.0
users_shared (:class:`telegram.UsersShared`, optional): Service message: users were shared
with the bot
.. versionadded:: 20.8
chat_shared (:class:`telegram.ChatShared`, optional):Service message: a chat was shared
with the bot.
.. versionadded:: 20.1
giveaway_created (:class:`telegram.GiveawayCreated`, optional): Service message: a
scheduled giveaway was created
.. versionadded:: 20.8
giveaway (:class:`telegram.Giveaway`, optional): The message is a scheduled giveaway
message
.. versionadded:: 20.8
giveaway_winners (:class:`telegram.GiveawayWinners`, optional): A giveaway with public
winners was completed
.. versionadded:: 20.8
giveaway_completed (:class:`telegram.GiveawayCompleted`, optional): Service message: a
giveaway without public winners was completed
.. versionadded:: 20.8
external_reply (:class:`telegram.ExternalReplyInfo`, optional): Information about the
message that is being replied to, which may come from another chat or forum topic.
.. versionadded:: 20.8
quote (:class:`telegram.TextQuote`, optional): For replies that quote part of the original
message, the quoted part of the message.
.. versionadded:: 20.8
forward_origin (:class:`telegram.MessageOrigin`, optional): Information about the original
message for forwarded messages
.. versionadded:: 20.8
reply_to_story (:class:`telegram.Story`, optional): For replies to a story, the original
story.
.. versionadded:: 21.0
boost_added (:class:`telegram.ChatBoostAdded`, optional): Service message: user boosted
the chat.
.. versionadded:: 21.0
sender_boost_count (:obj:`int`, optional): If the sender of the
message boosted the chat, the number of boosts added by the user.
.. versionadded:: 21.0
business_connection_id (:obj:`str`, optional): Unique identifier of the business connection
from which the message was received. If non-empty, the message belongs to a chat of the
corresponding business account that is independent from any potential bot chat which
might share the same identifier.
.. versionadded:: 21.1
sender_business_bot (:obj:`telegram.User`, optional): The bot that actually sent the
message on behalf of the business account. Available only for outgoing messages sent
on behalf of the connected business account.
.. versionadded:: 21.1
Attributes:
message_id (:obj:`int`): Unique message identifier inside this chat.
from_user (:class:`telegram.User`): Optional. Sender of the message; empty for messages
sent to channels. For backward compatibility, this will contain a fake sender user in
non-channel chats, if the message was sent on behalf of a chat.
sender_chat (:class:`telegram.Chat`): Optional. Sender of the message, sent on behalf of a
chat. For example, the channel itself for channel posts, the supergroup itself for
messages from anonymous group administrators, the linked channel for messages
automatically forwarded to the discussion group. For backward compatibility,
:attr:`from_user` contains a fake sender user in non-channel chats, if the message was
sent on behalf of a chat.
date (:class:`datetime.datetime`): Date the message was sent in Unix time. Converted to
:class:`datetime.datetime`.
.. versionchanged:: 20.3
|datetime_localization|
chat (:class:`telegram.Chat`): Conversation the message belongs to.
is_automatic_forward (:obj:`bool`): Optional. :obj:`True`, if the message is a channel
post that was automatically forwarded to the connected discussion group.
.. versionadded:: 13.9
reply_to_message (:class:`telegram.Message`): Optional. For replies, the original message.
Note that the Message object in this field will not contain further
``reply_to_message`` fields even if it itself is a reply.
edit_date (:class:`datetime.datetime`): Optional. Date the message was last edited in Unix
time. Converted to :class:`datetime.datetime`.
.. versionchanged:: 20.3
|datetime_localization|
has_protected_content (:obj:`bool`): Optional. :obj:`True`, if the message can't be
forwarded.
.. versionadded:: 13.9
is_from_offline (:obj:`bool`): Optional. :obj:`True`, if the message was sent
by an implicit action, for example, as an away or a greeting business message,
or as a scheduled message.
.. versionadded:: 21.1
media_group_id (:obj:`str`): Optional. The unique identifier of a media message group this
message belongs to.
text (:obj:`str`): Optional. For text messages, the actual UTF-8 text of the message,
0-:tg-const:`telegram.constants.MessageLimit.MAX_TEXT_LENGTH` characters.
entities (Tuple[:class:`telegram.MessageEntity`]): Optional. For text messages, special
entities like usernames, URLs, bot commands, etc. that appear in the text. See
:attr:`parse_entity` and :attr:`parse_entities` methods for how to use properly.
This list is empty if the message does not contain entities.
.. versionchanged:: 20.0
|tupleclassattrs|
link_preview_options (:class:`telegram.LinkPreviewOptions`): Optional. Options used for
link preview generation for the message, if it is a text message and link preview
options were changed.
.. versionadded:: 20.8
caption_entities (Tuple[:class:`telegram.MessageEntity`]): Optional. For messages with a
Caption. Special entities like usernames, URLs, bot commands, etc. that appear in the
caption. See :attr:`Message.parse_caption_entity` and :attr:`parse_caption_entities`
methods for how to use properly. This list is empty if the message does not contain
caption entities.
.. versionchanged:: 20.0
|tupleclassattrs|
audio (:class:`telegram.Audio`): Optional. Message is an audio file, information
about the file.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
document (:class:`telegram.Document`): Optional. Message is a general file, information
about the file.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
animation (:class:`telegram.Animation`): Optional. Message is an animation, information
about the animation. For backward compatibility, when this field is set, the document
field will also be set.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
game (:class:`telegram.Game`): Optional. Message is a game, information about the game.
photo (Tuple[:class:`telegram.PhotoSize`]): Optional. Message is a photo, available
sizes of the photo. This list is empty if the message does not contain a photo.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
.. versionchanged:: 20.0
|tupleclassattrs|
sticker (:class:`telegram.Sticker`): Optional. Message is a sticker, information
about the sticker.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
story (:class:`telegram.Story`): Optional. Message is a forwarded story.
.. versionadded:: 20.5
video (:class:`telegram.Video`): Optional. Message is a video, information about the
video.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
voice (:class:`telegram.Voice`): Optional. Message is a voice message, information about
the file.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
video_note (:class:`telegram.VideoNote`): Optional. Message is a video note, information
about the video message.
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
new_chat_members (Tuple[:class:`telegram.User`]): Optional. New members that were added
to the group or supergroup and information about them (the bot itself may be one of
these members). This list is empty if the message does not contain new chat members.
.. versionchanged:: 20.0
|tupleclassattrs|
caption (:obj:`str`): Optional. Caption for the animation, audio, document, photo, video
or voice, 0-:tg-const:`telegram.constants.MessageLimit.CAPTION_LENGTH` characters.
contact (:class:`telegram.Contact`): Optional. Message is a shared contact, information
about the contact.
location (:class:`telegram.Location`): Optional. Message is a shared location, information
about the location.
venue (:class:`telegram.Venue`): Optional. Message is a venue, information about the
venue. For backward compatibility, when this field is set, the location field will
also be set.
left_chat_member (:class:`telegram.User`): Optional. A member was removed from the group,
information about them (this member may be the bot itself).
new_chat_title (:obj:`str`): Optional. A chat title was changed to this value.
new_chat_photo (Tuple[:class:`telegram.PhotoSize`]): A chat photo was changed to
this value. This list is empty if the message does not contain a new chat photo.
.. versionchanged:: 20.0
|tupleclassattrs|
delete_chat_photo (:obj:`bool`): Optional. Service message: The chat photo was deleted.
group_chat_created (:obj:`bool`): Optional. Service message: The group has been created.
supergroup_chat_created (:obj:`bool`): Optional. Service message: The supergroup has been
created. This field can't be received in a message coming through updates, because bot
can't be a member of a supergroup when it is created. It can only be found in
:attr:`reply_to_message` if someone replies to a very first message in a directly
created supergroup.
channel_chat_created (:obj:`bool`): Optional. Service message: The channel has been
created. This field can't be received in a message coming through updates, because bot
can't be a member of a channel when it is created. It can only be found in
:attr:`reply_to_message` if someone replies to a very first message in a channel.
message_auto_delete_timer_changed (:class:`telegram.MessageAutoDeleteTimerChanged`):
Optional. Service message: auto-delete timer settings changed in the chat.
.. versionadded:: 13.4
migrate_to_chat_id (:obj:`int`): Optional. The group has been migrated to a supergroup
with the specified identifier.
migrate_from_chat_id (:obj:`int`): Optional. The supergroup has been migrated from a group
with the specified identifier.
pinned_message (:class:`telegram.MaybeInaccessibleMessage`): Optional. Specified message
was pinned. Note that the Message object in this field will not contain further
:attr:`reply_to_message` fields even if it is itself a reply.
.. versionchanged:: 20.8
This attribute now is either class:`telegram.Message` or
class:`telegram.InaccessibleMessage`.
invoice (:class:`telegram.Invoice`): Optional. Message is an invoice for a payment,
information about the invoice.
successful_payment (:class:`telegram.SuccessfulPayment`): Optional. Message is a service
message about a successful payment, information about the payment.
connected_website (:obj:`str`): Optional. The domain name of the website on which the user
has logged in.
author_signature (:obj:`str`): Optional. Signature of the post author for messages in
channels, or the custom title of an anonymous group administrator.
passport_data (:class:`telegram.PassportData`): Optional. Telegram Passport data.
Examples:
:any:`Passport Bot <examples.passportbot>`
poll (:class:`telegram.Poll`): Optional. Message is a native poll,
information about the poll.
dice (:class:`telegram.Dice`): Optional. Message is a dice with random value.
via_bot (:class:`telegram.User`): Optional. Bot through which message was sent.
proximity_alert_triggered (:class:`telegram.ProximityAlertTriggered`): Optional. Service
message. A user in the chat triggered another user's proximity alert while sharing
Live Location.
video_chat_scheduled (:class:`telegram.VideoChatScheduled`): Optional. Service message:
video chat scheduled.
.. versionadded:: 20.0
video_chat_started (:class:`telegram.VideoChatStarted`): Optional. Service message: video
chat started.
.. versionadded:: 20.0
video_chat_ended (:class:`telegram.VideoChatEnded`): Optional. Service message: video chat
ended.
.. versionadded:: 20.0
video_chat_participants_invited (:class:`telegram.VideoChatParticipantsInvited`): Optional.
Service message: new participants invited to a video chat.
.. versionadded:: 20.0
web_app_data (:class:`telegram.WebAppData`): Optional. Service message: data sent by a Web
App.
.. versionadded:: 20.0
reply_markup (:class:`telegram.InlineKeyboardMarkup`): Optional. Inline keyboard attached
to the message. :paramref:`~telegram.InlineKeyboardButton.login_url` buttons are
represented as ordinary url buttons.
is_topic_message (:obj:`bool`): Optional. :obj:`True`, if the message is sent to a forum
topic.
.. versionadded:: 20.0
message_thread_id (:obj:`int`): Optional. Unique identifier of a message thread to which
the message belongs; for supergroups only.
.. versionadded:: 20.0
forum_topic_created (:class:`telegram.ForumTopicCreated`): Optional. Service message:
forum topic created.
.. versionadded:: 20.0
forum_topic_closed (:class:`telegram.ForumTopicClosed`): Optional. Service message:
forum topic closed.
.. versionadded:: 20.0
forum_topic_reopened (:class:`telegram.ForumTopicReopened`): Optional. Service message:
forum topic reopened.
.. versionadded:: 20.0
forum_topic_edited (:class:`telegram.ForumTopicEdited`): Optional. Service message:
forum topic edited.
.. versionadded:: 20.0
general_forum_topic_hidden (:class:`telegram.GeneralForumTopicHidden`): Optional.
Service message: General forum topic hidden.
.. versionadded:: 20.0
general_forum_topic_unhidden (:class:`telegram.GeneralForumTopicUnhidden`): Optional.
Service message: General forum topic unhidden.
.. versionadded:: 20.0
write_access_allowed (:class:`telegram.WriteAccessAllowed`): Optional. Service message:
the user allowed the bot added to the attachment menu to write messages.
.. versionadded:: 20.0
has_media_spoiler (:obj:`bool`): Optional. :obj:`True`, if the message media is covered
by a spoiler animation.
.. versionadded:: 20.0
users_shared (:class:`telegram.UsersShared`): Optional. Service message: users were shared
with the bot
.. versionadded:: 20.8
chat_shared (:class:`telegram.ChatShared`): Optional. Service message: a chat was shared
with the bot.
.. versionadded:: 20.1
giveaway_created (:class:`telegram.GiveawayCreated`): Optional. Service message: a
scheduled giveaway was created
.. versionadded:: 20.8
giveaway (:class:`telegram.Giveaway`): Optional. The message is a scheduled giveaway
message
.. versionadded:: 20.8
giveaway_winners (:class:`telegram.GiveawayWinners`): Optional. A giveaway with public
winners was completed
.. versionadded:: 20.8
giveaway_completed (:class:`telegram.GiveawayCompleted`): Optional. Service message: a
giveaway without public winners was completed
.. versionadded:: 20.8
external_reply (:class:`telegram.ExternalReplyInfo`): Optional. Information about the
message that is being replied to, which may come from another chat or forum topic.
.. versionadded:: 20.8
quote (:class:`telegram.TextQuote`): Optional. For replies that quote part of the original
message, the quoted part of the message.
.. versionadded:: 20.8
forward_origin (:class:`telegram.MessageOrigin`): Optional. Information about the original
message for forwarded messages
.. versionadded:: 20.8
reply_to_story (:class:`telegram.Story`): Optional. For replies to a story, the original
story.
.. versionadded:: 21.0
boost_added (:class:`telegram.ChatBoostAdded`): Optional. Service message: user boosted
the chat.
.. versionadded:: 21.0
sender_boost_count (:obj:`int`): Optional. If the sender of the
message boosted the chat, the number of boosts added by the user.
.. versionadded:: 21.0
business_connection_id (:obj:`str`): Optional. Unique identifier of the business connection
from which the message was received. If non-empty, the message belongs to a chat of the
corresponding business account that is independent from any potential bot chat which
might share the same identifier.
.. versionadded:: 21.1
sender_business_bot (:obj:`telegram.User`): Optional. The bot that actually sent the
message on behalf of the business account. Available only for outgoing messages sent
on behalf of the connected business account.
.. versionadded:: 21.1
.. |custom_emoji_no_md1_support| replace:: Since custom emoji entities are not supported by
:attr:`~telegram.constants.ParseMode.MARKDOWN`, this method now raises a
:exc:`ValueError` when encountering a custom emoji.
.. |blockquote_no_md1_support| replace:: Since block quotation entities are not supported
by :attr:`~telegram.constants.ParseMode.MARKDOWN`, this method now raises a
:exc:`ValueError` when encountering a block quotation.
.. |reply_same_thread| replace:: If :paramref:`message_thread_id` is not provided,
this will reply to the same thread (topic) of the original message.
"""
# fmt: on
__slots__ = (
"_effective_attachment",
"animation",
"audio",
"author_signature",
"boost_added",
"business_connection_id",
"caption",
"caption_entities",
"channel_chat_created",
"chat_shared",
"connected_website",
"contact",
"delete_chat_photo",
"dice",
"document",
"edit_date",
"entities",
"external_reply",
"forum_topic_closed",
"forum_topic_created",
"forum_topic_edited",
"forum_topic_reopened",
"forward_origin",
"from_user",
"game",
"general_forum_topic_hidden",
"general_forum_topic_unhidden",
"giveaway",
"giveaway_completed",
"giveaway_created",
"giveaway_winners",
"group_chat_created",
"has_media_spoiler",
"has_protected_content",
"invoice",
"is_automatic_forward",
"is_from_offline",
"is_topic_message",
"left_chat_member",
"link_preview_options",
"location",
"media_group_id",
"message_auto_delete_timer_changed",
"message_thread_id",
"migrate_from_chat_id",
"migrate_to_chat_id",
"new_chat_members",
"new_chat_photo",
"new_chat_title",
"passport_data",
"photo",
"pinned_message",
"poll",
"proximity_alert_triggered",
"quote",
"reply_markup",
"reply_to_message",
"reply_to_story",
"sender_boost_count",
"sender_business_bot",
"sender_chat",
"sticker",
"story",
"successful_payment",
"supergroup_chat_created",
"text",
"users_shared",
"venue",
"via_bot",
"video",
"video_chat_ended",
"video_chat_participants_invited",
"video_chat_scheduled",
"video_chat_started",
"video_note",
"voice",
"web_app_data",
"write_access_allowed",
)
def __init__(
self,
message_id: int,
date: datetime.datetime,
chat: Chat,
from_user: Optional[User] = None,
reply_to_message: Optional["Message"] = None,
edit_date: Optional[datetime.datetime] = None,
text: Optional[str] = None,
entities: Optional[Sequence["MessageEntity"]] = None,
caption_entities: Optional[Sequence["MessageEntity"]] = None,
audio: Optional[Audio] = None,
document: Optional[Document] = None,
game: Optional[Game] = None,
photo: Optional[Sequence[PhotoSize]] = None,
sticker: Optional[Sticker] = None,
video: Optional[Video] = None,
voice: Optional[Voice] = None,
video_note: Optional[VideoNote] = None,
new_chat_members: Optional[Sequence[User]] = None,
caption: Optional[str] = None,
contact: Optional[Contact] = None,
location: Optional[Location] = None,
venue: Optional[Venue] = None,
left_chat_member: Optional[User] = None,
new_chat_title: Optional[str] = None,
new_chat_photo: Optional[Sequence[PhotoSize]] = None,
delete_chat_photo: Optional[bool] = None,
group_chat_created: Optional[bool] = None,
supergroup_chat_created: Optional[bool] = None,
channel_chat_created: Optional[bool] = None,
migrate_to_chat_id: Optional[int] = None,
migrate_from_chat_id: Optional[int] = None,
pinned_message: Optional[MaybeInaccessibleMessage] = None,
invoice: Optional[Invoice] = None,
successful_payment: Optional[SuccessfulPayment] = None,
author_signature: Optional[str] = None,
media_group_id: Optional[str] = None,
connected_website: Optional[str] = None,
animation: Optional[Animation] = None,
passport_data: Optional[PassportData] = None,
poll: Optional[Poll] = None,
reply_markup: Optional[InlineKeyboardMarkup] = None,
dice: Optional[Dice] = None,
via_bot: Optional[User] = None,
proximity_alert_triggered: Optional[ProximityAlertTriggered] = None,
sender_chat: Optional[Chat] = None,
video_chat_started: Optional[VideoChatStarted] = None,
video_chat_ended: Optional[VideoChatEnded] = None,
video_chat_participants_invited: Optional[VideoChatParticipantsInvited] = None,
message_auto_delete_timer_changed: Optional[MessageAutoDeleteTimerChanged] = None,