gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
make full functional python downloader and uploader with telebot libarry
- User
make full functional python downloader and uploader with telebot libarry
- Assistant
make full functional python downloader and uploader with telebot libarry
To create a fully functional downloader and uploader using the telebot library (also known as pyTelegramBotAPI) for a Telegram bot, you'll need to first set up your bot and install the required library.
Install the telebot library: You can install it via pip if you haven't done it already:
pip install pyTelegramBotAPI
Create a Telegram Bot:
Below is an example of a simple downloading and uploading bot using the telebot library in Python. This bot can handle file uploads and will download received files to the local filesystem.
import telebot
import os
API_TOKEN = 'YOUR_API_TOKEN_HERE'
bot = telebot.TeleBot(API_TOKEN)
DOWNLOAD_FOLDER = 'downloads/'
# Ensure the download folder exists
if not os.path.exists(DOWNLOAD_FOLDER):
os.makedirs(DOWNLOAD_FOLDER)
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(message, "Welcome to the Downloader/Uploader bot!\n"
"Send me any file and I will upload it to the server.\n"
"You can also request a file to download by sending the /download command.")
@bot.message_handler(content_types=['document'])
def handle_document(message):
file_info = bot.get_file(message.document.file_id)
file_name = message.document.file_name
download_path = os.path.join(DOWNLOAD_FOLDER, file_name)
# Download the file
downloaded_file = bot.download_file(file_info.file_path)
# Save the file
with open(download_path, 'wb') as new_file:
new_file.write(downloaded_file)
bot.reply_to(message, f"File '{file_name}' has been downloaded successfully!")
@bot.message_handler(commands=['download'])
def send_file(message):
try:
# List available files in the DOWNLOAD_FOLDER
files = os.listdir(DOWNLOAD_FOLDER)
if not files:
bot.reply_to(message, "No files available for download.")
return
# Create a reply keyboard with the list of available files
keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True)
for file_name in files:
keyboard.add(telebot.types.KeyboardButton(file_name))
bot.send_message(message.chat.id, "Choose a file to download:", reply_markup=keyboard)
except Exception as e:
bot.reply_to(message, f"An error occurred while trying to list files: {str(e)}")
@bot.message_handler(func=lambda message: True)
def send_chosen_file(message):
file_name = message.text
file_path = os.path.join(DOWNLOAD_FOLDER, file_name)
if os.path.isfile(file_path):
with open(file_path, 'rb') as file:
bot.send_document(message.chat.id, file)
bot.reply_to(message, f"Sending you the file: {file_name}")
else:
bot.reply_to(message, "File not found! Please choose a valid file.")
if __name__ == '__main__':
print("Bot is polling...")
bot.polling(none_stop=True)
Imports and Setup:
TeleBot instance with your bot token.downloads/ directory exists to store downloaded files.Handler for /start and /help:
Document Handling:
Download Command:
/download command.File Sending Based on User Choice:
YOUR_API_TOKEN_HERE with your actual bot token obtained from BotFather./download command, and selecting files to download.Not yet included in a published release.