autoformat & reasonable /start command

This commit is contained in:
Tomáš Mládek 2021-09-19 11:48:29 +02:00
parent 7e6a761363
commit 7e41fe054e

View file

@ -16,15 +16,6 @@ from telegram.ext import (
) )
def start(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /start is issued."""
user = update.effective_user
update.message.reply_markdown_v2(
fr"Hi {user.mention_markdown_v2()}\!",
reply_markup=ForceReply(selective=True),
)
class KollagenBot: class KollagenBot:
def __init__( def __init__(
self, tg_token: str, kollagen_path: str, base_dir: Optional[str] self, tg_token: str, kollagen_path: str, base_dir: Optional[str]
@ -37,7 +28,7 @@ class KollagenBot:
dispatcher = self.updater.dispatcher dispatcher = self.updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start)) dispatcher.add_handler(CommandHandler("start", self.tg_start))
dispatcher.add_handler(CommandHandler("list_modes", self.tg_list_modes)) dispatcher.add_handler(CommandHandler("list_modes", self.tg_list_modes))
dispatcher.add_handler(CommandHandler("generate", self.tg_generate)) dispatcher.add_handler(CommandHandler("generate", self.tg_generate))
dispatcher.add_handler(CommandHandler("g", self.tg_generate)) dispatcher.add_handler(CommandHandler("g", self.tg_generate))
@ -45,6 +36,10 @@ class KollagenBot:
dispatcher.add_handler(CommandHandler("r", self.tg_regenerate)) dispatcher.add_handler(CommandHandler("r", self.tg_regenerate))
dispatcher.add_error_handler(self.tg_error) dispatcher.add_error_handler(self.tg_error)
def tg_start(self, update: Update, context: CallbackContext):
update.message.reply_text("Hi! Check out https://gitlab.com/tmladek/kollagen")
self._process("", update)
def tg_generate(self, update: Update, context: CallbackContext): def tg_generate(self, update: Update, context: CallbackContext):
cmd_line = update.message.text.split(" ")[1:] cmd_line = update.message.text.split(" ")[1:]
self._process(cmd_line, update) self._process(cmd_line, update)
@ -58,7 +53,9 @@ class KollagenBot:
update.message.reply_text("No previous command to regenerate!") update.message.reply_text("No previous command to regenerate!")
def _process(self, cmd_line: str, update: Update): def _process(self, cmd_line: str, update: Update):
self.logger.info(f"Generating from {update.effective_user}, with cmd_line: `{cmd_line}`") self.logger.info(
f"Generating from {update.effective_user}, with cmd_line: `{cmd_line}`"
)
directories = [ directories = [
os.path.join(self.base_dir or "./", re.sub(r"[^a-zA-Z0-9]", "", arg)) os.path.join(self.base_dir or "./", re.sub(r"[^a-zA-Z0-9]", "", arg))
for arg in cmd_line for arg in cmd_line
@ -88,7 +85,9 @@ class KollagenBot:
if isinstance(update, Update): if isinstance(update, Update):
if isinstance(context.error, subprocess.CalledProcessError): if isinstance(context.error, subprocess.CalledProcessError):
update.message.reply_text(f"Something is fucked!\n{context.error.stderr.decode('utf-8')}") update.message.reply_text(
f"Something is fucked!\n{context.error.stderr.decode('utf-8')}"
)
else: else:
update.message.reply_text(f"Something is fucked!\n{context.error}") update.message.reply_text(f"Something is fucked!\n{context.error}")