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

Question:

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?

Asked By: Laura

||

Answers:

use caption parameter in sendPhoto method

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

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

Answered By: Laurent
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.