Skip to content

Instantly share code, notes, and snippets.

@Poolitzer
Created October 21, 2019 18:03
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Poolitzer/f8aebd91e3fbbe3412cc747adaa90e0c to your computer and use it in GitHub Desktop.
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
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()
@Matin-B
Copy link

Matin-B commented Oct 22, 2019

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment