Exemplo n.º 1
0
def get_number(intent_request):
    """
    Performs dialog management and fulfillment for getting phone number.
    Beyond fulfillment, the implementation of this intent demonstrates the use of the elicitSlot dialog action
    in slot validation and re-prompting.
    """

    login_id = response.get_slots(intent_request)["loginId"]
    source = intent_request['invocationSource']

    if source == 'DialogCodeHook':
        # Perform basic validation on the supplied input slots.
        # Use the elicitSlot dialog action to re-prompt for the first violation detected.
        slots = response.get_slots(intent_request)

        validation_result = validate_idnum(login_id)
        if not validation_result['isValid']:
            slots[validation_result['violatedSlot']] = None
            return response.elicit_slot(
                intent_request['sessionAttributes'],
                intent_request['currentIntent']['name'], slots,
                validation_result['violatedSlot'],
                validation_result['message'])

        # Pass the price of the flowers back through session attributes to be used in various prompts defined
        # on the bot model.
        output_session_attributes = intent_request[
            'sessionAttributes'] if intent_request[
                'sessionAttributes'] is not None else {}
        if login_id is not None:
            global key_id
            val = db_handler(login_id)

            if val > 0:
                key_id = str(login_id)
                stateof = 'logged in'
            else:
                stateof = 'no such account'
                val = 'not logged in'
            output_session_attributes[
                'Number'] = stateof  # Elegant pricing model

        return response.delegate(output_session_attributes,
                                 response.get_slots(intent_request))

    # Order the flowers, and rely on the goodbye message of the bot to define the message to the end user.
    # In a real bot, this would likely involve a call to a backend service.

    return response.close(intent_request['sessionAttributes'], 'Fulfilled', {
        'contentType': 'PlainText',
        'content': 'Welcome to Lex chatbot'
    })
Exemplo n.º 2
0
def get_time(intent_request, key):
    """
    Performs dialog management and fulfillment for getting phone number.
    Beyond fulfillment, the implementation of this intent demonstrates the use of the elicitSlot dialog action
    in slot validation and re-prompting.
    """

    open_time = response.get_slots(intent_request)['openTime']
    close_time = response.get_slots(intent_request)['closeTime']
    source = intent_request['invocationSource']

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

        validation_result = validate_time(open_time, close_time)
        if not validation_result['isValid']:
            slots[validation_result['violatedSlot']] = None
            return response.elicit_slot(
                intent_request['sessionAttributes'],
                intent_request['currentIntent']['name'], slots,
                validation_result['violatedSlot'],
                validation_result['message'])

        # Pass the price of the flowers back through session attributes to be used in various prompts defined
        # on the bot model.
        output_session_attributes = intent_request[
            'sessionAttributes'] if intent_request[
                'sessionAttributes'] is not None else {}
        if open_time is not None and close_time is not None:

            #val=str(app_state)
            db_handlertime(open_time, close_time, key)
            output_session_attributes[
                'time'] = "added to bot open"  # Elegant pricing model

        return response.delegate(output_session_attributes,
                                 response.get_slots(intent_request))

    # Order the flowers, and rely on the goodbye message of the bot to define the message to the end user.
    # In a real bot, this would likely involve a call to a backend service.
    return response.close(intent_request['sessionAttributes'], 'Fulfilled', {
        'contentType': 'PlainText',
        'content': 'Contact Time Updated'
    })
Exemplo n.º 3
0
def specific_parking(intent_request):
    """Fulfillment for giving the user information regarding a specified lot"""

    # Check for any errors with the current slots
    parking_lot = helper.try_ex(
        lambda: intent_request['currentIntent']['slots']['ParkingLot']
        )

    # Use of sessionAttributes to store information that can be used to guide
    # conversation.
    if intent_request['sessionAttributes'] is not None:
        session_attributes = intent_request['sessionAttributes']
    else:
        session_attributes = {}

    # Load slot value history for parking lots
    parking_request = json.dumps({
        'ParkingRequest': 'LotAvailability',
        'ParkingLot': parking_lot
        })

    # Track current parking request.
    session_attributes['currentParkingRequest'] = parking_request

    source = intent_request['invocationSource']

    if source == 'DialogCodeHook':
        # Called on each user input until intent has been fulfilled.

        # Check and validate the slots that have been specified.
        validation_result = helper.validate_parking_lot(
                                intent_request['currentIntent']['slots']
                                )
        if not validation_result['isValid']:
            # If invalid, re-elicit for the slot values.
            slots = intent_request['currentIntent']['slots']
            slots[validation_result['violatedSlot']] = None

            return response.elicit_slot(
                session_attributes,
                intent_request['currentIntent']['name'],
                slots,
                validation_result['violatedSlot'],
                validation_result['message']
                )

        intent_request['currentIntent']['slots']['ParkingLot'] \
            = validation_result['newLotName']

        # Redirect to Amazon Lex to obtain slot values.
        return response.delegate(
            session_attributes,
            intent_request['currentIntent']['slots']
            )

    if source == 'FulfillmentCodeHook':
        lamfunc.logger.debug(
            'request for specific parking={}'.format(parking_request)
            )

        # Clear settings from sessionAttributes
        helper.try_ex(lambda: session_attributes.pop('currentParkingRequest'))

        # Keep track of what was the last parking lot the user requested
        # information for.
        session_attributes['lastParkingRequest'] = parking_request

        # End the intent.
        return response.close(
            session_attributes,
            'Fulfilled', {
                'contentType': 'PlainText',
                'content': helper.build_specific_parking_msg(parking_lot)
                }
            )

    raise Exception('Error fulfilling SpecificParking intent')
Exemplo n.º 4
0
def get_directions(intent_request):
    """Fulfillment for giving the user information regarding a specified lot"""

    # Check for any errors with the current slots
    parking_lot = helper.try_ex(
        lambda: intent_request['currentIntent']['slots']['ParkingLot']
        )

    # Use of sessionAttributes to retrieve information that can be used to
    # guide conversation.
    if intent_request['sessionAttributes'] is not None:
        session_attributes = intent_request['sessionAttributes']
    else:
        session_attributes = {}

    # Check for a previous parking request the user had made.
    last_parking_req = helper.try_ex(
        lambda: session_attributes['lastParkingRequest']
        )
    if last_parking_req:
        last_parking_req = json.loads(last_parking_req)

    # Load slot value history for parking lots
    parking_request = json.dumps({
        'ParkingRequest': 'Directions',
        'ParkingLot': parking_lot
        })

    # Track current parking request.
    session_attributes['currentParkingRequest'] = parking_request

    source = intent_request['invocationSource']

    if source == 'DialogCodeHook':
        # Check and validate the slots that have been specified.
        validation_result = helper.validate_parking_lot(
                                intent_request['currentIntent']['slots']
                                )
        if not validation_result['isValid']:
            # If invalid, re-elicit for the slot values.
            slots = intent_request['currentIntent']['slots']
            slots[validation_result['violatedSlot']] = None

            return response.elicit_slot(
                session_attributes,
                intent_request['currentIntent']['name'],
                slots,
                validation_result['violatedSlot'],
                validation_result['message']
                )

        intent_request['currentIntent']['slots']['ParkingLot'] \
            = validation_result['newLotName']

        if parking_lot is None and last_parking_req:
            # If the slot empty and there is a parking lot already stored
            # from a previous conversation, then use that parking value
            lamfunc.logger.debug(
                'request for lot directions={}'.format(parking_request)
                )

            # Clear settings from sessionsAttributes.
            helper.try_ex(
                lambda: session_attributes.pop('currentParkingRequest')
                )
            helper.try_ex(
                lambda: session_attributes.pop('lastParkingRequest')
                )

            # End the intent.
            return response.close(
                session_attributes,
                'Fulfilled', {
                    'contentType': 'PlainText',
                    'content': helper.build_directions_msg(
                        last_parking_req['ParkingLot']
                        )
                    }
                )

        # Otherwise, redirect to Amazon Lex to obtain slot values.
        return response.delegate(
            session_attributes,
            intent_request['currentIntent']['slots']
            )

    if source == 'FulfillmentCodeHook':
        lamfunc.logger.debug(
            'request for lot directions={}'.format(parking_request)
            )

        # Clear settings from sessionAttributes
        helper.try_ex(lambda: session_attributes.pop('currentParkingRequest'))
        helper.try_ex(lambda: session_attributes.pop('lastParkingRequest'))

        # End the intent.
        return response.close(
            session_attributes,
            'Fulfilled', {
                'contentType': 'PlainText',
                'content': helper.build_directions_msg(parking_lot)
                }
            )

    raise Exception('Error fulfilling SpecificParking intent')