Search code examples
pythonpython-3.xtelegramtelegram-botpython-telegram-bot

Send online photo with Telegram Messenger API with HTML mode


I'm creating a bot for Telegram Messenger with Python.

I'm using the following function :

def telegram_bot_sendtexHTML(bot_message):

    bot_token = 'token'
    bot_chatID = 'id'
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=HTML&text=' + bot_message
    response = requests.get(send_text)

    return response.json()

I have a list of images online (with URL link) I want to send using this function.

For example : https://media.wordpress.com/picture1.jpg

How can I insert this lik in my function in order to being able to send directly the picture (and not the link of the picture) ?


Solution

  • Use sendPhoto instead of the sendMessage method. The required parameters are nicely described in the documentation.

    I've altered your telegram_bot_sendtexHTML function to a telegram_bot_sendImage;

    def telegram_bot_sendImage(caption, url):
    
        bot_token = ''
        bot_chatID = ''
        send_text = 'https://api.telegram.org/bot' + bot_token + '/sendPhoto?chat_id=' + bot_chatID + '&parse_mode=HTML&caption=' + caption + '&photo=' + url
        response = requests.get(send_text)
    
        return response.json()
    
    telegram_bot_sendImage('Cool image!', 'http://placehold.it/200x200')
    

    enter image description here