main: Extract send_help function

pull/1/head
Michael DM Dryden 2020-05-30 00:27:26 -04:00
rodzic 00f57ad1ca
commit 6856b82a25
1 zmienionych plików z 16 dodań i 17 usunięć

33
main.py
Wyświetl plik

@ -96,17 +96,12 @@ def print_stats(update: Update, context: CallbackContext):
ns = stats_parser.parse_args(shlex.split(" ".join(context.args))) ns = stats_parser.parse_args(shlex.split(" ".join(context.args)))
except HelpException as e: except HelpException as e:
text = e.msg text = e.msg
try: send_help(text, context, update)
context.bot.send_message(chat_id=update.effective_user.id,
text=f"```\n{text}\n```",
parse_mode=telegram.ParseMode.MARKDOWN_V2)
except telegram.error.Unauthorized: # If user has never chatted with bot
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"```\n{text}\n```",
parse_mode=telegram.ParseMode.MARKDOWN_V2)
return return
except argparse.ArgumentError as e: except argparse.ArgumentError as e:
text = str(e) text = str(e)
send_help(text, context, update)
return
else: else:
args = vars(ns) args = vars(ns)
func = args.pop('func') func = args.pop('func')
@ -122,24 +117,28 @@ def print_stats(update: Update, context: CallbackContext):
text, image = func(**args) text, image = func(**args)
except HelpException as e: except HelpException as e:
text = e.msg text = e.msg
try: send_help(text, context, update)
context.bot.send_message(chat_id=update.effective_user.id,
text=f"```\n{text}\n```",
parse_mode=telegram.ParseMode.MARKDOWN_V2)
except telegram.error.Unauthorized:
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"```\n{text}\n```",
parse_mode=telegram.ParseMode.MARKDOWN_V2)
return return
if text: if text:
context.bot.send_message(chat_id=update.effective_chat.id, context.bot.send_message(chat_id=update.effective_chat.id,
text=f"```\n{text}\n```", text=text,
parse_mode=telegram.ParseMode.MARKDOWN_V2) parse_mode=telegram.ParseMode.MARKDOWN_V2)
if image: if image:
context.bot.send_photo(chat_id=update.effective_chat.id, photo=image) context.bot.send_photo(chat_id=update.effective_chat.id, photo=image)
def send_help(text, context, update):
try:
context.bot.send_message(chat_id=update.effective_user.id,
text=f"```\n{text}\n```",
parse_mode=telegram.ParseMode.MARKDOWN_V2)
except telegram.error.Unauthorized: # If user has never chatted with bot
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"```\n{text}\n```",
parse_mode=telegram.ParseMode.MARKDOWN_V2)
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('token', type=str, help="Telegram bot token") parser.add_argument('token', type=str, help="Telegram bot token")