J
J
Jordan_Belforts2020-08-12 22:39:45
Python
Jordan_Belforts, 2020-08-12 22:39:45

How to save multiple photos that are received by api?

Hello.
I don’t understand how you can save all received photos from telegrams.
The following code saves only the first photo that arrives.
I assume that you need to use async, but I'm not strong in it and I don't know how to solve it.
Tell me, please, in which direction should I look, thanks in advance for your attention.

@bot.message_handler(content_types=['photo'])
def getting_photos(message):
    if connect.select_status(chat_id=message.chat.id) == "waiting photo":

        response_getfile = requests.post(URL + "getfile", {"file_id": "%s" % message.photo[0].file_id}).json()
        file_path = response_getfile["result"]["file_path"]
        getting_photo = requests.get(URL_FILE + file_path)
        name_file_photo = file_path.split("/")
        directory = add_folder(connect.get_way_folder(chat_id=message.chat.id))
        with open("{}/{}".format(directory, name_file_photo[1]), "wb") as f:
            f.write(getting_photo.content)
        connect.update_status(status="waiting command", chat_id=message.chat.id)

        connect.default_way(message.chat.id)
        bot.send_message(chat_id=message.chat.id, text="Сохранено")

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question