Search code examples
python-telegram-bot

Multiple download file from telegram using python-telegram-bot


I wrote this function to download big files from Telegram using aiogram bot.

I call the function with message handler with filter:

application.add_handler(MessageHandler(filters.VIDEO, downloader_video_fnc, filters.User(username="@XXXXXXXX")))

and the function is:

async def downloader_video_fnc(update, context):
    """
    Function to list help commands.
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    try:
        filename = update.message.video.file_name
        filename = clean_filename(filename)
        reply_text = "<b>"+filename+"</b>" + " message ok" 
        await update.message.reply_text(reply_text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
        
        file = await context.bot.get_file(update.message.video)
        if(filename==None):
            a = urlparse(file.file_path)
            filename = os.path.basename(a.path)
        reply_text = "<b>"+filename+"</b>" + " file ok" 
        await update.message.reply_text(reply_text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)

        await file.download_to_drive(filename)
        reply_text = "<b>"+filename+"</b>" + " download ok"         
        await update.message.reply_text(reply_text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
        
        flag_file_exist = await ftp_file_check(filename, update)
        if(flag_file_exist):
            reply_text = "<b>"+filename+"</b>" + " file exist"
        else:
            reply_text = "<b>"+filename+"</b>" + " file don't exist"
        await update.message.reply_text(reply_text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
        
        if(flag_file_exist==False):
            flag_file_upload = await ftp_file_upload(filename, update)
            if(flag_file_upload):
                reply_text = "<b>"+filename+"</b>" + " upload ok"
            else:
                reply_text = "<b>"+filename+"</b>" + " upload ko"
            await update.message.reply_text(reply_text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
    
    except Exception as e:
        file_text = "Error" + str(e)
        await update.message.reply_text(file_text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
    return None

and it works fine for one or two files.

However, if I send multiple big files, the bot go offline and the download fail.

I have tried to implement using a multi-process approach without success. Can someone give some advice on how I should implement it, or suggest how to improve my code to make it work better?


Solution

  • I found a solution! The problem was not in the "downloader_video_fnc" function, but in the "MessageHandler"

    The correct MessageHandler for concurrency is

    application.add_handler(MessageHandler(filters=(filters.VIDEO & filters.User(username="@XXXXXXXX")), callback=downloader_video_fnc, block=False))
    

    block (bool, optional) – Determines whether the return value of the callback should be awaited before processing the next handler in telegram.ext.Application.process_update(). Defaults to True. MessageHandler manual