Files
Telegram-Downloader-Bot/telegram_downloader_bot/main.py
Roman Krček c1bf9c1e53
Some checks failed
Build Docker image / test (push) Failing after 2m29s
Build Docker image / build (push) Has been skipped
Fix minor problems with user IDs and datetime
2024-10-13 19:06:47 +02:00

79 lines
2.0 KiB
Python

import os
import uvloop
from pyrogram import Client, filters
from pyrogram.types import Message
from telegram_downloader_bot.logger import log
from telegram_downloader_bot.telemetry import init_telemetry
from telegram_downloader_bot import utils, security
API_ID = os.getenv("API_ID") # Your API ID from my.telegram.org
API_HASH = os.getenv("API_HASH") # Your API Hash from my.telegram.org
BOT_TOKEN = os.getenv("BOT_TOKEN") # Your bot token from BotFather
STORAGE = os.getenv("STORAGE") # Your bot token from BotFather
uvloop.install()
init_telemetry()
app = Client("downloader_bot",
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN,
workers=1)
@app.on_message(filters.command("start"))
@security.protected
async def start_handler(_, message: Message):
await message.reply_text(
"This bot downloads TikTok videos to my personal server"
)
@app.on_message(filters.command("help"))
@security.protected
async def help_handler(_, message: Message):
await message.reply_text("I won't help you!")
@app.on_message(filters.text)
@security.protected
async def message_handler(_, message: Message):
urls = utils.extract_urls(message.text)
if not urls:
return await message.reply_text(
"No links found in the message. Nothing to download!"
)
tt_urls = utils.filter_tt_urls(urls)
if not tt_urls:
return await message.reply_text(
"No TikTok URLs found! Nothing to download!"
)
for i, url in enumerate(urls):
msg = f"Downloading video {i+1}/{len(urls)}..."
log.info(msg)
await message.reply_text(msg)
utils.download_tt_video(STORAGE, url)
await message.reply_text("Done.")
@app.on_message(filters.media)
@security.protected
async def media_handler(client, message: Message):
await message.reply_text("Downloading media...")
utils.handle_media_message_contents(STORAGE, client, message)
if __name__ == "__main__":
utils.make_fs(STORAGE)
app.run()