def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        curr_req = handler_input.request_envelope.request

        # delegate directive for reorder intent
        reorder_intent = Intent(
            name="ReorderMedicinesIntent",
            slots={},
            confirmation_status=IntentConfirmationStatus.NONE)
        reorder_intent_directive = DelegateDirective(reorder_intent)

        # delegate intent for stop intent
        stop_intent = Intent(name="AMAZON.StopIntent",
                             slots={},
                             confirmation_status=IntentConfirmationStatus.NONE)
        stop_intent_directive = DelegateDirective(stop_intent)

        # delegate directive intent for current intent
        current_intent = Intent(
            name=curr_req.intent.name,
            slots=curr_req.intent.slots,
            confirmation_status=curr_req.intent.confirmation_status)
        confirm_intent_directive = DelegateDirective(current_intent)

        med_data = Utils.get_remaining_stock()
        speech, reorder_meds = data.get_remaining_stock_intent_response(
            med_data)

        # add reorder meds to session attributes to enable access from reorder intent handler
        handler_input.attributes_manager.session_attributes[
            'reorder_meds'] = reorder_meds

        # if user has allowed the intent to reorder medicines, then set dialog_state in the request to completed
        # to allow the dialog model to end
        if curr_req.intent.confirmation_status == IntentConfirmationStatus.CONFIRMED:
            handler_input.request_envelope.request.dialog_state = 'COMPLETED'

        # got to AMAZON.StopIntent when the user denies intent confirmation and stop the dialog model
        if curr_req.intent.confirmation_status == IntentConfirmationStatus.DENIED:
            return (handler_input.response_builder.speak("Okay").add_directive(
                stop_intent_directive).response)

        if handler_input.request_envelope.request.dialog_state != "COMPLETED":
            return (handler_input.response_builder.add_directive(
                confirm_intent_directive).speak(speech).response)
        else:
            return (handler_input.response_builder.speak(speech).add_directive(
                reorder_intent_directive).response)
コード例 #2
0
    def handle(self, handler_input):
        logger.debug("In StartedInProgressCommutePreferencesHandler")
        logger.debug(
            "Dialog state %s", handler_input.request_envelope.request.dialog_state
        )
        logger.debug("Slots %s", handler_input.request_envelope.request.intent.slots)

        return handler_input.response_builder.add_directive(
            DelegateDirective()
        ).response
コード例 #3
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        logger.debug("In StartedInProgressFavoriteLineHandler")
        logger.debug(
            "Dialog state %s", handler_input.request_envelope.request.dialog_state
        )
        logger.debug("Slots %s", handler_input.request_envelope.request.intent.slots)
        return handler_input.response_builder.add_directive(
            DelegateDirective()
        ).response
コード例 #4
0
def how_to_recycle_handler(handler_input: HandlerInput):
    # Check if a product category has already been recorded in session attributes
    slots = handler_input.request_envelope.request.intent.slots

    if item_slot in slots:
        item_slot_val = slots[item_slot].value
    else:
        item_slot_val = None

    speech = ""
    dialogstate = handler_input.request_envelope.request.dialog_state
    intent_request = handler_input.request_envelope.request.intent
    zip_val_valid = True

    if dialogstate.value != "COMPLETED" and (item_slot_val is None):
        handler_input.response_builder.set_should_end_session(False)
        handler_input.response_builder.add_directive(
            DelegateDirective(updated_intent=intent_request))

        return handler_input.response_builder.response

    else:
        print(str.format("Getting data with values item = {0}", item_slot_val))
        speech = get_recycling_info(item_slot_val)

        if speech == "":
            speech = "Sorry, I could not find the information you are looking for. Please try again"
            handler_input.response_builder.set_should_end_session(True)
        else:
            handler_input.response_builder.set_should_end_session(False)
            speech = speech + ' . You can ask for more information by saying how to recycle or say Stop to quit'

        info_document = load_apl_document(
            "apl_how_to_recycle.json")['document']
        info_datasources = load_apl_document(
            "apl_how_to_recycle.json")['dataSources']
        if is_apl_supported(handler_input):
            handler_input.response_builder.speak(speech).add_directive(
                RenderDocumentDirective(token="pagerToken",
                                        document=info_document,
                                        datasources=info_datasources))
        else:
            handler_input.response_builder.speak(speech)

    return handler_input.response_builder.response
コード例 #5
0
ファイル: location_finder.py プロジェクト: amitdan/3-R-bot
def find_location_handler(handler_input: HandlerInput):

    # Check if a product category has already been recorded in session attributes
    slots = handler_input.request_envelope.request.intent.slots

    if item_slot in slots:
        item_slot_val = slots[item_slot].value
    if zip_slot in slots:
        zip_slot_val = slots[zip_slot].value

    speech = ""
    dialogstate = handler_input.request_envelope.request.dialog_state
    intent_request = handler_input.request_envelope.request.intent
    zip_val_valid = True

    if dialogstate.value != "COMPLETED" and (item_slot_val is None
                                             or zip_slot_val is None):
        handler_input.response_builder.set_should_end_session(False)
        handler_input.response_builder.add_directive(
            DelegateDirective(updated_intent=intent_request))
        return handler_input.response_builder.response

    else:
        print(
            str.format("Getting data with values item ={0} and zip = {1}",
                       item_slot_val, zip_slot_val))

        result_list = get_location(item_slot_val, zip_slot_val)
        print("Got " + str(len(result_list)) + "results, Preparing results")

        if len(result_list) > 0:
            for k, v in result_list[0].items():
                print(str.format("{0} =>  {1}", k, v))
                speech = speech + str.format(" {0} is  {1}, ", k, v)

            handler_input.response_builder.set_should_end_session(True).speak(
                speech).set_card(SimpleCard(title=skill_name, content=speech))

        else:
            handler_input.response_builder.set_should_end_session(False).speak(
                sorry_text).set_card(
                    SimpleCard(title=skill_name, content=speech))

    return handler_input.response_builder.response
コード例 #6
0
def find_location_handler(handler_input: HandlerInput):

    # Check if a product category has already been recorded in session attributes
    slots = handler_input.request_envelope.request.intent.slots

    if item_slot in slots:
        item_slot_val = slots[item_slot].value
    if zip_slot in slots:
        zip_slot_val = slots[zip_slot].value

    speech = ""
    dialogstate = handler_input.request_envelope.request.dialog_state
    intent_request = handler_input.request_envelope.request.intent
    zip_val_valid = True

    if dialogstate.value != "COMPLETED" and (item_slot_val is None
                                             or zip_slot_val is None):
        handler_input.response_builder.set_should_end_session(False)
        handler_input.response_builder.add_directive(
            DelegateDirective(updated_intent=intent_request))

        return handler_input.response_builder.response

    else:
        print(
            str.format("Getting data with values item ={0} and zip = {1}",
                       item_slot_val, zip_slot_val))

        result_list = get_location(item_slot_val, zip_slot_val)
        print("Got " + str(len(result_list)) + "results, Preparing results")
        location_datasources = load_apl_document(
            'apl_location_recycling_center.json')['dataSources']
        location_document = load_apl_document(
            'apl_location_recycling_center.json')['document']

        if len(result_list) > 0:
            # try to get the map of location using Map API

            try:

                value = urlencode({
                    'center':
                    result_list[0]['Nearby center name'] + ", " +
                    result_list[0]['Address'] + ', USA'
                })

                print(map_query + '&' + value)
                location_datasources['bodyTemplate2Data']['image']['sources'][
                    0]['url'] = map_query + '&' + value
                location_datasources['bodyTemplate2Data']['image']['sources'][
                    1]['url'] = map_query + '&' + value
            except:
                location_datasources['bodyTemplate2Data']['image']['sources'][
                    0]['url'] = ""  # No picture for Echo Spot
                location_datasources['bodyTemplate2Data']['image']['sources'][
                    1]['url'] = 'https://s3.amazonaws.com/aws-sumerian-ar/APL/location_pin_hi_res_512.png'
                print('error while loading images of location')
            for k, v in result_list[0].items():
                print(str.format("{0} =>  {1}", k, v))
                speech = speech + str.format(" {0} is  {1}, ", k, v)
                location_datasources['bodyTemplate2Data']['textContent'][
                    'primaryText']['text'] = speech

            if is_apl_supported(handler_input):
                handler_input.response_builder.set_should_end_session(
                    True).speak(speech).add_directive(
                        RenderDocumentDirective(
                            token="pagerToken",
                            document=location_document,
                            datasources=location_datasources))
            else:
                handler_input.response_builder.set_should_end_session(
                    True).speak(speech)

        else:
            location_datasources['bodyTemplate2Data']['textContent'][
                'primaryText']['text'] = sorry_text
            if is_apl_supported(handler_input):
                handler_input.response_builder.set_should_end_session(
                    False).speak(sorry_text).add_directive(
                        RenderDocumentDirective(
                            token="pagerToken",
                            document=location_document,
                            datasources=location_datasources))
            else:
                handler_input.response_builder.set_should_end_session(
                    False).speak(sorry_text)

    return handler_input.response_builder.response