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='', entities=None, audio=None, document=None, photo=None, sticker=None, video=None, voice=None, caption='', contact=None, location=None, venue=None, new_chat_member=None, left_chat_member=None, new_chat_title='', new_chat_photo=None, delete_chat_photo=False, group_chat_created=False, supergroup_chat_created=False, migrate_to_chat_id=0, migrate_from_chat_id=0, channel_chat_created=False, pinned_message=None, forward_from_message_id=None, bot=None, **kwargs)¶ Bases:
telegram.base.TelegramObjectThis object represents a Telegram Message.
Note
- In Python from is a reserved word, use from_user instead.
-
message_id¶ int
-
from_user¶
-
date¶ datetime.datetime
-
forward_from¶
-
forward_from_chat¶
-
forward_from_message_id¶ int
-
forward_date¶ datetime.datetime
-
reply_to_message¶
-
edit_date¶ datetime.datetime
-
text¶ str
-
audio¶
-
document¶
-
game¶
-
photo¶ List[
telegram.PhotoSize]
-
sticker¶
-
video¶
-
voice¶
-
caption¶ str
-
contact¶
-
location¶
-
new_chat_member¶
-
left_chat_member¶
-
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: - message_id (int) –
- from_user (
telegram.User) – - date (
datetime.datetime) – - chat (
telegram.Chat) – - forward_from (Optional[
telegram.User]) – - forward_from_chat (Optional[
telegram.Chat]) – - forward_from_message_id (Optional[int]) –
- forward_date (Optional[
datetime.datetime]) – - reply_to_message (Optional[
telegram.Message]) – - edit_date (Optional[
datetime.datetime]) – - text (Optional[str]) –
- audio (Optional[
telegram.Audio]) – - document (Optional[
telegram.Document]) – - game (Optional[
telegram.Game]) – - photo (Optional[List[
telegram.PhotoSize]]) – - sticker (Optional[
telegram.Sticker]) – - video (Optional[
telegram.Video]) – - voice (Optional[
telegram.Voice]) – - caption (Optional[str]) –
- contact (Optional[
telegram.Contact]) – - location (Optional[
telegram.Location]) – - new_chat_member (Optional[
telegram.User]) – - left_chat_member (Optional[
telegram.User]) – - new_chat_title (Optional[str]) –
- new_chat_photo (Optional[List[
telegram.PhotoSize]) – - delete_chat_photo (Optional[bool]) –
- group_chat_created (Optional[bool]) –
- supergroup_chat_created (Optional[bool]) –
- migrate_to_chat_id (Optional[int]) –
- migrate_from_chat_id (Optional[int]) –
- channel_chat_created (Optional[bool]) –
- bot (Optional[Bot]) – The Bot to use for instance methods
-
chat_id¶ int – Short for
Message.chat.id
-
static
de_json(data, bot)¶ Parameters: - data (dict) –
- bot (telegram.Bot) –
Returns: Return type:
-
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
-
parse_entities(types=None)¶ Returns a
dictthat mapstelegram.MessageEntitytostr. It contains entities from this message filtered by theirtypeattribute 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
entitiesattribute, since it calculates the correct substring from the message text based on UTF-16 codepoints. Seeget_entity_textfor more info.Parameters: types (Optional[list]) – List of telegram.MessageEntitytypes as strings. If thetypeattribute 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.textwith 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein 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. Ifreply_to_message_idis passed inkwargs, this parameter will be ignored. Default:Truein group chats andFalsein private chats.Returns: On success, instance representing the message posted. Return type: telegram.Message
-
to_dict()¶ Returns: Return type: dict