I am building a Telegram bot using a Telepot library. To send a picture downloaded from Internet I have to use sendPhoto method, which accepts a file-like object.
Looking through the docs I found this advice:
If the file-like object is obtained by
urlopen()
, you most likely have to supply a filename because Telegram servers require to know the file extension.
So the question is, if I get my filelike object by opening it with requests.get
and wrapping with BytesIO
like so:
res = requests.get(some_url)
tbot.sendPhoto(
messenger_id,
io.BytesIO(res.content)
)
how and where do I supply a filename?
You would supply the filename as the object's
.name
attribute.Opening a file with
open()
has a .name attribute.Where opening a url does not. Which is why the documentation specifically mentions this.
In your case, you would need to create the file-like object, and give it a
.name
attribute: