T
T
Tim2452020-12-16 12:11:35
Bots
Tim245, 2020-12-16 12:11:35

How to save a video sent by a telegram user to a bot?

I need to save the video sent by the user

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
John Franco, 2020-12-16
@user-anonymous

From phone or PC?

O
o5a, 2020-12-16
@o5a

The simplest option. It is worth adding checks in the form of try..except in case of errors.

@bot.message_handler(content_types=['document'])
def get_file(message):
    file_name = message.document.file_name
    file_info = bot.get_file(message.document.file_id)
    with open(file_name, "wb") as f:
        file_content = bot.download_file(file_info.file_path)
        f.write(file_content)
    bot.reply_to(message, f"OK. Сохранил {file_name}")

The types of received files are just written in content_types, i.e. for video you will need to use 'video' (add to list or replace).
I looked, you need to use other tags for the video, and for some reason it doesn’t work by analogy with document, but this is how it should be
@bot.message_handler(content_types=['video'])
def get_file(message):
    file_name = message.json['video']['file_name']
    file_info = bot.get_file(message.video.file_id)
    with open(file_name, "wb") as f:
        file_content = bot.download_file(file_info.file_path)
        f.write(file_content)
    bot.reply_to(message, f"OK. Сохранил {file_name}")

I
Ivan Tataush, 2017-01-16
@WannaCreative

here is font.ubuntu.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question