'How can I send image with text using telegram bot API?

I'm trying to send a message to telegram using Telegram API bot.

I want to make a GET request, that will send both text and image to my telegram channel.

Now fetch looks like that:

telegram_msg = requests.get('https://api.telegram.org/<botname>:botAPI/sendMessage?chat_id=<chat_id>=some text')

How can I include an image that is located here and send both text and image in one message?

Thank you for your help.

UPD: I can send image using sendPhoto, but how can I combine these two requests into one? So that I can send both image and text?



Solution 1:[1]

use caption parameter in sendPhoto method

telegram_msg = requests.get('https://api.telegram.org/bot<botAPI>/sendPhoto?chat_id=<chat_id>&caption=some text')

Solution 2:[2]

After using @BotFather to get your API token

import requests
chat_id = '<chat_id>'
token = '<token>'
msg = "Send text with photo ?"
img_uri = "https://www.ixbt.com/img/n1/news/2022/3/1/62342d1404eb2_large.jpg"
telegram_msg = requests.get(f'https://api.telegram.org/bot{token}/sendPhoto?chat_id={chat_id}&caption={msg}&photo={img_uri}')
print(telegram_msg)
print(telegram_msg.content)

Post result with text and image in Telegram

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Laurent