skip to Main Content

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?

2

Answers


  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')
    
    Login or Signup to reply.
  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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search