0

Can anyone please show me how can I use the requests library in python to send photos from the internet?

I'm currently trying to send the photos as:

https://api.telegram.org/bot<token>/sendPhoto-Fchat_id=<MY-ID>-Fphoto=https://random.dog/7e1527a4-9c07-48df-a877-4c6e4681adc1.jpg

Thank you in advance

0stone0
  • 34,288
  • 4
  • 39
  • 64
m.prime
  • 67
  • 7

1 Answers1

1

You pass the wrong url parameters.

Instead off sendPhoto-Fchat_id use the ? and & sign. If you wish to add multiple parameters; take a look at Querystring Array Parameters in Python using Requests

sendPhoto?chat_id=123&photo=somelink

To answer the question, you can use the requests library like this:

import requests
from requests.exceptions import HTTPError

url="https://api.telegram.org/bot<MY-TOKEN>/sendPhoto?chat_id=<MY-CHAT-ID>&photo=http://via.placeholder.com/100x100"

try:
    response = requests.get(url)

    # If the response was successful, no Exception will be raised
    response.raise_for_status()
except HTTPError as http_err:
    print(f'HTTP error occurred: {http_err}')  # Python 3.6
except Exception as err:
    print(f'Other error occurred: {err}')  # Python 3.6
else:
    print('Success!')

0stone0
  • 34,288
  • 4
  • 39
  • 64