J
J
jojohf5552021-08-02 18:43:20
Python
jojohf555, 2021-08-02 18:43:20

Why is the picture not updating in telegram bot?

I'm making a bot to generate random cats. I take the url https://thiscatdoesnotexist.com/ . But the image is not updated. What to do?

if message.text == '!кот':
        bot.send_photo(message.chat.id,
                        f'https://thiscatdoesnotexist.com/', reply_markup=klikeordislike)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-08-02
@jojohf555

Most likely telegram caches the request and then uses the photo from the cache. Just upload the file and then send

import requests

r = requests.get('https://thiscatdoesnotexist.com')
with open('image_temp.jpg', 'wb') as f:
    f.write(r.content)
bot.send_photo(message.chat.id, open('image_temp.jpg', 'rb'))

It is possible without saving the file, of course,
import requests
from PIL import Image
import io

r = requests.get('https://thiscatdoesnotexist.com')
bot.send_photo(message.chat.id, Image.open(io.BytesIO(r.content)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question