예제 #1
0
 def test_is_direct_message(self):
     sample_channel_id_1 = "C2147483705"
     sample_channel_id_2 = "D1235792374"
     self.assertEqual(slack_clients.is_direct_message(sample_channel_id_1),
                      None)
     self.assertIsNotNone(
         type(slack_clients.is_direct_message(sample_channel_id_2)))
예제 #2
0
    def get_credential(self, event, state_id, user=None):
        """
        Returns either the user's credentials, or starts the credentialing process if no credentials can be found
        :return: a credentials object, or None if there is no credentials associated with the user
        """
        if user is None:
            user = self.default_user
        try:
            if self._credentials_dict[user].access_token_expired:
                raise GoogleAccessError
            return self._credentials_dict[user]
        except KeyError or GoogleAccessError:
            # create and encrypt state
            state = {'state_id': str(state_id.hex), 'user_id': user}
            encrypted_state = self.crypt.encrypt(json.dumps(state).encode('utf-8'))

            # generate flow, and begin auth
            flow = client.OAuth2WebServerFlow(client_id=os.getenv("GOOGLE_CLIENT_ID", ""),
                                              client_secret=os.getenv("GOOGLE_CLIENT_SECRET", ""),
                                              scope=SCOPES,
                                              redirect_uri=os.getenv("CALLBACK_URI", ""))
            flow.params['access_type'] = 'offline'
            flow.params['prompt'] = 'consent'
            logger.info("flow {}".format(flow.params))
            auth_uri = flow.step1_get_authorize_url(state=encrypted_state)
            if not is_direct_message(event['channel']):
                self.msg_writer.send_message(event['channel'],
                                             "I'll send you the authorization link in a direct message")
            channel = event['user_dm']
            self.msg_writer.send_message_with_attachments(channel, "Authorization Link", [{'text': "<{}|Click here to authorize>".format(auth_uri)}])
            return None
예제 #3
0
    def _proof_message(self, event):
        """
        :param event: The triggering message event
        Checks the event to see if this is a message that should be processed
        :return: Bool indicating whether or not the Rtm should continue processing the message
        """
        # Event won't have a user if slackbot is unfurling messages for you
        if 'user' not in event:
            return False

        # Filter out messages from the bot itself
        if self.clients.is_message_from_me(event['user']):
            return False

        msg_txt = event['text']
        channel_id = event['channel']

        # Filter out message unless this bot is mentioned or it is a direct message
        if not (is_direct_message(channel_id) or self.clients.is_bot_mention(msg_txt)):
            return False

        # Ensure that we don't go to wit with messages posted by an ignored user
        if event['user'] in user_ignore_list:
            return False

        return True
예제 #4
0
    def _handle_message(self, event):

        if not self._proof_message(event):
            return

        msg_txt = event['text']
        channel_id = event['channel']

        # Remove mention of the bot so that the rest of the code doesn't need to
        msg_txt = self.clients.remove_mention(msg_txt).strip()

        # Ask wit to interpret the text and send back a list of entities
        logger.info("Asking wit to interpret| {}".format(msg_txt))
        wit_resp = self.wit_client.interpret(msg_txt)

        # Add username and channel name, user dm, and cleaned text to the event object
        user_name = self.clients.get_user_name_from_id(event['user'])
        if is_direct_message(channel_id):
            channel_name = "Direct Message"
        else:
            channel_name = self.clients.get_channel_name_from_id(channel_id)
        event.update({
            "user_name": user_name,
            "channel_name": channel_name,
            "user_dm": self.clients.get_dm_id_from_user_id(event['user']),
            "cleaned_text": msg_txt
        })

        # Find the intent with the highest confidence that met our default threshold
        intent_entity = get_highest_confidence_entity(wit_resp['entities'], 'intent')

        # If we couldn't find an intent entity, let the user know
        if intent_entity is None:
            self.msg_writer.write_prompt(channel_id, self.intents)
            return

        intent_value = intent_entity['value']
        if intent_value in conversation_intent_types:
            match = self._conversation_match(intent_value, wit_resp, event)
            if match:
                event.update({"conversation": match})

        if intent_value in self.intents:
            t = {
                'intent': self.intents[intent_value][0],
                'msg_writer': self.msg_writer,
                'event': event,
                'wit_entities': wit_resp['entities'],
                'credentials': self.credentials,
                'state_q': self.state_updating_q
            }
            self.event_processing_q.put(t)

        else:
            raise ReferenceError("No function found to handle intent {}".format(intent_value))
예제 #5
0
    def _handle_message(self, event):

        # Event won't have a user if slackbot is unfurling messages for you
        if 'user' not in event:
            return

        # Filter out messages from the bot itself
        if self.clients.is_message_from_me(event['user']):
            return

        msg_txt = event['text']
        channel_id = event['channel']

        # Filter out message unless this bot is mentioned or it is a direct message
        if not (is_direct_message(channel_id) or self.clients.is_bot_mention(msg_txt)):
            return

        # Remove mention of the bot so that the rest of the code doesn't need to
        msg_txt = self.clients.remove_mention(msg_txt).strip()

        # Ensure that we don't go to wit with messages posted by an ignored user
        if event['user'] in user_ignore_list:
            return

        # bot_uid = self.clients.bot_user_id()

        # Ask wit to interpret the text and send back a list of entities
        logger.info("Asking wit to interpret| {}".format(msg_txt))
        wit_resp = self.wit_client.interpret(msg_txt)

        # Find the intent with the highest confidence that met our default threshold
        intent_entity = get_highest_confidence_entity(wit_resp['entities'], 'intent')

        # If we couldn't find an intent entity, let the user know
        if intent_entity is None:
            self.msg_writer.write_prompt(channel_id, intents)
            return

        intent_value = intent_entity['value']
        if intent_value in intents:
            intents[intent_value][0](self.msg_writer, event, wit_resp['entities'])
        else:
            raise ReferenceError("No function found to handle intent {}".format(intent_value))
 def test_is_direct_message(self):
     sample_channel_id_1 = "C2147483705"
     sample_channel_id_2 = "D1235792374"
     self.assertEqual(slack_clients.is_direct_message(sample_channel_id_1), None)
     self.assertIsNotNone(type(slack_clients.is_direct_message(sample_channel_id_2)))