admin管理员组文章数量:1022726
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?
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?
Share Improve this question asked Nov 19, 2024 at 8:29 user3130061user3130061 12 bronze badges1 Answer
Reset to default 0I 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
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?
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?
Share Improve this question asked Nov 19, 2024 at 8:29 user3130061user3130061 12 bronze badges1 Answer
Reset to default 0I 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
本文标签: Multiple download file from telegram using pythontelegrambotStack Overflow
版权声明:本文标题:Multiple download file from telegram using python-telegram-bot - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745575628a2157000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论