F
F
Famper Wasteland2021-10-20 23:41:34
Python
Famper Wasteland, 2021-10-20 23:41:34

How do I return the text sent to the bot (Telebot)?

import telebot

token = 'TOKEN'
channel_name = '@CHANNEL'

bot = telebot.TeleBot(token)

@bot.message_handler(content_types=['text', 'audio'])
def send_message_group(message):
    doc_id = message.audio.file_id
    bot.send_audio(channel_name, audio=doc_id, caption=message.text)
    print(f'\nСообщение успешно доставлено.\nText: {message.text}\nFile_Id: {doc_id}')

if __name__ == '__main__':
    bot.infinity_polling()


RESULT:
Сообщение успешно доставлено.
Text: None
File_Id: CQACAgIAsssAAMlYXssssbNssRZSqzDa02kb5ssssssoIBLtEsctssrvOEssA


The most important question. How can I get the text and send it formatted? When I try to get it with the example method, it fails (result: None).

Bot concept:
I send him a message with an audio file, he forwards this message with text, but the text is formatted. In telegram, the combinations CTRL + B & CTRL + U are used for this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
InternetMaster, 2021-10-21
@InternetMaster

You need to write a message according to HTML tags!
At the end, do not forget to attribute parse_mode!
For example, to send bold or underlined text, you need to sharpen them into the appropriate tags

<b> это жирный текст </b>
<u> это подчеркнутый текст </u>

That is, in the end the code will look like this:
import telebot

token = 'TOKEN'
channel_name = '@CHANNEL'

bot = telebot.TeleBot(token)

@bot.message_handler(content_types=['text', 'audio'])
def send_message_group(message):
    doc_id = message.audio.file_id
    bot.send_audio(channel_name, audio=doc_id, caption=message.text, parse_mode='HTML')
    print(f'\nСообщение успешно доставлено.\nText: {message.text}\nFile_Id: {doc_id}')

if __name__ == '__main__':
    bot.infinity_polling()

# При этом message.text должен быть составлен так "Это обычный шрифт <b> а тут уже жирный шрифт </b> тут снова обычный <u> а тут подчеркнутый текст </u>"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question