def command(): ''' Recevies commands from the client, parses it and takes appropriate action. ''' # This object will be sent to the client output = { 'commands': [], 'error': False, # Used in the catch block 'final': True, # In the client we'll know if some more information is needed or if the command has been executed 'parsed': {}, # Whatever has been parsed so far 'message': '', # The message to be shown for interactive mode 'type': None, # Type of the next command is. Used if final is False. # Can be one of confirm, option, intent, argument, 'matched':False, # After selection of an option from a list matched is set to True } # "data" from $.ajax will be in request.form command = request.form['input'] # The input command newCommand = request.form['newCommand'] # Flag indicating whether the command is new or a continuation currentSession = request.form['currentSession'] # The device that is currently running oldResult = json.loads(request.form['oldResult']) # Any old results if the command is a continuation output['commands'].append(command) try: result, device, output = core.parse(command, newCommand, oldResult, currentSession, output) output['parsed'] = result if 'dont_execute' in output.keys(): return jsonify(output) if output['parsed']['intent'] == None: # no intent given, so ask user to give one output['final'] = False output['type'] = 'continue' # user needs to provide the correct command output['example'] = device['operations']['examples_intent']['arguments']['example'] # provide the required message and output['message'] = device['operations']['examples_intent']['arguments']['message'] # example in devices.py return jsonify(output) # If the operation requires user confirmation if device['operations'][result['intent']]['confirm'] == True: # If the user has told "no" if 'cancel' in output.keys(): return jsonify(output) output['final'] = False output['type'] = 'confirm' output['message'] = device['operations'][result['intent']]['message'] output['parsed'] = result return jsonify(output) else: output = execute.process(result, device, output) return jsonify(output) except: output = { 'error': True, 'final': True, 'message': 'Something went wrong, please try again' } return jsonify(output)
def command(): ''' Recevies commands from the client, parses it and takes appropriate action. ''' # This object will be sent to the client output = { 'commands': [], 'error': False, # Used in the catch block 'final': True, # In the client we'll know if some more information is needed or if the command has been executed 'parsed': {}, # Whatever has been parsed so far 'message': '', # The message to be shown for interactive mode 'type': None, # Type of the next command is. Used if final is False. # Can be one of confirm, option, intent, argument, 'matched': False, # After selection of an option from a list matched is set to True } # "data" from $.ajax will be in request.form command = request.form['input'] # The input command newCommand = request.form[ 'newCommand'] # Flag indicating whether the command is new or a continuation currentSession = request.form[ 'currentSession'] # The device that is currently running oldResult = json.loads( request.form['oldResult'] ) # Any old results if the command is a continuation output['commands'].append(command) try: result, device, output = core.parse(command, newCommand, oldResult, currentSession, output) output['parsed'] = result if 'dont_execute' in output.keys(): return jsonify(output) if output['parsed'][ 'intent'] == None: # no intent given, so ask user to give one output['final'] = False output[ 'type'] = 'continue' # user needs to provide the correct command output['example'] = device['operations']['examples_intent'][ 'arguments']['example'] # provide the required message and output['message'] = device['operations']['examples_intent'][ 'arguments']['message'] # example in devices.py return jsonify(output) # If the operation requires user confirmation if device['operations'][result['intent']]['confirm'] == True: # If the user has told "no" if 'cancel' in output.keys(): return jsonify(output) output['final'] = False output['type'] = 'confirm' output['message'] = device['operations'][ result['intent']]['message'] output['parsed'] = result return jsonify(output) else: output = execute.process(result, device, output) return jsonify(output) except: output = { 'error': True, 'final': True, 'message': 'Something went wrong, please try again' } return jsonify(output)
def execution_handler(result, device, output): execution_result = execute.process(result, device, output) return execution_result