Example #1
0
def respond(intent_request):
    """
    fulfill assign intent
    """
    output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
    
    runbook_id = get_slots(intent_request)["RunbookId"]
    current_user = getSlackUserById(intent_request['userId'])

    print('respond::fulfill intentName={}, slots={}'.format(str(intent_request['currentIntent']['name']), str(get_slots(intent_request))))

    runbook=getRunbookById(runbook_id)
    runlog_entry = Runlog.create(runbook_id, runbook['name'], current_user, current_user)
    save(runlog_entry)
    print('respond save runlog={}'.format(str(runlog_entry)))
    output_session_attributes['runlog']=json.dumps(runlog_entry.__dict__)

    print('respond elicit_intent session={}, slots={}'.format(str(output_session_attributes), str(get_slots(intent_request))))
    buttons = [ 
        {"text":"Execute","value": "execute "+runlog_entry.runlogId}
    ]
    response_card = build_response_card('Execute '+runbook_id, runbook['name'], buttons)
    print('response elicit_intent_with_response_card = {}'.format(str(response_card)))

    return elicit_intent_with_response_card(output_session_attributes, 'Added \''+runbook_id+'\' to your TODOs, click Execute to run through now.', response_card)
Example #2
0
def respond(intent_request):
    """
    fulfill assign intent
    """
    output_session_attributes = intent_request[
        'sessionAttributes'] if intent_request[
            'sessionAttributes'] is not None else {}
    current_user = getSlackUserById(intent_request['userId'])
    runbook_id = get_slots(intent_request)["RunbookId"]
    assignee = get_slots(intent_request)["SlackUser"]

    runbook = getRunbookById(runbook_id)
    runlog_entry = Runlog.create(runbook_id, runbook['name'], assignee,
                                 current_user)
    save(runlog_entry)
    print('respond save runlog={}'.format(str(runlog_entry)))
    output_session_attributes['runlog'] = json.dumps(runlog_entry.__dict__)

    print('respond output_session_attributes={}, slots={}'.format(
        str(output_session_attributes), str(get_slots(intent_request))))
    return close(
        output_session_attributes, 'Fulfilled', {
            'contentType': 'PlainText',
            'content': 'Assigned \'' + runbook_id + '\' to ' + assignee
        })
Example #3
0
 def __init__(self, runlog_id, user):
     self.runlogId = runlog_id
     self.user = user
     runbook_id = getRunbookIdForRunlog(self.runlogId, self.user)
     print('ExecutionContext::getRunbook runbook_id={}'.format(
         str(runbook_id)))
     self.runbook = getRunbookById(runbook_id)
     print('ExecutionContext::getRunbook runbook_id={}, runbook={}'.format(
         str(runbook_id), str(self.runbook)))
Example #4
0
def respond(intent_request):
    """
    Performs dialog management for delegating runlog to a user
    """
    intent = intent_request['currentIntent']['name']
    current_user = getSlackUserById(intent_request['userId'])
    source = intent_request['invocationSource']
    userInput = intent_request['inputTranscript'] if 'inputTranscript' in intent_request else None
    output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}

    #get runlog_id from user input or session
    runlog = None
    if userInput is not None and re.search('RL[0-9]+', userInput) is not None:
        #check if user has specified runbook id in utterance
        runlog_id=re.search('RL[0-9]+', userInput).group(0)
        intent_request['currentIntent']['slots']["RunlogId"]=runlog_id            
    elif 'runlog' in output_session_attributes:
        # get runlog from session if present
        runlog=json.loads(output_session_attributes['runlog'])
        runlog_id=runlog['runlogId']
        intent_request['currentIntent']['slots']["RunlogId"]=runlog['runlogId']
        
    #get slack user from user input or from slot (unlikely this will work)
    if re.search('@\w+', userInput) is not None:
        #check if user has specified @user in utterance
        intent_request['currentIntent']['slots']["SlackUser"]=re.search('@\w+', userInput).group(0)
    
    runbook_id=intent_request['currentIntent']['slots']['RunbookId'] if 'RunbookId' in intent_request['currentIntent']['slots'] else None
    assignee=intent_request['currentIntent']['slots']["SlackUser"] if 'SlackUser' in intent_request['currentIntent']['slots'] else None
    
    print('respond intentName={}, slots={}'.format(str(intent_request['currentIntent']['name']), str(get_slots(intent_request))))

    if source == 'DialogCodeHook':
        slots = get_slots(intent_request)

        validation_result = validate(runlog_id, assignee)
        print('respond validation result={}, invalid slot={}'.format(str(validation_result['isValid']),str(validation_result['violatedSlot'])))
        
        if not validation_result['isValid']:
            slots[validation_result['violatedSlot']] = None            
            
            if validation_result['violatedSlot']=='RunlogId':
                print('respond elicit-slot session={}, invalid slot={}'.format(str(output_session_attributes),str(validation_result['violatedSlot'])))
                return elicit_slot_with_text_message(output_session_attributes, intent, slots, validation_result['violatedSlot'], 'Invalid Runbook, please provide a valid Runbook ID')                
            else:
                print('respond elicit-slot session={}, invalid slot={}'.format(str(output_session_attributes),str(validation_result['violatedSlot'])))
                return elicit_slot_with_text_message(output_session_attributes, intent, slots, validation_result['violatedSlot'], 'Invalid slack user, mention slack users with \'@\' prefix')                
        else:
            runbook_id=getRunbookIdForRunlog(runlog_id, current_user)
            runbook=getRunbookById(runbook_id)
            runlog_entry = Runlog.assign(runbook_id, runbook['name'], current_user, assignee)
            save(runlog_entry)
            print('respond save runlog={}'.format(str(runlog_entry)))
            output_session_attributes['runlog']=json.dumps(runlog_entry.__dict__)

    print('respond intentName={}, slots={}, output_session_attributes={}'.format(str(intent_request['currentIntent']['name']), str(slots), str(output_session_attributes)))
    return delegate(output_session_attributes, get_slots(intent_request))
Example #5
0
def respond(intent_request):
    """
    Performs dialog management for searching and selecting a runbook.
    """
    product = get_slots(intent_request)["Product"]
    runbook_type = get_slots(intent_request)["RunbookType"]
    action = get_slots(intent_request)["ActionType"]
    runbook_id = get_slots(intent_request)["RunbookId"]
    
    print('respond intentName={}, slots={}'.format(str(intent_request['currentIntent']['name']), str(get_slots(intent_request))))

    source = intent_request['invocationSource']    
    intent = intent_request['currentIntent']['name']
    output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
	
    if source == 'DialogCodeHook':
        slots = get_slots(intent_request)

        validation_result = validate(product, runbook_type, action, runbook_id)
        print('respond validation result={}, invalid slot={}'.format(str(validation_result['isValid']),str(validation_result['violatedSlot'])))
        
        if not validation_result['isValid']:
            slots[validation_result['violatedSlot']] = None
            
            # elicit slot for product            
            if validation_result['violatedSlot']=='Product':
                buttons = [ {"text":f['key'].upper()+' ('+str(f['doc_count'])+')',"value":f['key']} for f in getAllRunbooks()['facets']['product']['buckets'] ]
                response_card = build_response_card('Filter by Product/Platform','Select from the following products/platforms', buttons)
                
                print('respond elicit-slot response_card={}, invalid slot={}'.format(str(response_card),str(validation_result['violatedSlot'])))
                return elicit_slot_with_response_card(output_session_attributes,
                               intent,
                               slots,
                               validation_result['violatedSlot'],
                               'Please select from below products',
                               response_card)
                
    
            if validation_result['violatedSlot']=='RunbookId':                
                result = searchByName(None, product, runbook_type, action) #search for runbooks with current slots                
                attachments=[]
                for rbk in result['runbooks']:                    
                    buttons = [ {"text":"Select "+rbk['id'], "value":rbk['id']} ]
                    response_card = { 'title': (rbk['id']+': '+rbk['name']), 'subTitle' : rbk['description'][:75], 'options': buttons }
                    attachments.insert(0, response_card)
                
                response_cards=build_multiple_response_cards(attachments)
                
                print('respond elicit-slot response_cards={}, invalid slot={}'.format(str(response_cards),str(validation_result['violatedSlot'])))
                return elicit_slot_with_response_card(output_session_attributes,
                               intent,
                               slots,
                               validation_result['violatedSlot'],
                               'Please select from below list',
                               response_cards)


        if runbook_id:
            output_session_attributes['selected_runbook'] = json.dumps(getRunbookById(runbook_id)) # serialize runbook obj
            output_session_attributes['previous_intent'] = intent_request['currentIntent']['name']
                        
        print('respond elicit_intent session={}'.format(str(output_session_attributes)))
        # select runbook, and rely on the goodbye message of the bot to define the message to the end user.
        buttons = [ 
                    {"text":"View Details","value": "get "+runbook_id}, 
                    {"text":"Assign to","value": "assign "+runbook_id}, 
                    {"text":"Execute","value": "to do "+runbook_id}
        ]
        response_card = build_response_card('Runbook '+runbook_id, getRunbookById(runbook_id)['name'], buttons)
        print('response elicit_intent_with_response_card = {}'.format(str(response_card)))
    
        return elicit_intent_with_response_card(output_session_attributes, 'Now that you have selected a runbook, what do you want to do next?', response_card)