telegram.Message¶
-
class
telegram.
Message
(message_id, from_user, date, chat, forward_from=None, forward_from_chat=None, forward_from_message_id=None, forward_date=None, reply_to_message=None, edit_date=None, text=None, entities=None, caption_entities=None, audio=None, document=None, game=None, photo=None, sticker=None, video=None, voice=None, video_note=None, new_chat_members=None, caption=None, contact=None, location=None, venue=None, left_chat_member=None, new_chat_title=None, new_chat_photo=None, delete_chat_photo=False, group_chat_created=False, supergroup_chat_created=False, channel_chat_created=False, migrate_to_chat_id=None, migrate_from_chat_id=None, pinned_message=None, invoice=None, successful_payment=None, forward_signature=None, author_signature=None, media_group_id=None, connected_website=None, animation=None, passport_data=None, poll=None, forward_sender_name=None, reply_markup=None, bot=None, **kwargs)¶ Bases:
telegram.base.TelegramObject
This object represents a message.
Note
- In Python from is a reserved word, use from_user instead.
-
message_id
¶ Unique message identifier inside this chat.
Type: int
-
from_user
¶ Optional. Sender.
Type: telegram.User
-
date
¶ Date the message was sent.
Type: datetime.datetime
-
chat
¶ Conversation the message belongs to.
Type: telegram.Chat
-
forward_from
¶ Optional. Sender of the original message.
Type: telegram.User
-
forward_from_chat
¶ Optional. Information about the original channel.
Type: telegram.Chat
-
forward_from_message_id
¶ Optional. Identifier of the original message in the channel.
Type: int
-
forward_date
¶ Optional. Date the original message was sent.
Type: datetime.datetime
-
reply_to_message
¶ Optional. The original message.
Type: telegram.Message
-
edit_date
¶ Optional. Date the message was last edited.
Type: datetime.datetime
-
media_group_id
¶ Optional. The unique identifier of a media message group this message belongs to.
Type: str
-
text
¶ Optional. The actual UTF-8 text of the message.
Type: str
-
entities
¶ Optional. Special entities like usernames, URLs, bot commands, etc. that appear in the text. See
Message.parse_entity
andparse_entities
methods for how to use properly.Type: List[ telegram.MessageEntity
]
Optional. Special entities like usernames, URLs, bot commands, etc. that appear in the caption. See
Message.parse_caption_entity
andparse_caption_entities
methods for how to use properly.Type: List[ telegram.MessageEntity
]
-
audio
¶ Optional. Information about the file.
Type: telegram.Audio
-
document
¶ Optional. Information about the file.
Type: telegram.Document
-
animation
¶ For backward compatibility, when this field is set, the document field will also be set.
Type: telegram.Animation
-
game
¶ Optional. Information about the game.
Type: telegram.Game
-
photo
¶ Optional. Available sizes of the photo.
Type: List[ telegram.PhotoSize
]
-
sticker
¶ Optional. Information about the sticker.
Type: telegram.Sticker
-
video
¶ Optional. Information about the video.
Type: telegram.Video
-
voice
¶ Optional. Information about the file.
Type: telegram.Voice
-
video_note
¶ Optional. Information about the video message.
Type: telegram.VideoNote
-
new_chat_members
¶ Optional. Information about new members to the chat. (the bot itself may be one of these members).
Type: List[ telegram.User
]
Optional. Caption for the document, photo or video, 0-1024 characters.
Type: str
-
contact
¶ Optional. Information about the contact.
Type: telegram.Contact
-
location
¶ Optional. Information about the location.
Type: telegram.Location
-
venue
¶ Optional. Information about the venue.
Type: telegram.Venue
-
left_chat_member
¶ Optional. Information about the user that left the group. (this member may be the bot itself).
Type: telegram.User
-
new_chat_title
¶ Optional. A chat title was changed to this value.
Type: str
-
new_chat_photo
¶ Optional. A chat photo was changed to this value.
Type: List[ telegram.PhotoSize
]
-
delete_chat_photo
¶ Optional. The chat photo was deleted.
Type: bool
-
group_chat_created
¶ Optional. The group has been created.
Type: bool
-
supergroup_chat_created
¶ Optional. The supergroup has been created.
Type: bool
-
channel_chat_created
¶ Optional. The channel has been created.
Type: bool
-
migrate_to_chat_id
¶ Optional. The group has been migrated to a supergroup with the specified identifier.
Type: int
-
migrate_from_chat_id
¶ Optional. The supergroup has been migrated from a group with the specified identifier.
Type: int
-
pinned_message
¶ Optional. Specified message was pinned.
Type: telegram.message
-
invoice
¶ Optional. Information about the invoice.
Type: telegram.Invoice
-
successful_payment
¶ Optional. Information about the payment.
Type: telegram.SuccessfulPayment
-
connected_website
¶ Optional. The domain name of the website on which the user has logged in.
Type: str
-
forward_signature
¶ Optional. Signature of the post author for messages forwarded from channels.
Type: str
-
forward_sender_name
¶ Optional. Sender’s name for messages forwarded from users who disallow adding a link to their account in forwarded messages.
Type: str
Optional. Signature of the post author for messages in channels.
Type: str
-
passport_data
¶ Optional. Telegram Passport data.
Type: telegram.PassportData
-
poll
¶ Optional. Message is a native poll, information about the poll.
Type: telegram.Poll
-
reply_markup
¶ Optional. Inline keyboard attached to the message.
Type: telegram.InlineKeyboardMarkup
-
bot
¶ Optional. The Bot to use for instance methods.
Type: telegram.Bot
Parameters: - message_id (
int
) – Unique message identifier inside this chat. - from_user (
telegram.User
, optional) – Sender, can be empty for messages sent to channels. - date (
datetime.datetime
) – Date the message was sent in Unix time. Converted todatetime.datetime
. - chat (
telegram.Chat
) – Conversation the message belongs to. - forward_from (
telegram.User
, optional) – For forwarded messages, sender of the original message. - forward_from_chat (
telegram.Chat
, optional) – For messages forwarded from a channel, information about the original channel. - forward_from_message_id (
int
, optional) – For forwarded channel posts, identifier of the original message in the channel. - forward_sender_name (
str
, optional) – Sender’s name for messages forwarded from users who disallow adding a link to their account in forwarded messages. - forward_date (
datetime.datetime
, optional) – For forwarded messages, date the original message was sent in Unix time. Converted todatetime.datetime
. - reply_to_message (
telegram.Message
, optional) – For replies, the original message. Note that the Message object in this field will not contain furtherreply_to_message
fields even if it itself is a reply. - edit_date (
datetime.datetime
, optional) – Date the message was last edited in Unix time. Converted todatetime.datetime
. - media_group_id (
str
, optional) – The unique identifier of a media message group this message belongs to. - text (str, optional) – For text messages, the actual UTF-8 text of the message, 0-4096
characters. Also found as
telegram.constants.MAX_MESSAGE_LENGTH
. - entities (List[
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. - caption_entities (List[
telegram.MessageEntity
]) – Optional. For Messages with a Caption. Special entities like usernames, URLs, bot commands, etc. that appear in the caption. SeeMessage.parse_caption_entity
andparse_caption_entities
methods for how to use properly. - audio (
telegram.Audio
, optional) – Message is an audio file, information about the file. - document (
telegram.Document
, optional) – Message is a general file, information about the file. - animation (
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 (
telegram.Game
, optional) – Message is a game, information about the game. - photo (List[
telegram.PhotoSize
], optional) – Message is a photo, available sizes of the photo. - sticker (
telegram.Sticker
, optional) – Message is a sticker, information about the sticker. - video (
telegram.Video
, optional) – Message is a video, information about the video. - voice (
telegram.Voice
, optional) – Message is a voice message, information about the file. - video_note (
telegram.VideoNote
, optional) – Message is a video note, information about the video message. - new_chat_members (List[
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). - caption (
str
, optional) – Caption for the document, photo or video, 0-1024 characters. - contact (
telegram.Contact
, optional) – Message is a shared contact, information about the contact. - location (
telegram.Location
, optional) – Message is a shared location, information about the location. - venue (
telegram.Venue
, optional) – Message is a venue, information about the venue. - left_chat_member (
telegram.User
, optional) – A member was removed from the group, information about them (this member may be the bot itself). - new_chat_title (
str
, optional) – A chat title was changed to this value. - new_chat_photo (List[
telegram.PhotoSize
], optional) – A chat photo was change to this value. - delete_chat_photo (
bool
, optional) – Service message: The chat photo was deleted. - group_chat_created (
bool
, optional) – Service message: The group has been created. - supergroup_chat_created (
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 inreply_to_message
if someone replies to a very first message in a directly created supergroup. - channel_chat_created (
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. - migrate_to_chat_id (
int
, optional) – The group has been migrated to a supergroup with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. - migrate_from_chat_id (
int
, optional) – The supergroup has been migrated from a group with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. - pinned_message (
telegram.message
, 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. - invoice (
telegram.Invoice
, optional) – Message is an invoice for a payment, information about the invoice. - successful_payment (
telegram.SuccessfulPayment
, optional) – Message is a service message about a successful payment, information about the payment. - connected_website (
str
, optional) – The domain name of the website on which the user has logged in. - forward_signature (
str
, optional) – Signature of the post author for messages forwarded from channels. - author_signature (
str
, optional) – Signature of the post author for messages in channels. - passport_data (
telegram.PassportData
, optional) – Telegram Passport data. - poll (
telegram.Poll
, optional) – Message is a native poll, information about the poll. - reply_markup (
telegram.InlineKeyboardMarkup
, optional) – Inline keyboard attached to the message. login_url buttons are represented as ordinary url buttons.
Creates an HTML-formatted string from the markup entities found in the message’s caption.
Use this if you want to retrieve the message caption with the caption entities formatted as HTML in the same way the original message was formatted.
Returns: Message caption with captionentities formatted as HTML. Return type: str
Creates an HTML-formatted string from the markup entities found in the message’s caption.
Use this if you want to retrieve the message caption with the caption entities formatted as HTML. This also formats
telegram.MessageEntity.URL
as a hyperlink.Returns: Message caption with caption entities formatted as HTML. Return type: str
Creates an Markdown-formatted string from the markup entities found in the message’s caption.
Use this if you want to retrieve the message caption with the caption entities formatted as Markdown in the same way the original message was formatted.
Returns: Message caption with caption entities formatted as Markdown. Return type: str
Creates an Markdown-formatted string from the markup entities found in the message’s caption.
Use this if you want to retrieve the message caption with the caption entities formatted as Markdown. This also formats
telegram.MessageEntity.URL
as a hyperlink.Returns: Message caption with caption entities formatted as Markdown. Return type: str
-
chat_id
¶ Shortcut for
telegram.Chat.id
forchat
.Type: int
-
delete
(*args, **kwargs)¶ Shortcut for:
bot.delete_message(chat_id=message.chat_id, message_id=message.message_id, *args, **kwargs)
Returns: On success, True
is returned.Return type: bool
Shortcut for:
bot.edit_message_caption(chat_id=message.chat_id, message_id=message.message_id, *args, **kwargs)
Note
You can only edit messages that the bot sent itself, therefore this method can only be used on the return value of the
bot.send_*
family of methods.Returns: On success, instance representing the edited message. Return type: telegram.Message
-
edit_media
(media, *args, **kwargs)¶ Shortcut for:
bot.edit_message_media(chat_id=message.chat_id, message_id=message.message_id, *args, **kwargs)
Note
You can only edit messages that the bot sent itself, therefore this method can only be used on the return value of the
bot.send_*
family of methods.Returns: On success, instance representing the edited message. Return type: telegram.Message
-
edit_reply_markup
(*args, **kwargs)¶ Shortcut for:
bot.edit_message_reply_markup(chat_id=message.chat_id, message_id=message.message_id, *args, **kwargs)
Note
You can only edit messages that the bot sent itself, therefore this method can only be used on the return value of the
bot.send_*
family of methods.Returns: On success, instance representing the edited message. Return type: telegram.Message
-
edit_text
(*args, **kwargs)¶ Shortcut for:
bot.edit_message_text(chat_id=message.chat_id, message_id=message.message_id, *args, **kwargs)
Note
You can only edit messages that the bot sent itself, therefore this method can only be used on the return value of the
bot.send_*
family of methods.Returns: On success, instance representing the edited message. Return type: telegram.Message
-
effective_attachment
¶ telegram.Audio
ortelegram.Contact
ortelegram.Document
ortelegram.Animation
ortelegram.Game
ortelegram.Invoice
ortelegram.Location
or List[telegram.PhotoSize
] ortelegram.Sticker
ortelegram.SuccessfulPayment
ortelegram.Venue
ortelegram.Video
ortelegram.VideoNote
ortelegram.Voice
: The attachment that this message was sent with. May beNone
if no attachment was sent.
-
forward
(chat_id, disable_notification=False)¶ Shortcut for:
bot.forward_message(chat_id=chat_id, from_chat_id=update.message.chat_id, disable_notification=disable_notification, message_id=update.message.message_id)
Returns: On success, instance representing the message forwarded. Return type: telegram.Message
-
link
¶ Convenience property. If the chat of the message is a supergroup or a channel and has a
Chat.username
, returns a t.me link of the message.Type: str
Returns a
dict
that mapstelegram.MessageEntity
tostr
. It contains entities from this message’s caption filtered by theirtelegram.MessageEntity.type
attribute as the key, and the text that each entity belongs to as the value of thedict
.Note
This method should always be used instead of the
caption_entities
attribute, since it calculates the correct substring from the message text based on UTF-16 codepoints. Seeparse_entity
for more info.Parameters: types (List[ str
], optional) – List oftelegram.MessageEntity
types as strings. If thetype
attribute of an entity is contained in this list, it will be returned. Defaults to a list of all types. All types can be found as constants intelegram.MessageEntity
.Returns: A dictionary of entities mapped to the text that belongs to them, calculated based on UTF-16 codepoints. Return type: Dict[ telegram.MessageEntity
,str
]
Returns the text from a given
telegram.MessageEntity
.Note
This method is present because Telegram calculates the offset and length in UTF-16 codepoint pairs, which some versions of Python don’t handle automatically. (That is, you can’t just slice
Message.caption
with the offset and length.)Parameters: - entity (
telegram.MessageEntity
) – The entity to extract the text from. It must - an entity that belongs to this message. (be) –
Returns: The text of the given entity
Return type: str
- entity (
-
parse_entities
(types=None)¶ Returns a
dict
that mapstelegram.MessageEntity
tostr
. It contains entities from this message filtered by theirtelegram.MessageEntity.type
attribute as the key, and the text that each entity belongs to as the value of thedict
.Note
This method should always be used instead of the
entities
attribute, since it calculates the correct substring from the message text based on UTF-16 codepoints. Seeparse_entity
for more info.Parameters: types (List[ str
], optional) – List oftelegram.MessageEntity
types as strings. If thetype
attribute of an entity is contained in this list, it will be returned. Defaults to a list of all types. All types can be found as constants intelegram.MessageEntity
.Returns: A dictionary of entities mapped to the text that belongs to them, calculated based on UTF-16 codepoints. Return type: Dict[ telegram.MessageEntity
,str
]
-
parse_entity
(entity)¶ Returns the text from a given
telegram.MessageEntity
.Note
This method is present because Telegram calculates the offset and length in UTF-16 codepoint pairs, which some versions of Python don’t handle automatically. (That is, you can’t just slice
Message.text
with the offset and length.)Parameters: - entity (
telegram.MessageEntity
) – The entity to extract the text from. It must - an entity that belongs to this message. (be) –
Returns: The text of the given entity
Return type: str
- entity (
-
reply_animation
(*args, **kwargs)¶ Shortcut for:
bot.send_animation(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_audio
(*args, **kwargs)¶ Shortcut for:
bot.send_audio(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_contact
(*args, **kwargs)¶ Shortcut for:
bot.send_contact(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_document
(*args, **kwargs)¶ Shortcut for:
bot.send_document(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_html
(*args, **kwargs)¶ Shortcut for:
bot.send_message(update.message.chat_id, parse_mode=ParseMode.HTML, *args, **kwargs)
Sends a message with HTML formatting.
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the message is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.
-
reply_location
(*args, **kwargs)¶ Shortcut for:
bot.send_location(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_markdown
(*args, **kwargs)¶ Shortcut for:
bot.send_message(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, *args, **kwargs)
Sends a message with markdown formatting.
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the message is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.
-
reply_media_group
(*args, **kwargs)¶ Shortcut for:
bot.reply_media_group(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the media group is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: An array of the sent Messages. Return type: List[ telegram.Message
]Raises: telegram.TelegramError
-
reply_photo
(*args, **kwargs)¶ Shortcut for:
bot.send_photo(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_poll
(*args, **kwargs)¶ Shortcut for:
bot.send_poll(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_sticker
(*args, **kwargs)¶ Shortcut for:
bot.send_sticker(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_text
(*args, **kwargs)¶ Shortcut for:
bot.send_message(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the message is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.
-
reply_venue
(*args, **kwargs)¶ Shortcut for:
bot.send_venue(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_video
(*args, **kwargs)¶ Shortcut for:
bot.send_video(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_video_note
(*args, **kwargs)¶ Shortcut for:
bot.send_video_note(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
reply_voice
(*args, **kwargs)¶ Shortcut for:
bot.send_voice(update.message.chat_id, *args, **kwargs)
Keyword Arguments: quote ( bool
, optional) – If set toTrue
, the photo is sent as an actual reply to this message. Ifreply_to_message_id
is passed inkwargs
, this parameter will be ignored. Default:True
in group chats andFalse
in private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
text_html
¶ Creates an HTML-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as HTML in the same way the original message was formatted.
Returns: Message text with entities formatted as HTML. Return type: str
-
text_html_urled
¶ Creates an HTML-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as HTML. This also formats
telegram.MessageEntity.URL
as a hyperlink.Returns: Message text with entities formatted as HTML. Return type: str
-
text_markdown
¶ Creates an Markdown-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as Markdown in the same way the original message was formatted.
Returns: Message text with entities formatted as Markdown. Return type: str
-
text_markdown_urled
¶ Creates an Markdown-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as Markdown. This also formats
telegram.MessageEntity.URL
as a hyperlink.Returns: Message text with entities formatted as Markdown. Return type: str