From 00e24e3bd9b711afcad1dc7f579bbac62b88e0eb Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sun, 20 Jun 2021 19:10:35 +0530 Subject: [PATCH] Add /finish --- bot.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index de1946a..5bf8d95 100755 --- a/bot.py +++ b/bot.py @@ -3,6 +3,7 @@ import os import os.path as pt import shutil +import subprocess from telegram.ext import Updater, CommandHandler, MessageHandler, filters import config @@ -33,7 +34,23 @@ def on_img_received(upd, ctx): def on_finish(upd, ctx): - pass + user_id = upd.effective_chat.id + path = f"data/{user_id}" + if not pt.isdir(path): + ctx.bot.send_message(upd.effective_chat.id, "No Images Available") + return + + ctx.bot.send_message(upd.effective_chat.id, "Started Merging") + files = list(sorted( + os.listdir(path), + key=lambda file: int(file) + )) + pdf_path = f"{path}/final.pdf" + subprocess.run(["convert", *[path + "/" + file for file in files], pdf_path]) + with open(pdf_path, "rb") as f: + ctx.bot.send_document(upd.effective_chat.id, f) + shutil.rmtree(path) + def on_cancel(upd, ctx): @@ -41,7 +58,7 @@ def on_cancel(upd, ctx): path = f"data/{user_id}" if pt.isdir(path): shutil.rmtree(f"data/{user_id}") - ctx.bot.send_message(upd.effective_chat.id, f"Cleared images") + ctx.bot.send_message(upd.effective_chat.id, "Cleared images") updater = Updater(token=config.API_KEY, use_context=True)