Esempio n. 1
0
    def execute(self, request_data) -> dict:
        state_logger.debug('Executing state: ' + str(self),
                           extra={'uid': request_data.get('session', False)})
        old_response = request_data.get('response', False)
        butts = self.properties['buttons']
        for butt in butts:
            butt['label'] = State.contextualize(request_data['context'],
                                                butt['label'])
            message = {
                'type': 'button',
                'payload': butt,
                'delay': self.properties['delay']
            }
            state_logger.debug(
                'Button: ' + str(butt),
                extra={'uid': request_data.get('session', False)})

            if old_response:
                old_response.append(message)
            else:
                old_response = [message]
        request_data.update({
            'response':
            old_response,
            'next_state':
            self.transitions.get('next_state', False)
        })
        state_logger.debug('State ' + self.name + ' complete.',
                           extra={'uid': request_data.get('session', False)})
        state_logger.debug('Next state: ' +
                           str(request_data.get('next_state')),
                           extra={'uid': request_data.get('session', False)})
        return request_data
Esempio n. 2
0
    def execute(self, request_data) -> dict:
        state_logger.debug('Executing state: ' + str(self),
                           extra={'uid': request_data.get('session', False)})

        text = State.contextualize(request_data['context'],
                                   self.properties['text'])  # Add context
        old_response = request_data.get('response', False)
        message = {
            'type': 'text',
            'payload': {
                'text': text
            },
            'delay': self.properties['delay']
        }
        if old_response:
            old_response.append(message)
        else:
            old_response = [message]

        request_data.update({
            'response':
            old_response,
            'next_state':
            self.transitions.get('next_state', False)
        })

        state_logger.debug('Response: ' + text,
                           extra={'uid': request_data.get('session', False)})
        state_logger.debug('State ' + self.name + ' complete.',
                           extra={'uid': request_data.get('session', False)})
        state_logger.debug('Next state: ' +
                           str(request_data.get('next_state')),
                           extra={'uid': request_data.get('session', False)})

        return request_data
Esempio n. 3
0
 def execute(self, request_data) -> dict:
     state_logger.debug('Executing state: ' + str(self),
                        extra={'uid': request_data.get('session', False)})
     old_response = request_data.get('response', False)
     url = State.contextualize(request_data['context'],
                               self.properties['url'])
     width = State.contextualize(request_data['context'],
                                 self.properties['width'])
     height = State.contextualize(request_data['context'],
                                  self.properties['height'])
     scrolling = State.contextualize(request_data['context'],
                                     self.properties['scrolling'])
     align = State.contextualize(request_data['context'],
                                 self.properties['align'])
     message = {
         'type': 'iframe',
         'payload': {
             'url': url,
             'width': width,
             'height': height,
             'scrolling': scrolling,
             'align': align
         },
         'delay': self.properties['delay']
     }
     state_logger.debug('Iframe: ' + str(url),
                        extra={'uid': request_data.get('session', False)})
     if old_response:
         old_response.append(message)
     else:
         old_response = [message]
     request_data.update({
         'response':
         old_response,
         'next_state':
         self.transitions.get('next_state', False)
     })
     state_logger.debug('State ' + self.name + ' complete.',
                        extra={'uid': request_data.get('session', False)})
     state_logger.debug('Next state: ' +
                        str(request_data.get('next_state')),
                        extra={'uid': request_data.get('session', False)})
     return request_data
Esempio n. 4
0
    def execute(self, request_data) -> dict:
        state_logger.debug('Executing state: ' + str(self),
                           extra={'uid': request_data.get('session', False)})
        state_logger.debug('User message: ' + request_data['text'],
                           extra={'uid': request_data.get('session', False)})

        response = get_entities(request_data['text'])
        state_logger.debug('NLP output: ' + str(response),
                           extra={'uid': request_data.get('session', False)})

        # Log latest user response to context
        if self.properties['log_json']:
            request_data['context'].update({'latest': response})

        # Switch intent according to user response
        response_intent = response.get('intent', False)
        if response_intent:
            if request_data['context'].get(
                    'intent', False) and intent_transitions.get(
                        response_intent, False
                    ) and response_intent != request_data['context'].get(
                        'intent', False):

                state_logger.debug(
                    'Switching intent: current intent: ' +
                    str(request_data['context'].get('intent', False)) +
                    ', user intent: ' + str(response_intent),
                    extra={'uid': request_data.get('session', False)})
                state_logger.debug(
                    'State ' + self.name + ' complete.',
                    extra={'uid': request_data.get('session', False)})
                request_data['context'].update(response)
                request_data.update({
                    'next_state':
                    intent_transitions.get(response_intent, False)
                })

                state_logger.debug(
                    'Next state: ' + str(request_data.get('next_state')),
                    extra={'uid': request_data.get('session', False)})
                return request_data

        # Require entity match check
        if self.properties['require_match']:
            state_logger.debug(
                'Checking required entities: ' +
                str(self.properties.get('entities', False)),
                extra={'uid': request_data.get('session', False)})

            if self.check_response(response):
                state_logger.debug(
                    'PASS', extra={'uid': request_data.get('session', False)})
                state_logger.debug(
                    'Updating context...\rContext: ' +
                    str(request_data.get('context', False)) + '\rUpdate: ' +
                    str(response),
                    extra={'uid': request_data.get('session', False)})
                self.update_context(request_data['context'], response)
                request_data.update(
                    {'next_state': self.transitions.get('match', False)})
                state_logger.debug(
                    'State ' + self.name + ' complete.',
                    extra={'uid': request_data.get('session', False)})
                state_logger.debug(
                    'Next state: ' + str(request_data.get('next_state')),
                    extra={'uid': request_data.get('session', False)})

                return request_data

            else:
                state_logger.debug(
                    'FAIL', extra={'uid': request_data.get('session', False)})
                request_data.update(
                    {'next_state': self.transitions.get('notmatch', False)})
                state_logger.debug(
                    'Updating context...\rContext: ' +
                    str(request_data.get('context', False)) + '\rUpdate: ' +
                    str(response),
                    extra={'uid': request_data.get('session', False)})
                self.update_context(request_data['context'], response)
                state_logger.debug(
                    'State ' + self.name + ' complete.',
                    extra={'uid': request_data.get('session', False)})
                state_logger.debug(
                    'Next state: ' + str(request_data.get('next_state')),
                    extra={'uid': request_data.get('session', False)})

                return request_data
        state_logger.debug('Updating context...\rContext: ' +
                           str(request_data.get('context', False)) +
                           '\rUpdate: ' + str(response),
                           extra={'uid': request_data.get('session', False)})
        self.update_context(request_data['context'], response)
        request_data.update(
            {'next_state': self.transitions.get('next_state', False)})
        state_logger.debug('State ' + self.name + ' complete.',
                           extra={'uid': request_data.get('session', False)})
        state_logger.debug('Next state: ' +
                           str(request_data.get('next_state')),
                           extra={'uid': request_data.get('session', False)})

        return request_data
Esempio n. 5
0
    def execute(self, request_data) -> dict:
        state_logger.debug('Executing state: ' + str(self),
                           extra={'uid': request_data.get('session', False)})

        response = request_data['context'].get('latest', {})
        state_logger.debug('Latest user message: ' + str(response),
                           extra={'uid': request_data.get('session', False)})

        # Switch intent according to user response
        response_intent = response.get('intent', False)
        if response_intent:
            if request_data['context'].get(
                    'intent', False) and intent_transitions.get(
                        response_intent, False
                    ) and response_intent != request_data['context'].get(
                        'intent', False):

                state_logger.debug(
                    'Switching intent: current intent: ' +
                    str(request_data['context'].get('intent', False)) +
                    ', user intent: ' + str(response_intent),
                    extra={'uid': request_data.get('session', False)})
                state_logger.debug(
                    'State ' + self.name + ' complete.',
                    extra={'uid': request_data.get('session', False)})
                request_data['context'].update(response)
                request_data.update({
                    'next_state':
                    intent_transitions.get(response_intent, False)
                })

                state_logger.debug(
                    'Next state: ' + str(request_data.get('next_state')),
                    extra={'uid': request_data.get('session', False)})
                return request_data

        state_logger.debug('Updating context...\rContext: ' +
                           str(request_data.get('context', False)) +
                           '\rUpdate: ' + str(response),
                           extra={'uid': request_data.get('session', False)})
        self.update_context(request_data['context'], response)
        request_data.update(
            {'next_state': self.transitions.get('next_state', False)})
        state_logger.debug('State ' + self.name + ' complete.',
                           extra={'uid': request_data.get('session', False)})
        state_logger.debug('Next state: ' +
                           str(request_data.get('next_state')),
                           extra={'uid': request_data.get('session', False)})

        return request_data
Esempio n. 6
0
    def execute(self, request_data) -> dict:
        state_logger.debug('Executing state: ' + str(self),
                           extra={'uid': request_data.get('session', False)})

        val1 = State.contextualize(request_data['context'],
                                   self.properties['value1'])
        val2 = State.contextualize(request_data['context'],
                                   self.properties['value2'])
        state_logger.debug('Comparing values: ' +
                           str(self.properties['value1']) + '\rContext: ' +
                           str(request_data.get('context', False)),
                           extra={'uid': request_data.get('session', False)})

        if val1 == val2:
            state_logger.debug(
                'PASS', extra={'uid': request_data.get('session', False)})
            request_data.update(
                {'next_state': self.transitions.get('equal', False)})
            state_logger.debug(
                'State ' + self.name + ' complete.',
                extra={'uid': request_data.get('session', False)})
            state_logger.debug(
                'Next state: ' + str(request_data.get('next_state')),
                extra={'uid': request_data.get('session', False)})
            return request_data
        else:
            state_logger.debug(
                'FAIL', extra={'uid': request_data.get('session', False)})
            request_data.update(
                {'next_state': self.transitions.get('notequal', False)})
            state_logger.debug(
                'State ' + self.name + ' complete.',
                extra={'uid': request_data.get('session', False)})
            state_logger.debug(
                'Next state: ' + str(request_data.get('next_state')),
                extra={'uid': request_data.get('session', False)})
            return request_data
Esempio n. 7
0
    def execute(self, request_data) -> dict:
        state_logger.debug('Executing state: ' + str(self),
                           extra={'uid': request_data.get('session', False)})

        m = re.search('(?<={{)(.*?)(?=}})', self.properties['key'])
        if m:
            entity = m.group(1)
            state_logger.debug(
                'Checking key' + entity + '\rContext: ' +
                str(request_data.get('context', False)),
                extra={'uid': request_data.get('session', False)})

            if request_data['context'].get(entity, False):
                request_data.update(
                    {'next_state': self.transitions.get('exists', False)})
                state_logger.debug(
                    'State ' + self.name + ' complete.',
                    extra={'uid': request_data.get('session', False)})
                state_logger.debug(
                    'Next state: ' + str(request_data.get('next_state')),
                    extra={'uid': request_data.get('session', False)})
                return request_data
        request_data.update(
            {'next_state': self.transitions.get('notexists', False)})
        state_logger.debug('State ' + self.name + ' complete.',
                           extra={'uid': request_data.get('session', False)})
        state_logger.debug('Next state: ' +
                           str(request_data.get('next_state')),
                           extra={'uid': request_data.get('session', False)})
        return request_data