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

Hey everyone! I’m trying to make a cool Telegram bot that can send messages with both pictures and text, like those fancy news posts you see. I’ve been looking at the Telegram docs and spotted an example from TechCrunch that looks awesome, but I can’t figure out how to do it myself.

I’m using Python and I’ve got this code so far:

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

But it’s not working quite right. Can anyone help me figure out how to get that slick image-with-text-below format? I’d really appreciate some tips or examples on how to nail this! Thanks in advance!

hey alice45, i’ve played around with telegram bots before. for that fancy layout, try using sendMediaGroup instead of sendPhoto. you can group an image and text as separate entities. something like:

media = [
    {'type': 'photo', 'media': image_url},
    {'type': 'text', 'text': caption}
]
requests.post(API_URL + 'sendMediaGroup', data={'chat_id': chat_id, 'media': json.dumps(media)})

hope this helps! lmk if u need more info

I’ve been working with Telegram bots for a while now, and I’ve found that using the ‘sendPhoto’ method with inline keyboard markup can give you that sleek look you’re after. Here’s a snippet that might help:

from telegram import InlineKeyboardButton, InlineKeyboardMarkup

def send_fancy_message(chat_id, text, image_url):
    keyboard = [[InlineKeyboardButton('Read More', url='https://your-link.com')]]
    reply_markup = InlineKeyboardMarkup(keyboard)
    
    return bot.send_photo(
        chat_id=chat_id,
        photo=image_url,
        caption=text,
        reply_markup=reply_markup,
        parse_mode='HTML'
    )

This approach allows you to add interactive buttons below your image and text, which can really enhance user engagement. Just make sure you’ve properly set up your bot instance before calling this function. Hope this helps you achieve that professional news post look!

I’ve encountered a similar issue while developing Telegram bots. The key is to use the ‘sendPhoto’ method with the ‘parse_mode’ parameter set to ‘HTML’. This allows you to format your caption text for a more polished look. Here’s an improved version of your code:

def send_message(chat_id, text, image_url):
    response = requests.post(
        f'{API_URL}sendPhoto',
        data={
            'chat_id': chat_id,
            'photo': image_url,
            'caption': text,
            'parse_mode': 'HTML'
        }
    )
    return response.json()

This approach should give you the image-with-text-below format you’re after. Remember to properly encode any HTML in your caption text to avoid formatting issues.