A
A
arynyklas2020-01-08 20:35:24
Python
arynyklas, 2020-01-08 20:35:24

How to implement downloading a file to a specific directory in telebot?

Example:
The bot admin (owner) sends a file with a signature (let's call it text) to the bot. This text contains only the directory where you want to download the file. So, how to implement it?
Unfinished and non-working crutches:

import telebot
import time

token = 'типо токена'
owner = 'типо ид'

bot = telebot.TeleBot(token)

@bot.message_handler(commands=['upload'])
def send_text(message):
    try:
        save_dir = message.text.replace('/upload ', '')
        file_info = bot.get_file(message.file.file_id)
        downloaded_file = bot.download_file(file_info.file_path)
        src = message.file.file_name
        with open(save_dir + "/" + src, 'wb') as new_file:
            new_file.write(downloaded_file)
        bot.send_message(message.chat.id, "[*] File added:\nFile name - {}Directory - \n".format(str(src), str(save_dir)))
    except Exception as ex:
        bot.send_message(message.chat.id, "[!] error - {}".format(str(ex)))

while True:
    try:
        print("[*] bot starting..")
        bot.send_message(owner, "[*] bot starting..")
        print("[*] bot started!")
        bot.send_message(owner, "[*] bot started!")
        bot.polling(none_stop=True, interval=2)
        # Предполагаю, что бот может мирно завершить работу, поэтому
        # даем выйти из цикла
        break

    except Exception as ex:
        print("[*] error - {}".format(str(ex))) # Описание ошибки
        bot.send_message(owner, "[*] error - {}".format(str(ex)))
        print("[*] rebooting..")
        bot.send_message(owner, "[*] rebooting..")
        bot.stop_polling()
        # Останавливаем чтобы не получить блокировку
        time.sleep(15)
        print("[*] restarted!")
        bot.send_message(owner, "[*] restarted!")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
arynyklas, 2020-01-09
@arynyklas

Solved the problem myself:

@bot.message_handler(content_types=['document'])
def send_text(message):
    try:
        try:
            save_dir = message.caption
        except:
            save_dir = os.getcwd()
            s = "[!] you aren't entered directory, saving to {}".format(save_dir)
            bot.send_message(message.chat.id, str(s))
        file_name = message.document.file_name
        file_id = message.document.file_name
        file_id_info = bot.get_file(message.document.file_id)
        downloaded_file = bot.download_file(file_id_info.file_path)
        src = file_name
        with open(save_dir + "/" + src, 'wb') as new_file:
            new_file.write(downloaded_file)
        bot.send_message(message.chat.id, "[*] File added:\nFile name - {}\nFile directory - {}".format(str(file_name), str(save_dir)))
    except Exception as ex:
        bot.send_message(message.chat.id, "[!] error - {}".format(str(ex)))

Here a person sends a file, I specify the directory where to save

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question