K
K
keithzetterstrom2017-10-20 08:49:01
Python
keithzetterstrom, 2017-10-20 08:49:01

How to teach a bot to download files (text) that users send to it?

bot = telebot.TeleBot(helpbot.token);
@bot.message_handler(content_types=['document']);
somefile = bot.get_file(message.document.file.id);
file = requests.get(' https://api.telegram.org/file/bot{0}/{1} '.format(API_TOKEN, somefile.file_path));
In the third line, it gives an error of unvalid syntax. I can't figure out what the problem is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil K., 2018-01-12
@Danya_Violet

# Сохраняем присланый от пользователя файл
@bot.message_handler(content_types=['document'])
def handle_file(message):
    try:
        chat_id = message.chat.id
        file_info = bot.get_file(message.document.file_id)
        downloaded_file = bot.download_file(file_info.file_path)
        src = 'C:/Python/Project/bot/files/received/' + message.document.file_name;
        with open(src, 'wb') as new_file:
            new_file.write(downloaded_file)
        bot.reply_to(message, "Пожалуй, я сохраню это")
    except Exception as e:
        bot.reply_to(message, e)
    log(message)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question