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 the parse_mode='HTML'
parameter. If I use:
send_photo(chat_id, photo, caption="<b>Some text</b>", parse_mode='HTML')
it sends the message but without formatting. Why?
First, you need to import ParseMode
from telegram
:
from telegram import ParseMode
Then, specify parse_mode=ParseMode.HTML
:
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
)
It works:
Both parse_mode='html'
(suggested by slackmart) and parse_mode='HTML'
that you used yourself work for me! As per your comment: You can use multiple tags. Here's an example with hyperlink
, bold
, and italic
:
Regarding your comment:
...do I have any limitations on HTML tags? I can't use something like
<img>
or<br>
to draw a line
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>
). I believe you can only use: <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