Allow renaming a file by passing parameter to /finish
Update /start. Add /help as an alias to /start
This commit is contained in:
parent
00e24e3bd9
commit
7e680c0d22
24
bot.py
24
bot.py
|
@ -5,15 +5,20 @@ import os.path as pt
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
from telegram.parsemode import ParseMode as Parse
|
||||||
from telegram.ext import Updater, CommandHandler, MessageHandler, filters
|
from telegram.ext import Updater, CommandHandler, MessageHandler, filters
|
||||||
import config
|
import config
|
||||||
|
|
||||||
def on_start(upd, ctx):
|
def on_start(upd, ctx):
|
||||||
message = ("Welcome to PDF Uniter.\n"
|
message = (
|
||||||
"To get started, simply send images that will be compiled into "
|
"<b>Welcome to PDF Uniter</b>\n\n"
|
||||||
"a pdf in the order they are sent. Once completed, send /finish "
|
"To get started, simply send images that will be compiled into a pdf "
|
||||||
"to receive a pdf. In case of errors, send /cancel to restart.")
|
"in the order they are sent.\n\n"
|
||||||
ctx.bot.send_message(upd.effective_chat.id, message)
|
"- Once completed, send /finish to receive a pdf.\n"
|
||||||
|
"- To set a different filename, use <code>/finish filename.</code>\n"
|
||||||
|
"- In case of errors, send /cancel to restart.\n"
|
||||||
|
)
|
||||||
|
ctx.bot.send_message(upd.effective_chat.id, message, parse_mode=Parse.HTML)
|
||||||
|
|
||||||
|
|
||||||
def on_img_received(upd, ctx):
|
def on_img_received(upd, ctx):
|
||||||
|
@ -47,8 +52,14 @@ def on_finish(upd, ctx):
|
||||||
))
|
))
|
||||||
pdf_path = f"{path}/final.pdf"
|
pdf_path = f"{path}/final.pdf"
|
||||||
subprocess.run(["convert", *[path + "/" + file for file in files], pdf_path])
|
subprocess.run(["convert", *[path + "/" + file for file in files], pdf_path])
|
||||||
|
|
||||||
|
_, *filename = upd.message.text.split(" ")
|
||||||
|
if filename:
|
||||||
|
filename = " ".join(filename).rstrip(".pdf") + ".pdf"
|
||||||
|
else:
|
||||||
|
filename = "final.pdf"
|
||||||
with open(pdf_path, "rb") as f:
|
with open(pdf_path, "rb") as f:
|
||||||
ctx.bot.send_document(upd.effective_chat.id, f)
|
ctx.bot.send_document(upd.effective_chat.id, f, filename=filename)
|
||||||
shutil.rmtree(path)
|
shutil.rmtree(path)
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,6 +75,7 @@ def on_cancel(upd, ctx):
|
||||||
updater = Updater(token=config.API_KEY, use_context=True)
|
updater = Updater(token=config.API_KEY, use_context=True)
|
||||||
dispatcher = updater.dispatcher
|
dispatcher = updater.dispatcher
|
||||||
dispatcher.add_handler(CommandHandler("start", on_start))
|
dispatcher.add_handler(CommandHandler("start", on_start))
|
||||||
|
dispatcher.add_handler(CommandHandler("help", on_start))
|
||||||
dispatcher.add_handler(MessageHandler(filters.Filters.photo, on_img_received))
|
dispatcher.add_handler(MessageHandler(filters.Filters.photo, on_img_received))
|
||||||
dispatcher.add_handler(CommandHandler("finish", on_finish))
|
dispatcher.add_handler(CommandHandler("finish", on_finish))
|
||||||
dispatcher.add_handler(CommandHandler("cancel", on_cancel))
|
dispatcher.add_handler(CommandHandler("cancel", on_cancel))
|
||||||
|
|
Loading…
Reference in New Issue