简体   繁体   English

我如何在Alexa Skill中重新启动对话框

[英]How can i restart a dialog in Alexa Skill

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. 我有一个NewContactIntent,用户可以在其中输入诸如名字和手机号码之类的数据。我希望他能够在任何时候重新启动对话框。 Therefore I have a RestartIntent so when the user says 'Restart' the RestartIntentHandler handles the request and should forward it back to the NewContactIntent. 因此,我有一个RestartIntent,因此当用户说“ Restart”时,RestartIntentHandler处理该请求,并将其转发回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. 当我尝试它时,它不起作用alexa说来自ErrorHandler的错误消息。

EDIT 编辑

This example works and the BarHandler calls the FooHandler, so I don't understand why my case doesn't work: 此示例有效,BarHandler调用FooHandler,所以我不明白为什么我的案例不起作用:

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);
    }
};

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. 即使您在IntentA的中间然后说“重置” ,它也可能触发同样具有“重置”话语的IntentB

Adding a RestartIntent 添加一个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. 添加诸如“ reset”“ restart”之类的话语的RestartIntent肯定会触发RestartIntent并借助诸如lastDialogIntent sessionAttribute类的lastDialogIntent可以帮助您了解要重启哪个对话框。

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. 这意味着当您说“ restart”时 ,Alexa将触发RestartIntent并且您无法使用要重置的意图的Dialog Directive进行响应。

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. 请注意,返回Dialog指令时无法更改意图,因此意图名称和插槽组必须与发送给您的技能的意图相匹配。

The tricky part is that, the user has to say something that will trigger the required intent again from RestartIntent, say IntentA . 棘手的部分是, 用户必须说一些会从RestartIntent再次触发所需意图的内容 ,例如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. 利用sessionAttribute来存储和检索上次使用的对话框意图,例如: "lastDialogIntent":"BuyCarIntent"以响应与意图匹配的适当问题。

Once it's re-triggered, dialog model will start from the beginning. 重新触发后,对话框模型将从头开始。

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. 我可以想到两种可能的解决方案: 1)NewContactIntent中添加“ restart ”,“ start over ”之类的语音 ,当用户说重启时,对话框将自动从头开始。 2) add another restart intent but instead of writing a separate handler do this in canHandle function of NewContactIntent 2)添加另一个重新启动意图,但不必编写单独的处理程序, 而是NewContactIntent的canHandle函数中执行此操作

 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: 注意 :如果您希望重新启动对话框仅 NewContactIntent对话框启动才能工作,则可以通过在NewContactIntent中将会话属性状态设置为' dialogStarted ',然后按如下所示更新 NewContactIntent的canHandle函数来执行此操作

 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')) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM