def execute(self, request_data) -> dict: loggers.get(self.bot).get("state_logger").debug('Executing state: ' + str(self), extra={'uid': request_data.get('session', False)}) request_data.update({'next_state': self.transitions.get('next_state', False)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) request_data['context'].update(request_data.get("payload", {})) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("state_logger").debug( 'Executing state: ' + str(self), extra={'uid': request_data.get('session', False)}) request_data.update( {'next_state': self.transitions.get('next_state', False)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) request_data['context'].update(request_data.get("payload", {})) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("state_logger").debug('Executing state: ' + str(self), extra={'uid': request_data.get('session', False)}) old_response = request_data.get('response', False) checkboxes = self.properties['checkboxes'] for checkbox in checkboxes: new_checkbox = checkbox.copy() new_checkbox['label'] = State.contextualize(request_data['context'], new_checkbox['label']) message = {'type': 'checkbox', 'payload': new_checkbox, 'delay': self.properties['delay']} loggers.get(self.bot).get("state_logger").debug('Checkbox: ' + str(new_checkbox), 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)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug('Response: ' + text, extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("state_logger").debug('Executing state: ' + str(self), extra={'uid': request_data.get('session', False)}) old_response = request_data.get('response', False) entities = State.contextualize(request_data['context'], self.properties['entities']) max_value = State.contextualize(request_data['context'], self.properties['max_value']) min_value = State.contextualize(request_data['context'], self.properties['min_value']) default_values = State.contextualize(request_data['context'], self.properties['default_values']) connect = State.contextualize(request_data['context'], self.properties['connect']) step = State.contextualize(request_data['context'], self.properties['step']) tooltips = State.contextualize(request_data['context'], self.properties['tooltips']) tooltips_decimals = State.contextualize(request_data['context'], self.properties['tooltips_decimals']) tooltips_prefix = State.contextualize(request_data['context'], self.properties['tooltips_prefix']) tooltips_postfix = State.contextualize(request_data['context'], self.properties['tooltips_postfix']) message = {'type': 'slider', 'payload': {'entities': entities, 'max_value': max_value, 'min_value': min_value, 'default_values': default_values, 'step': step, 'connect': connect, 'tooltips': tooltips, 'tooltips_decimals': tooltips_decimals, 'tooltips_prefix': tooltips_prefix, 'tooltips_postfix': tooltips_postfix}, 'delay': self.properties['delay']} loggers.get(self.bot).get("state_logger").debug('Slider', 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)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("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']} loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("state_logger").debug( 'Executing state: ' + str(self), extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'User message: ' + request_data['text'], extra={'uid': request_data.get('session', False)}) # Get nlp type to use for processing input if 'nlp_type' in self.properties: nlp_type = self.properties['nlp_type'] else: nlp_type = None response = get_entities(request_data['text'], nlp_type) loggers.get(self.bot).get("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) intents_project = intent_transitions[self.bot] if response_intent: if intents_project.get( response_intent, False) and response_intent != request_data['context'].get( 'intent', False): loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) request_data['context'].update(response) request_data.update({ 'next_state': intents_project.get(response_intent, False) }) loggers.get(self.bot).get("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']: loggers.get(self.bot).get("state_logger").debug( 'Checking required entities: ' + str(self.properties.get('entities', False)), extra={'uid': request_data.get('session', False)}) if self.check_response(response): loggers.get(self.bot).get("state_logger").debug( 'PASS', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data else: loggers.get(self.bot).get("state_logger").debug( 'FAIL', extra={'uid': request_data.get('session', False)}) request_data.update( {'next_state': self.transitions.get('notmatch', False)}) loggers.get(self.bot).get("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) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("state_logger").debug( 'Executing state: ' + str(self), extra={'uid': request_data.get('session', False)}) response = request_data['context'].get('latest', {}) loggers.get(self.bot).get("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 intent_transitions.get( response_intent, False) and response_intent != request_data['context'].get( 'intent', False): loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("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) }) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("state_logger").debug('Executing state: ' + str(self), extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('User message: ' + request_data['text'], extra={'uid': request_data.get('session', False)}) # Get nlp type to use for processing input if 'nlp_type' in self.properties: nlp_type = self.properties['nlp_type'] else: nlp_type = None response = get_entities(request_data['text'], nlp_type) loggers.get(self.bot).get("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) intents_project = intent_transitions[self.bot] if response_intent: if intents_project.get(response_intent, False) and response_intent != request_data['context'].get( 'intent', False): loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) request_data['context'].update(response) request_data.update({'next_state': intents_project.get(response_intent, False)}) loggers.get(self.bot).get("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']: loggers.get(self.bot).get("state_logger").debug( 'Checking required entities: ' + str(self.properties.get('entities', False)), extra={'uid': request_data.get('session', False)}) if self.check_response(response): loggers.get(self.bot).get("state_logger").debug('PASS', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data else: loggers.get(self.bot).get("state_logger").debug('FAIL', extra={'uid': request_data.get('session', False)}) request_data.update({'next_state': self.transitions.get('notmatch', False)}) loggers.get(self.bot).get("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) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("state_logger").debug('Executing state: ' + str(self), extra={'uid': request_data.get('session', False)}) response = request_data['context'].get('latest', {}) loggers.get(self.bot).get("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 intent_transitions.get(response_intent, False) and response_intent != request_data['context'].get( 'intent', False): loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug('State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug('Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def process_request(bot, state_name, context, text, session, payload): if text == '!undo': prev_context = context.get('previous_context', {}) request_data = { 'context': prev_context, 'text': prev_context.get('previous_text', ''), 'session': session, 'next_state': context.get('previous_request', 'init'), 'payload': prev_context.get('previous_payload', {}) } state_name = prev_context.get('previous_request', 'init') loggers.get(bot).get("main_logger").info("UNDO", extra={'uid': session}) dialogue_logger.log("UNDO", session) loggers.get(bot).get("main_logger").info("GOTO: " + str(state_name), extra={'uid': session}) dialogue_logger.log("GOTO: " + str(state_name), session) elif text == '!reset': prev_context = context.get('previous_context', {}) request_data = { 'context': { 'previous_context': context.get('previous_context', {}) }, 'text': context.get('previous_text', ''), 'session': session, 'next_state': 'init', 'payload': {} } state_name = 'init' loggers.get(bot).get("main_logger").info("RESET", extra={'uid': session}) dialogue_logger.log("RESET", session) loggers.get(bot).get("main_logger").info("GOTO: " + str(state_name), extra={'uid': session}) dialogue_logger.log("GOTO: " + str(state_name), session) elif "@YodaQA" in text: request_data = { 'context': context, 'text': context.get('previous_text', ''), 'session': session } state_name = context.get('previous_request', 'init') text = process_question(text[8:]) response = [{'type': 'text', 'payload': {'text': text}, 'delay': 0}] request_data.update({ "response": response, 'next_state': context.get("previous_request", "init") }) else: request_data = { 'context': context, 'text': text, 'session': session, 'payload': payload } context.update({ 'previous_context': copy.deepcopy(context), 'previous_request': state_name, 'previous_text': text, 'previous_payload': copy.deepcopy(payload) }) if text != '' and text != '!undo' and text != '!reset': loggers.get(bot).get("main_logger").info("USER SAYS: " + request_data['text'], extra={'uid': session}) dialogue_logger.log("USER SAYS: " + request_data['text'], session) while True: loggers.get(bot).get("main_logger").debug("Entering State: " + state_name, extra={'uid': session}) current_state = build_state(bot, state_name) dialogue_logger.log("Entering State: " + str(current_state), session) dialogue_logger.log("Actual context: " + str(context), session) request_data = current_state.execute(request_data) has_next = request_data.get('next_state', False) if has_next: next_type = str( state_dict.get(bot)['states'][has_next].get( 'type', lambda: "nothing")) if next_type == str( '<class \'states.user_input.InputUser\'>' ) or next_type == str( '<class \'states.user_input.InputSpecial\'>'): if request_data.get('response', '') != '': loggers.get(bot).get("main_logger").info( "BOT SAYS: " + str(request_data['response']), extra={'uid': session}) dialogue_logger.log( "BOT SAYS: " + str(request_data['response']), session) if next_type == str( '<class \'states.user_input.InputSpecial\'>'): show = str( state_dict.get(bot)['states'][has_next].get( 'properties').get("show_input", "both")) request_data.update({"input": show}) else: request_data.update({"input": "both"}) return request_data state_name = has_next else: request_data.update({'next_state': 'END'}) if request_data.get('response', '') != '': loggers.get(bot).get("main_logger").info( "BOT SAYS: " + str(request_data['response']), extra={'uid': session}) dialogue_logger.log( "BOT SAYS: " + str(request_data['response']), session) loggers.get(bot).get("main_logger").info( "===== SESSION END =====", extra={'uid': session}) dialogue_logger.log("===== SESSION END =====", session) request_data.update({"input": "both"}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("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']) loggers.get(self.bot).get("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: loggers.get(self.bot).get("state_logger").debug( 'PASS', extra={'uid': request_data.get('session', False)}) request_data.update( {'next_state': self.transitions.get('equal', False)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data else: loggers.get(self.bot).get("state_logger").debug( 'FAIL', extra={'uid': request_data.get('session', False)}) request_data.update( {'next_state': self.transitions.get('notequal', False)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data
def execute(self, request_data) -> dict: loggers.get(self.bot).get("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) loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("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)}) loggers.get(self.bot).get("state_logger").debug( 'State ' + self.name + ' complete.', extra={'uid': request_data.get('session', False)}) loggers.get(self.bot).get("state_logger").debug( 'Next state: ' + str(request_data.get('next_state')), extra={'uid': request_data.get('session', False)}) return request_data