Created
October 21, 2019 18:03
-
-
Save Poolitzer/f8aebd91e3fbbe3412cc747adaa90e0c to your computer and use it in GitHub Desktop.
An example of how to download bigger files via telethon, coming from ptb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from telegram.ext import Updater, MessageHandler, Filters | |
from telethon.utils import resolve_bot_file_id, get_input_location | |
from telethon import TelegramClient | |
import logging | |
logging.basicConfig() | |
def file_handler(update, context): | |
file = update.effective_message.document | |
file_size = file.file_size | |
file_id = file.file_id | |
file_name = file.file_name | |
if file_size > 20000000: | |
loop = asyncio.new_event_loop() | |
get_file_telethon(context.bot.token, file_id, file_name, loop) | |
else: | |
update.effective_message.document.get_file().download() | |
def get_file_telethon(bot_token, file_id, file_name, loop): | |
api_id = 12345 | |
api_hash = "wuhuw1346789" | |
bot = TelegramClient('bot', api_id, api_hash, loop=loop).start(bot_token=bot_token) | |
with bot: | |
document = resolve_bot_file_id(file_id) | |
location = get_input_location(document) | |
loop.run_until_complete(bot.download_file(location[1], file_name)) | |
updater = Updater("BOT_TOKEN", use_context=True) | |
dp = updater.dispatcher | |
dp.add_handler(MessageHandler(Filters.document, file_handler)) | |
updater.start_polling() | |
updater.idle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks