How to send a colored text message?
Asked Answered
D

3

25

I am trying to send a colored text message to a user as reply, using sendMessage with HTML parsing.

bot.sendMessage(update.message.chat_id, "<span style=\"color:blue\">foo</span>", telegram.ParseMode.HTML)

Sending <span style="color:blue">foo</span> doesn't work, as span is not supported:

Bad Request: Can't parse message text: Unsupported start tag "span" at byte offset 0 (400)

I am using the python-telegram-bot v. 4.0.3.

Is there another way?

Devalue answered 7/5, 2016 at 19:38 Comment(4)
Which module are you using, please provide a code exampleCelluloid
@EdoPut there you go, added both ;)Devalue
Telegram lacks this feature, so you cannot send coloured text. I have asked to add bb-code support in @BotSupport.Irksome
possible duplicate : https://mcmap.net/q/539813/-using-html-in-telegram-botMutualize
C
13

Unfortunately there is no documentation about the tag accepted by the bot, except the unit test for parsemode.

The tags that work are b, i, a for bold, italic and link elements, this is what is offered, maybe there's more but I couldn't find it in the docs

Celluloid answered 8/5, 2016 at 7:28 Comment(3)
Is there maybe some other solution other than HTML?Devalue
No, the telegram api docs describes the messageEntity as > mention (@username), hashtag, bot_command, url, email, bold (bold > text), italic (italic text), code (monowidth string), pre (monowidth block), > text_link (for clickable text URLs) So there is no way to add colour to text message but you can always process the text into an image and display that.Celluloid
yes, it is sad....but the few html tags supported are: core.telegram.org/bots/api#html-styleDominy
A
3

Your can enlight piece of text with grave character "`"

AAAA ` enlight piece ` BBB

Anissaanita answered 12/4, 2020 at 0:11 Comment(0)
S
1

According to the core API docs, Telegram only supports the <span> tag for spoiler text. I tried to send a message with HTML parsing and the text:

"<span style=\"color:#fc5252;\">text</span>"

And got:

Can't parse entities: tag "span" must have class "tg-spoiler" at byte offset 0

I'm guessing this means the <span> tag can only be used for spoiler text.

Schick answered 7/5, 2022 at 15:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.