Ejemplo n.º 1
0
    def handle_app_command(self, cmd_txt: str, user: str) -> ResponseTuple:
        """
        Handle a command call to rocket.

        :param cmd_txt: the command itself
        :param user: slack ID of user who executed the command
        :return: tuple where first element is the response text (or a
                 ``flask.Response`` object), and the second element
                 is the response status code
        """
        # Slightly hacky way to deal with Apple platform
        # smart punctuation messing with argparse.
        cmd_txt = ''.join(map(util.regularize_char, cmd_txt))
        cmd_txt = util.escaped_id_to_id(cmd_txt)
        s = cmd_txt.split(' ', 1)
        if s[0] == "help" or s[0] is None:
            logging.info("Help command was called")
            return self.get_help(), 200
        if s[0] in self.__commands:
            return self.__commands[s[0]].handle(cmd_txt, user)
        elif is_slack_id(s[0]):
            logging.info("mention command activated")
            return self.__commands["mention"].handle(cmd_txt, user)
        else:
            logging.error("app command triggered incorrectly")
            return 'Please enter a valid command', 200
Ejemplo n.º 2
0
    def handle_app_command(self,
                           cmd_txt: str,
                           user: str,
                           response_url: str):
        """
        Handle a command call to rocket.

        :param cmd_txt: the command itself
        :param user: slack ID of user who executed the command
        :return: tuple where first element is the response text (or a
                 ``flask.Response`` object), and the second element
                 is the response status code
        """
        start_time_ms = time.time() * 1000

        # Slightly hacky way to deal with Apple platform
        # smart punctuation messing with argparse.
        cmd_txt = ''.join(map(util.regularize_char, cmd_txt))
        cmd_txt = util.escaped_id_to_id(cmd_txt)
        cmd_txt = util.ios_dash(cmd_txt)
        s = cmd_txt.split(' ', 1)
        cmd_name = 'help'
        if s[0] == 'help' or s[0] is None:
            logging.info('Help command was called')
            v = self.get_help()
        elif s[0] in self.commands:
            v = self.commands[s[0]].handle(cmd_txt, user)

            # Hack to only grab first 2 command/subcommand pair
            s = cmd_txt.split(' ')
            if '-' in s[1]:
                cmd_name = s[0]
            else:
                cmd_name = ' '.join(s[0:2])
        elif is_slack_id(s[0]):
            logging.info('mention command activated')
            v = self.commands['mention'].handle(cmd_txt, user)
            cmd_name = 'mention'
        else:
            logging.error("app command triggered incorrectly")
            v = self.get_help()
        if isinstance(v[0], str):
            response_data: Any = {'text': v[0]}
        else:
            response_data = v[0]

        # Submit metrics
        duration_taken_ms = time.time() * 1000 - start_time_ms
        self.__metrics.submit_cmd_mstime(cmd_name, duration_taken_ms)

        if response_url != "":
            requests.post(url=response_url, json=response_data)
        else:
            return v
Ejemplo n.º 3
0
    def test_escaped_id_conversion(self):
        """Test how this function reacts to normal operation."""
        CMDS = [
            # Normal operation
            ('/rocket user edit --username <@U1234|user> --name "User"',
             '/rocket user edit --username U1234 --name "User"'),
            # No users
            ('/rocket user view', '/rocket user view'),
            # Multiple users
            ('/rocket foo <@U1234|user> <@U4321|ruse> <@U3412|sure> -h',
             '/rocket foo U1234 U4321 U3412 -h')
        ]

        for inp, expect in CMDS:
            self.assertEqual(util.escaped_id_to_id(inp), expect)
Ejemplo n.º 4
0
    def handle_app_command(self,
                           cmd_txt: str,
                           user: str,
                           response_url: str):
        """
        Handle a command call to rocket.

        :param cmd_txt: the command itself
        :param user: slack ID of user who executed the command
        :return: tuple where first element is the response text (or a
                 ``flask.Response`` object), and the second element
                 is the response status code
        """
        # Slightly hacky way to deal with Apple platform
        # smart punctuation messing with argparse.
        cmd_txt = ''.join(map(util.regularize_char, cmd_txt))
        cmd_txt = util.escaped_id_to_id(cmd_txt)
        s = cmd_txt.split(' ', 1)
        if s[0] == "help" or s[0] is None:
            logging.info("Help command was called")
            v = self.get_help()
        if s[0] in self.__commands:
            v = self.__commands[s[0]].handle(cmd_txt, user)
        elif is_slack_id(s[0]):
            logging.info("mention command activated")
            v = self.__commands["mention"].handle(cmd_txt, user)
        else:
            logging.error("app command triggered incorrectly")
            v = self.get_help()
        if isinstance(v[0], str):
            response_data: Any = {'text': v[0]}
        else:
            response_data = v[0]
        if response_url != "":
            requests.post(url=response_url, json=response_data)
        else:
            return v