How can i restart a dialog in Alexa Skill

99封情书 提交于 2020-01-03 05:07:06

问题


I have a NewContactIntent where the user enters data like first name and mobile number etc. I want him to be able to restart the dialog at any point. Therefore I have a RestartIntent so when the user says 'Restart' the RestartIntentHandler handles the request and should forward it back to the NewContactIntent. Here is the code:

const NewContactIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'NewContactIntent';
    },
    handle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        const currentIntent = handlerInput.requestEnvelope.request.intent;

        if (request.dialogState !== 'COMPLETED') {
            return handlerInput.responseBuilder
                .addDelegateDirective(currentIntent) 
                .getResponse();
        } else {
            const speechText = 'Kontakt hinzugefügt.';

            return handlerInput.responseBuilder
                .speak(speechText)
                .getResponse();
        }

    }
};

const RestartIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && (handlerInput.requestEnvelope.request.intent.name === 'RestartIntent');
    },
    handle(handlerInput) {
        return NewContactIntentHandler.handle(handlerInput);
    }
};

It doesn't work, when I try it alexa says the error message from the ErrorHandler.

EDIT

This example works and the BarHandler calls the FooHandler, so I don't understand why my case doesn't work:

const FooIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'FooIntent';
    },
    handle(handlerInput) {
        const speechText = 'Ich bin FooIntent!';

        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};

const BarIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'BarIntent';
    },
    handle(handlerInput) {
        return FooIntentHandler.handle(handlerInput);
    }
};

回答1:


Adding a "restart" utterance

If you have only one intent then you could add "restart" or "reset" as utterances and will start the dialog from the beginning.

Issue: But if you have two more intents to be reset-ed, the utterance "reset" will trigger any of it. Even if you are in the middle of IntentA and then say "reset", it might trigger IntentB who also has a "reset" utterance.

Adding a RestartIntent

Adding a RestartIntent with utterances like "reset" or "restart" will definitely trigger RestartIntent and with the help of a sessionAttribute like lastDialogIntent will help you to understand which Dialog to restart.

Issue: The problem with dialog model is that, you cannot elicit a slot of a different intent from current intent. That means when you say "restart", Alexa will trigger RestartIntent and you cannot respond with a Dialog Directive of the intent to be reseted.

Note that you cannot change intents when returning a Dialog directive, so the intent name and set of slots must match the intent sent to your skill.

The tricky part is that, the user has to say something that will trigger the required intent again from RestartIntent, say IntentA.

Ex: 
User: I want to buy a car [triggered BuyCarIntent]
Alexa: Which color do you want? [first slot to be filled]
User: I want yellow color. [First slot filled]
Alexa: Which is your preferred make?
User: restart  [triggred RestartIntent]
Alexa: okay, what color do you want? [make the user respond in such a way that the user 
                                      speech will trigger the required intent again.]
User: I want yellow color.  [triggered BuyCarIntent]

If the user says just "yellow", sometimes the intent might not get triggered depending on the design of your interaction model. Tune your interaction model and add utterances which can help you with this.

Respond in such a way that the user will say something that will re-trigger the intent again. Make use of the sessionAttribute to store and retrieve last used dialog intent Ex: "lastDialogIntent":"BuyCarIntent" to respond with appropriate question matching the intent.

Once it's re-triggered, dialog model will start from the beginning.




回答2:


There are two possible solutions I can think of: 1) add utterances like "restart", "start over" in NewContactIntent and when the user will say restart, the dialog will automatically start from beginning. 2) add another restart intent but instead of writing a separate handler do this in canHandle function of NewContactIntent

const NewContactIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && (handlerInput.requestEnvelope.request.intent.name === 'NewContactIntent'
            ||handlerInput.requestEnvelope.request.intent.name === 'RestartIntent')
    },

Note: If you want that restart dialog to work only after the dialog of NewContactIntent is started then you can do this by setting a session attribute state to 'dialogStarted' in NewContactIntent and then updating the canHandle function of NewContactIntent like following:

const NewContactIntentHandler = {
        canHandle(handlerInput) {
            const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
            return handlerInput.requestEnvelope.request.type === 'IntentRequest'
                && ((handlerInput.requestEnvelope.request.intent.name === 'NewContactIntent')
                ||(handlerInput.requestEnvelope.request.intent.name === 'RestartIntent'
                && sessionAttributes.state === 'dialogStarted'))


来源:https://stackoverflow.com/questions/53175962/how-can-i-restart-a-dialog-in-alexa-skill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!