How to create a formatted Telegram bot message with image and text?

I’m trying to make a Telegram bot that sends messages with both pictures and text. I saw an example in the docs that looked really cool. It had an image with some text below it, kind of like a TechCrunch post. I’ve been messing around with Python but can’t get it to work right. Here’s what I’ve tried so far:

def send_fancy_message(chat_id, text=None, image=None):
    if text and image:
        response = requests.post(API_ENDPOINT + 'sendPhoto', data={
            'chat_id': chat_id,
            'caption': text,
            'photo': image
        })
        return response.json()

Does anyone know how to make it look like that example? I’m kinda stuck and could use some help. Thanks!

I’ve worked with Telegram bots before, and it sounds like you’re on the right track. The key to getting that polished look is using Telegram’s built-in formatting options. Instead of just sending plain text in the caption, you can use HTML or Markdown formatting. For HTML, try something like this:

caption = ‘Your Bold Title\n\nYour description text here. You can use italics or even links

Then, in your API call, add ‘parse_mode’: ‘HTML’. This should give you that professional, formatted look you’re after. Just remember to escape any special characters in your text to avoid parsing errors. Hope this helps you achieve the style you’re going for!

hey, i’ve done something similar before. try using markdown formatting in ur caption. it’s easier than HTML imho. just add ‘parse_mode’: ‘Markdown’ to ur API call. u can do stuff like bold and italic text. also, make sure ur image url is valid. that tripped me up b4. good luck!

I’ve been in your shoes, and I know how frustrating it can be to get the formatting just right. One thing that really helped me was using Telegram’s inline keyboards. They add a professional touch and make your bot more interactive.

To implement this, you’ll need to modify your code a bit. Instead of just sending a photo with a caption, you can send a photo as a media group and attach an inline keyboard. Here’s a rough example:

from telegram import InlineKeyboardButton, InlineKeyboardMarkup

keyboard = [[InlineKeyboardButton('Read More', url='https://example.com')]]
reply_markup = InlineKeyboardMarkup(keyboard)

media = [InputMediaPhoto(media=image_url, caption=text, parse_mode='Markdown')]

bot.send_media_group(chat_id=chat_id, media=media)
bot.send_message(chat_id=chat_id, text='Additional options:', reply_markup=reply_markup)

This approach gives you more control over the layout and adds interactivity. It took me some trial and error, but the results were worth it. Good luck with your bot!