From 016011437e39b5043fbb71329915c0671bde5c53 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sun, 27 May 2018 19:52:31 -0700 Subject: [PATCH] baremetrics: Simplify core handler code. --- .../bots/baremetrics/baremetrics.py | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py index f3c56c8..f7d648b 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py @@ -47,13 +47,19 @@ class BaremetricsHandler(object): ''' def handle_message(self, message: Dict[str, Any], bot_handler: Any) -> None: - message['content'] = message['content'].strip() + content = message['content'].strip().split() - if message['content'].lower() == 'help': + if content == []: + bot_handler.send_reply(message, 'No Command Specified') + return + + content[0] = content[0].lower() + + if content == ['help']: bot_handler.send_reply(message, self.usage()) return - if message['content'].lower() == 'list-commands': + if content == ['list-commands']: response = '**Available Commands:** \n' for command, description in zip(self.commands, self.descriptions): response += ' - {} : {}\n'.format(command, description) @@ -61,36 +67,32 @@ class BaremetricsHandler(object): bot_handler.send_reply(message, response) return - if message['content'] == '': - bot_handler.send_reply(message, 'No Command Specified') - return - - response = self.generate_response(message['content']) + response = self.generate_response(content) bot_handler.send_reply(message, response) - def generate_response(self, command: str) -> str: + def generate_response(self, commands: List[str]) -> str: try: - if command.lower() == 'account-info': + instruction = commands[0] + + if instruction == 'account-info': return self.get_account_info() - if command.lower() == 'list-sources': + if instruction == 'list-sources': return self.get_sources() - part_commands = command.split() - try: - if part_commands[0].lower() == 'list-plans': - return self.get_plans(part_commands[1]) + if instruction == 'list-plans': + return self.get_plans(commands[1]) - if part_commands[0].lower() == 'list-customers': - return self.get_customers(part_commands[1]) + if instruction == 'list-customers': + return self.get_customers(commands[1]) - if part_commands[0].lower() == 'list-subscriptions': - return self.get_subscriptions(part_commands[1]) + if instruction == 'list-subscriptions': + return self.get_subscriptions(commands[1]) - if part_commands[0].lower() == 'create-plan': - if len(part_commands) == 8: - return self.create_plan(part_commands[1:]) + if instruction == 'create-plan': + if len(commands) == 8: + return self.create_plan(commands[1:]) else: return 'Invalid number of arguments.'