telegram.message module

This module contains an object that represents a Telegram Message.

class telegram.message.Message(message_id, from_user, date, chat, forward_from=None, forward_from_chat=None, forward_date=None, reply_to_message=None, edit_date=None, text=None, entities=None, audio=None, document=None, photo=None, sticker=None, video=None, voice=None, caption=None, contact=None, location=None, venue=None, new_chat_member=None, new_chat_members=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, migrate_to_chat_id=None, migrate_from_chat_id=None, channel_chat_created=False, pinned_message=None, forward_from_message_id=None, bot=None, video_note=None, **kwargs)

Bases: telegram.base.TelegramObject

This object represents a Telegram Message.

Note

  • In Python from is a reserved word, use from_user instead.
message_id

int

from_user

telegram.User

date

datetime.datetime

forward_from

telegram.User

forward_from_chat

telegram.Chat

forward_from_message_id

int

forward_date

datetime.datetime

reply_to_message

telegram.Message

edit_date

datetime.datetime

text

str

audio

telegram.Audio

document

telegram.Document

game

telegram.Game

photo

List[telegram.PhotoSize]

sticker

telegram.Sticker

video

telegram.Video

voice

telegram.Voice

video_note

telegram.VideoNote – Message is a video note, information about the video message

caption

str

contact

telegram.Contact

location

telegram.Location

new_chat_member

telegram.User

left_chat_member

telegram.User

new_chat_title

str

new_chat_photo

List[telegram.PhotoSize]

delete_chat_photo

bool

group_chat_created

bool

supergroup_chat_created

bool

migrate_to_chat_id

int

migrate_from_chat_id

int

channel_chat_created

bool

Deprecated: 4.0

new_chat_participant (telegram.User): Use new_chat_member instead.

left_chat_participant (telegram.User): Use left_chat_member instead.

Parameters:
chat_id

int – Short for Message.chat.id

static de_json(data, bot)
Parameters:
Returns:

Return type:

telegram.Message

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.editMessageCaption(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.

edit_reply_markup(*args, **kwargs)

Shortcut for

>>> bot.editReplyMarkup(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.

edit_text(*args, **kwargs)

Shortcut for

>>> bot.editMessageText(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.

forward(chat_id, disable_notification=False)

Shortcut for

>>> bot.forwardMessage(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
parse_entities(types=None)

Returns a dict that maps telegram.MessageEntity to str. It contains entities from this message filtered by their 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 get_entity_text for more info.

Parameters:types (Optional[list]) – 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 be an entity that belongs to this message.
Returns:The text of the given entity
Return type:str
reply_audio(*args, **kwargs)

Shortcut for bot.sendAudio(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – If set to True, the audio 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.sendContact(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – If set to True, the contact 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.sendDocument(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – If set to True, the document 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.sendLocation(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – If set to True, the location 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.sendPhoto(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – 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.sendSticker(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – If set to True, the sticker 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.sendMessage(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – 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.sendVenue(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – If set to True, the venue 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.sendVideo(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – If set to True, the video 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 (Optional[bool]) – If set to True, the video 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.sendVoice(update.message.chat_id, *args, **kwargs)

Keyword Arguments:
 quote (Optional[bool]) – If set to True, the voice 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 (uses parse_entities).

Use this if you want to retrieve the original string sent by the bot, as opposed to the plain text with corresponding markup entities.

Returns:str
text_markdown

Creates a markdown-formatted string from the markup entities found in the message (uses parse_entities).

Use this if you want to retrieve the original string sent by the bot, as opposed to the plain text with corresponding markup entities.

Returns:str
to_dict()
Returns:
Return type:dict