baremetrics: Simplify core handler code.

This commit is contained in:
neiljp (Neil Pilgrim) 2018-05-27 19:52:31 -07:00 committed by showell
parent ce4404b9db
commit 016011437e

View file

@ -47,13 +47,19 @@ class BaremetricsHandler(object):
''' '''
def handle_message(self, message: Dict[str, Any], bot_handler: Any) -> None: 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()) bot_handler.send_reply(message, self.usage())
return return
if message['content'].lower() == 'list-commands': if content == ['list-commands']:
response = '**Available Commands:** \n' response = '**Available Commands:** \n'
for command, description in zip(self.commands, self.descriptions): for command, description in zip(self.commands, self.descriptions):
response += ' - {} : {}\n'.format(command, description) response += ' - {} : {}\n'.format(command, description)
@ -61,36 +67,32 @@ class BaremetricsHandler(object):
bot_handler.send_reply(message, response) bot_handler.send_reply(message, response)
return return
if message['content'] == '': response = self.generate_response(content)
bot_handler.send_reply(message, 'No Command Specified')
return
response = self.generate_response(message['content'])
bot_handler.send_reply(message, response) bot_handler.send_reply(message, response)
def generate_response(self, command: str) -> str: def generate_response(self, commands: List[str]) -> str:
try: try:
if command.lower() == 'account-info': instruction = commands[0]
if instruction == 'account-info':
return self.get_account_info() return self.get_account_info()
if command.lower() == 'list-sources': if instruction == 'list-sources':
return self.get_sources() return self.get_sources()
part_commands = command.split()
try: try:
if part_commands[0].lower() == 'list-plans': if instruction == 'list-plans':
return self.get_plans(part_commands[1]) return self.get_plans(commands[1])
if part_commands[0].lower() == 'list-customers': if instruction == 'list-customers':
return self.get_customers(part_commands[1]) return self.get_customers(commands[1])
if part_commands[0].lower() == 'list-subscriptions': if instruction == 'list-subscriptions':
return self.get_subscriptions(part_commands[1]) return self.get_subscriptions(commands[1])
if part_commands[0].lower() == 'create-plan': if instruction == 'create-plan':
if len(part_commands) == 8: if len(commands) == 8:
return self.create_plan(part_commands[1:]) return self.create_plan(commands[1:])
else: else:
return 'Invalid number of arguments.' return 'Invalid number of arguments.'