roaste
roaste

Reputation: 1

How to transfer entities to telebot?

The official API has an entities parameter, but when I pass it to telebot, it gives me an error:

TypeError: send_message() got an unexpected keyword argument 'entities'

Code:

import telebot

bot = telebot.TeleBot(token)

@bot.message_handler(content_types=['text', 'audio', 'document', 'photo'])
def messages(message):
  bot.send_message(message.chat.id, text=message.text, entities=message.entities)       
bot.polling(none_stop=True, timeout=123)

How can I fix this error?

Upvotes: 0

Views: 1509

Answers (2)

Del Dmc
Del Dmc

Reputation: 1

If you want to pass "entities" to the message you should pass first of all list of

MessageEntity()

instances, where each instance will have it's own instruction regarding the text should be sent.

As it was mentioned by Senku the attributes are as follows:

type - can be "mention", "bold", "italic", " spoiler" etc. more in docs

Type is used to give instruction which type of text will have the message given in the "text" keyword

bot.send_message(message.chat.id, text="Here is the text to be edited by 
    entities")

So, for this example I will use "italic"

type="italic"

offset is used to denote offset from the first symbol in a text. I will use 5 for this case, so that transformation will start from 6th symbol

offset=5

length is used to denote how many symbols will be used after offset

length=6

Finally, my code will look as follows:

text_message = "Here is the text to be edited by entities"
message_entities = [MessageEntity(length=6, offset=5, type="italic")]
bot.send_message(
    chat_id=message.chat.id, 
    entities=message_entities, 
    text=text_message)

And the output:

" Here is the text to be edited by entities "

url: is used for “text_link” only, URL that will be opened after user taps on the text (from official documentation)

user is used for “text_mention” only, the mentioned user (from official documentation)

Remember, that entities work only if

parse_mode=None

Upvotes: 0

Senku
Senku

Reputation: 46

First of all bo.send_message has no attribute 'entities' If you want to look up the entities, just try

print(message.entities[0])

it returns you this text: {'type': 'bot_command', 'offset': 0, 'length': 6, 'url': None, 'user': None, 'language': None}

You can return multiple reply entities object using message.entities without number. By the way you can not return entities in bot.send_message command. For this you should try to use a variable or print command to look what is in the entity.

The attributes in entities:

  • type
  • url
  • offset
  • length
  • user

Upvotes: 1

Related Questions