Reference
Resources
arbitrarycallbackdatabot.py
chatmemberbot.py
contexttypesbot.py
conversationbot.py
conversationbot2.py
customwebhookbot.py
deeplinking.py
echobot.py
errorhandlerbot.py
inlinebot.py
inlinekeyboard.py
inlinekeyboard2.py
nestedconversationbot.py
passportbot.py
paymentbot.py
persistentconversationbot.py
pollbot.py
timerbot.py
webappbot.py
Project
Bases:telegram.TelegramObject
This object represents one special entity in a text message. For example, hashtags,usernames, URLs, etc.
Objects of this class are comparable in terms of equality. Two objects of this class areconsidered equal, if theirtype
,offset
andlength
are equal.
Use In
Available In
Type of the entity. Can beMENTION
(@username
),HASHTAG
(#hashtag
or#hashtag@chatusername
),CASHTAG
($USD
orUSD@chatusername
),BOT_COMMAND
(/start@jobs_bot
),URL
(https://telegram.org
),EMAIL
(do-not-reply@telegram.org
),PHONE_NUMBER
(+1-212-555-0123
),BOLD
(bold text),ITALIC
(italic text),UNDERLINE
(underlined text),STRIKETHROUGH
,SPOILER
(spoiler message),BLOCKQUOTE
(block quotation),CODE
(monowidth string),PRE
(monowidth block),TEXT_LINK
(for clickable text URLs),TEXT_MENTION
(for users without usernames),CUSTOM_EMOJI
(for inline custom emoji stickers).
Added in version 20.0:Added inline custom emoji
Added in version 20.8:Added block quotation
offset (int
) – Offset in UTF-16 code units to the start of the entity.
url (str
, optional) – ForTEXT_LINK
only, url that will be opened afteruser taps on the text.
user (telegram.User
, optional) – ForTEXT_MENTION
only, the mentioneduser.
language (str
, optional) – ForPRE
only, the programming language ofthe entity text.
custom_emoji_id (str
, optional) –
ForCUSTOM_EMOJI
only, unique identifierof the custom emoji. Usetelegram.Bot.get_custom_emoji_stickers()
to get fullinformation about the sticker.
Added in version 20.0.
Type of the entity. Can beMENTION
(@username
),HASHTAG
(#hashtag
or#hashtag@chatusername
),CASHTAG
($USD
orUSD@chatusername
),BOT_COMMAND
(/start@jobs_bot
),URL
(https://telegram.org
),EMAIL
(do-not-reply@telegram.org
),PHONE_NUMBER
(+1-212-555-0123
),BOLD
(bold text),ITALIC
(italic text),UNDERLINE
(underlined text),STRIKETHROUGH
,SPOILER
(spoiler message),BLOCKQUOTE
(block quotation),CODE
(monowidth string),PRE
(monowidth block),TEXT_LINK
(for clickable text URLs),TEXT_MENTION
(for users without usernames),CUSTOM_EMOJI
(for inline custom emoji stickers).
Added in version 20.0:Added inline custom emoji
Added in version 20.8:Added block quotation
Optional. ForTEXT_MENTION
only, the mentioneduser.
Optional. ForCUSTOM_EMOJI
only, unique identifierof the custom emoji. Usetelegram.Bot.get_custom_emoji_stickers()
to get fullinformation about the sticker.
Added in version 20.0.
A list of all available message entity types.
list[str
]
telegram.constants.MessageEntityType.BLOCKQUOTE
Added in version 20.8.
telegram.constants.MessageEntityType.CUSTOM_EMOJI
Added in version 20.0.
telegram.constants.MessageEntityType.EXPANDABLE_BLOCKQUOTE
Added in version 21.3.
telegram.constants.MessageEntityType.SPOILER
Added in version 13.10.
Utility functionality for converting the offset and length of entities fromUnicode (str
) to UTF-16 (utf-16-le
encodedbytes
).
Tip
Only the offsets and lengths calulated in UTF-16 is acceptable by the Telegram Bot API.If they are calculated using the Unicode string (str
object), errors may occurwhen the text contains characters that are not in the Basic Multilingual Plane (BMP).For more information, seeUnicode andPlane (Unicode).
Added in version 21.4.
Examples
Below is a snippet of code that demonstrates how to use this function to convertentities from Unicode to UTF-16 space. Theunicode_entities
are calculated inUnicode and theutf_16_entities are calculated in UTF-16.
text="𠌕 bold 𝄢 italic underlined: 𝛙𝌢𑁍"unicode_entities=[MessageEntity(offset=2,length=4,type=MessageEntity.BOLD),MessageEntity(offset=9,length=6,type=MessageEntity.ITALIC),MessageEntity(offset=28,length=3,type=MessageEntity.UNDERLINE),]utf_16_entities=MessageEntity.adjust_message_entities_to_utf_16(text,unicode_entities)awaitbot.send_message(chat_id=123,text=text,entities=utf_16_entities,)# utf_16_entities[0]: offset=3, length=4# utf_16_entities[1]: offset=11, length=6# utf_16_entities[2]: offset=30, length=6
entities (Sequence[telegram.MessageEntity
]) – Sequence of entitieswith offset and length calculated in Unicode
Sequence of entitieswith offset and length calculated in UTF-16 encoding
Sequence[telegram.MessageEntity
]
Utility functionality for concatenating two text along with their formatting entities.
Tip
This function is useful for prefixing an already formatted text with a new text and itsformatting entities. In particular, it automatically correctly handles UTF-16 encoding.
Examples
This example shows a callback function that can be used to add a prefix and suffix tothe message in aCallbackQueryHandler
:
asyncdefprefix_message(update:Update,context:ContextTypes.DEFAULT_TYPE):prefix="𠌕 bold 𝄢 italic underlined: 𝛙𝌢𑁍 | "prefix_entities=[MessageEntity(offset=2,length=4,type=MessageEntity.BOLD),MessageEntity(offset=9,length=6,type=MessageEntity.ITALIC),MessageEntity(offset=28,length=3,type=MessageEntity.UNDERLINE),]suffix=" | 𠌕 bold 𝄢 italic underlined: 𝛙𝌢𑁍"suffix_entities=[MessageEntity(offset=5,length=4,type=MessageEntity.BOLD),MessageEntity(offset=12,length=6,type=MessageEntity.ITALIC),MessageEntity(offset=31,length=3,type=MessageEntity.UNDERLINE),]message=update.effective_messagefirst=(prefix,prefix_entities,True)second=(message.text,message.entities)third=(suffix,suffix_entities,True)new_text,new_entities=MessageEntity.concatenate(first,second,third)awaitupdate.callback_query.edit_message_text(text=new_text,entities=new_entities,)
Hint
The entities arenot modified in place. The function returns anew sequence of objects.
Added in version 21.5.
*args (tuple[str
, Sequence[telegram.MessageEntity
]] | tuple[str
, Sequence[telegram.MessageEntity
],bool
]) – Arbitrary number of tuples containing the text and its entities to concatenate.If the last element of the tuple is abool
, it is used to determine whetherto adjust the entities to UTF-16 viaadjust_message_entities_to_utf_16()
. UTF-16 adjustment is disabled bydefault.
The concatenated textand its entities
tuple[str
, Sequence[telegram.MessageEntity
]]
Utility functionality for shifting the offset of entities by a given amount.
Examples
Shifting by an integer amount:
text="Hello, world!"entities=[MessageEntity(offset=0,length=5,type=MessageEntity.BOLD),MessageEntity(offset=7,length=5,type=MessageEntity.ITALIC),]shifted_entities=MessageEntity.shift_entities(1,entities)awaitbot.send_message(chat_id=123,text="!"+text,entities=shifted_entities,)
Shifting using a string:
text="Hello, world!"prefix="𝄢"entities=[MessageEntity(offset=0,length=5,type=MessageEntity.BOLD),MessageEntity(offset=7,length=5,type=MessageEntity.ITALIC),]shifted_entities=MessageEntity.shift_entities(prefix,entities)awaitbot.send_message(chat_id=123,text=prefix+text,entities=shifted_entities,)
Tip
Theentities
arenot modified in place. The function returns a sequenceof new objects.
Added in version 21.5.
by (str
|int
) – Either the amount to shift the offset by ora string whose length will be used as the amount to shift the offset by. In thiscase, UTF-16 encoding will be used to calculate the length.
entities (Sequence[telegram.MessageEntity
]) – Sequence of entities
Sequence of entities with the offset shifted
Sequence[telegram.MessageEntity
]
MessageEntity
MessageEntity.type
MessageEntity.offset
MessageEntity.length
MessageEntity.url
MessageEntity.user
MessageEntity.language
MessageEntity.custom_emoji_id
MessageEntity.ALL_TYPES
MessageEntity.BLOCKQUOTE
MessageEntity.BOLD
MessageEntity.BOT_COMMAND
MessageEntity.CASHTAG
MessageEntity.CODE
MessageEntity.CUSTOM_EMOJI
MessageEntity.EMAIL
MessageEntity.EXPANDABLE_BLOCKQUOTE
MessageEntity.HASHTAG
MessageEntity.ITALIC
MessageEntity.MENTION
MessageEntity.PHONE_NUMBER
MessageEntity.PRE
MessageEntity.SPOILER
MessageEntity.STRIKETHROUGH
MessageEntity.TEXT_LINK
MessageEntity.TEXT_MENTION
MessageEntity.UNDERLINE
MessageEntity.URL
MessageEntity.adjust_message_entities_to_utf_16()
MessageEntity.concatenate()
MessageEntity.de_json()
MessageEntity.shift_entities()