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, new_chat_member=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, 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

int – Unique message identifier inside this chat.

from_user

telegram.User – Optional. Sender.

date

datetime.datetime – Date the message was sent.

chat

telegram.Chat – Conversation the message belongs to.

forward_from

telegram.User – Optional. Sender of the original message.

forward_from_chat

telegram.Chat – Optional. Information about the original channel.

forward_from_message_id

int – Optional. Identifier of the original message in the channel.

forward_date

datetime.datetime – Optional. Date the original message was sent.

reply_to_message

telegram.Message – Optional. The original message.

edit_date

datetime.datetime – Optional. Date the message was last edited.

text

str – Optional. The actual UTF-8 text of the message.

entities

List[telegram.MessageEntity] – Optional. Special entities like usernames, URLs, bot commands, etc. that appear in the text. See Message.parse_entity and parse_entities methods for how to use properly.

caption_entities

List[telegram.MessageEntity] – Optional. Special entities like usernames, URLs, bot commands, etc. that appear in the caption. See Message.parse_caption_entity and parse_caption_entities methods for how to use properly.

audio

telegram.Audio – Optional. Information about the file.

document

telegram.Document – Optional. Information about the file.

game

telegram.Game – Optional. Information about the game.

photo

List[telegram.PhotoSize] – Optional. Available sizes of the photo.

sticker

telegram.Sticker – Optional. Information about the sticker.

video

telegram.Video – Optional. Information about the video.

voice

telegram.Voice – Optional. Information about the file.

video_note

telegram.VideoNote – Optional. Information about the video message.

new_chat_members

List[telegram.User] – Optional. Information about new members to the chat. (the bot itself may be one of these members).

caption

str – Optional. Caption for the document, photo or video, 0-200 characters.

contact

telegram.Contact – Optional. Information about the contact.

location

telegram.Location – Optional. Information about the location.

venue

telegram.Venue – Optional. Information about the venue.

left_chat_member

telegram.User – Optional. Information about the user that left the group. (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 changed to this value.

delete_chat_photo

bool – Optional. The chat photo was deleted.

group_chat_created

bool – Optional. The group has been created.

supergroup_chat_created

bool – Optional. The supergroup has been created.

channel_chat_created

bool – Optional. The channel has been created.

migrate_to_chat_id

int – Optional. The group has been migrated to a supergroup with the specified identifier.

migrate_from_chat_id

int – Optional. The supergroup has been migrated from a group with the specified identifier.

pinned_message

telegram.message – Optional. Specified message was pinned.

invoice

telegram.Invoice – Optional. Information about the invoice.

successful_payment

telegram.SuccessfulPayment – Optional. Information about the payment.

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.

bot

telegram.Bot – Optional. The Bot to use for instance methods.

Deprecated: 6.0
new_chat_member (telegram.User): Replaced with new_chat_members
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 to datetime.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_date (datetime.datetime, optional) – For forwarded messages, date the original message was sent in Unix time. Converted to datetime.datetime.
  • reply_to_message (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 (datetime.datetime, optional) – Date the message was last edited in Unix time. Converted to datetime.datetime.
  • 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. See Message.parse_caption_entity and parse_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.
  • 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-200 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 in reply_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.
  • 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.
chat_id

int – Shortcut for telegram.Chat.id for chat.

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
edit_caption(*args, **kwargs)

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_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 or telegram.Contact or telegram.Document or telegram.Game or telegram.Invoice or telegram.Location or List[telegram.PhotoSize] or telegram.Sticker or telegram.SuccessfulPayment or telegram.Venue or telegram.Video or telegram.VideoNote or telegram.Voice: The attachment that this message was sent with. May be None 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
new_chat_member

Deprecated

parse_caption_entities(types=None)

Returns a dict that maps telegram.MessageEntity to str. It contains entities from this message’s caption filtered by their telegram.MessageEntity.type attribute as the key, and the text that each entity belongs to as the value of the dict.

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. See parse_entity for more info.

Parameters:types (List[str], optional) – List of telegram.MessageEntity types as strings. If the type 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 in telegram.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_caption_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.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

parse_entities(types=None)

Returns a dict that maps telegram.MessageEntity to str. It contains entities from this message filtered by their telegram.MessageEntity.type attribute as the key, and the text that each entity belongs to as the value of the dict.

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. See parse_entity for more info.

Parameters:types (List[str], optional) – List of telegram.MessageEntity types as strings. If the type 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 in telegram.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

reply_audio(*args, **kwargs)

Shortcut for:

bot.send_audio(update.message.chat_id, *args, **kwargs)
Keyword Arguments:
 quote (bool, optional) – If set to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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 to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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 to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False in private chats.
Returns:On success, instance representing the message posted.
Return type:telegram.Message
reply_location(*args, **kwargs)

Shortcut for:

bot.send_location(update.message.chat_id, *args, **kwargs)
Keyword Arguments:
 quote (bool, optional) – If set to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False in private chats.
Returns:On success, instance representing the message posted.
Return type:telegram.Message
reply_photo(*args, **kwargs)

Shortcut for:

bot.send_photo(update.message.chat_id, *args, **kwargs)
Keyword Arguments:
 quote (bool, optional) – If set to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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 to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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 to True, the message is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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 to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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 to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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 to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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 to True, the photo is sent as an actual reply to this message. If reply_to_message_id is passed in kwargs, this parameter will be ignored. Default: True in group chats and False 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