How should I use parse_mode='HTML' in telegram python bot?
Asked Answered
H

4

19

I'm trying to send a message in a channel with a bot, using Telegram API's send_photo() method. It takes a caption parameter (type String) but I can't format it through parse_mode='HTML' parameter...

If I use something like this:

send_photo(chat_id, photo, caption="<b>Some text</b>", parse_mode='HTML') 

it sends the message but without any kind of formatting. Does anybody know why? Thanks

Horton answered 12/5, 2019 at 22:33 Comment(0)
C
27

First, you need to import ParseMode from telegram like this:

from telegram import ParseMode

Then, all you need is to specify parse_mode=ParseMode.HTML. Here's a working example:

def jordan(bot, update):
    chat_id = update.message.chat.id
    with open('JordanPeterson.jpg', 'rb') as jordan_picture:
        caption = "<a href='https://twitter.com/jordanbpeterson'>Jordan B. Peterson</a>"
        bot.send_photo(
            chat_id, 
            photo=jordan_picture, 
            caption=caption,
            parse_mode=ParseMode.HTML
        )

And we can see that it works:

works

Update: Actually, both parse_mode='html' (as suggested by @slackmart) and parse_mode='HTML' that you used yourself work for me!

Another Update (as per your comment): You can use multiple tags. Here's an example of one, with hyperlink, bold, and italic:

multiple tags

Yet Another Update: Regarding your comment:

...do I have any limitations on HTML tags? I can't use something like <img> or <br> to draw a line

Honestly,

try and find out

That's what I did!

Now you're trying to format the caption of an image, using HTML, meaning you're formatting a text, so obviously, you can't use "something like <img>." It has to be a "text formatting tag" (plus <a>). And not even all of them! I believe you can only use these: <a>, <b>, <strong>, <i> and <em>.

If you try to use a text-formatting tag like <del>, it will give you this error:

Can't parse entities: unsupported start tag "del" at byte offset 148

Which is a shame! I'd love to be able to do something like this in captions of images.or something like this!

Culhert answered 13/5, 2019 at 11:21 Comment(0)
C
4

As per version 20, you need to use:

from telegram.constants import ParseMode

More about it in the official docs here: https://pythontelegramrobot.readthedocs.io/en/latest/telegram.parsemode.html

Crockery answered 23/4, 2023 at 15:49 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Faultless
S
2

It works for me! Here's the code I'm using:

>>> from telegram import Bot
>>> tkn = '88888:199939393'; chid = '-31828'
>>> bot = Bot(tkn)
>>> with open('ye.jpeg', 'rb') as fme:
...   bot.send_photo(chid, fme, caption='<b>Hallo</b>', parse_mode='html')
...
<telegram.message.Message object at 0x7f6301b44d10>

Of course, you must use your own telegram token and channel id. Also notice I'm using parse_mode='html' # lowercase

Sibell answered 13/5, 2019 at 1:42 Comment(6)
huh, I always did it with parse_mode=ParseMode.HTML. Didn't think parse_mode='html' would work as well! +1 for that.Culhert
BTW, your answer suggests that the problem is with HTML, being uppercase. However, html works as well.Culhert
Thanks, but do I have a limitation on tags I can use? I mean, I can't use <a href=...> on caption, right? Or is there a way to use it the same?Horton
@Horton check out my second update. Also, you can use <a href=""></a>! Did you even look at my answer?Culhert
@Amir sorry, I saw your update now...I see that I can use <a href> tags in captions but do I have any limitations on HTML tags? I can't use something like <img> or <br> to draw a lineHorton
@Horton I added another update to address your concern. Let's move the conversation under my own answer, as to not make unnecessary noise for @slackmart. However, comments are not meant for extended conversations, if there's something that hasn't been answered, you should update your question to make it clear as what you wish to understand.Culhert
R
1

Instead of <br> use \n.

Rok answered 10/5, 2023 at 10:5 Comment(1)
Thanks, this helped me solve my issue!Hidden

© 2022 - 2024 — McMap. All rights reserved.