コード例 #1
0
ファイル: run.py プロジェクト: KTH/Jan-Ove
def handle_command(slack_client, args, channel, user):
    try:
        log = logging.getLogger(__name__)
        log.info('Handling cmd "%s" on ch "%s" and user "%s"', args, channel,
                 user)
        trigger_text = os.environ.get('BOT_TRIGGER') or '!pingis'
        default_response = f'Not sure what you mean. Use *{trigger_text} help* for help'
        split_args = util.args_to_commands(args)
        log.debug('split_args is "%s"', split_args)
        main_command = split_args[0]
        response = None
        command_json = commands.is_valid_command(main_command)
        if command_json:
            if (len(split_args) - 1) != command_json['params']:
                response = (f'Wrong number of arguments for command. '
                            f'Should be {command_json["params"]} '
                            f'but was {len(split_args) - 1}')
            else:
                response = command_json['func'](slack_client, split_args)

    except ReadTimeout as error:
        log.error('Error while handling command: %s', error)
        response = (
            'Sorry, the :whale: refused to do as it was told. Try again ...\n'
            '```{}```'.format(error))
    if isinstance(response, list):
        slack.send_block_message(slack_client, channel, response)
    else:
        slack.send_message(slack_client, channel, response, default_response)
コード例 #2
0
 def test_3_words_normal_quotes(self):
     test_data = 'new-season "name of season"'
     result = util.args_to_commands(test_data)
     self.assertEqual(len(result), 2)
     self.assertEqual(result, ['new-season', 'name of season'])
コード例 #3
0
 def test_multiple_args_no_quotes(self):
     test_data = 'register-result @tinglev @hoyce 10 0'
     result = util.args_to_commands(test_data)
     self.assertEqual(len(result), 5)
     self.assertEqual(result,
                      ['register-result', '@tinglev', '@hoyce', '10', '0'])
コード例 #4
0
 def test_curly_quotes(self):
     test_data = 'new-season “name of season”'
     result = util.args_to_commands(test_data)
     self.assertEqual(len(result), 2)
     self.assertEqual(result, ['new-season', 'name of season'])