Alexa技能:如何在此示例中插入AMAZON.yesintent和AMAZON.nointent?

问题描述 投票:0回答:1

这个Alexa技能提供有关平装书或在线书籍的信息。这只是一个例子,而不是真实的东西。所以,这是一个例子用户:“Alexa,找到一本作者最好的书。” Alexa:“好的!这是书,等等等等。” Alexa:“你想再听一次吗?”用户:“是的!” Alexa:重复相同的信息。我想知道如何实现代码以使Alexa回答是/否问题并重复所说的内容。我对此进行了广泛的研究,但我仍然无法理解。顺便说一下,我是新手。

'booksIntent': function () {
        var speechOutput = '';
        var speechReprompt = '';

        var sourceSlot = resolveCanonical(this.event.request.intent.slots.source);
        console.log('User requested source: ' + sourceSlot);

        var authorSlot = resolveCanonical(this.event.request.intent.slots.author);
        console.log('User requested author: ' + authorSlot);

        var sources = {
            'basic book' : {
                'one author' : "Blah blah one author",

                'two authors' : "Blah blah two authors",

                'multiple authors' : "blah blah multiple authors"
            },

            'basic electronic' : {
                'one author' : "Blah blah...some electronic information"
            },

        }

         var authors = [
            'one author',
            'two authors',
            'multiple authors'
            ];

        var source =sourceSlot.toLowerCase();

        if(sourceSlot && sources[source]){
            var standardSource = '';
            var author = 'one author'; //default author choice

            if(authorSlot && author.indexOf(authorSlot) - 1){
                author = authorSlot;
            }

            var getSource = sources[source][author];

                speechOutput = 'Ok! ' + getSource;
                speechReprompt= 'Would you like to hear this again?'; //I want the user to answer with a yes or no and the program to respond accordingly. 


        }
            else{
                speechOutput = 'Sorry, the information you asked is not supported yet'
                speechReprompt = 'I support: this and that!'
            }
            this.emit(":ask", speechOutput, speechReprompt)

    },
javascript alexa alexa-skills-kit alexa-skill alexa-app
1个回答
1
投票

首先,您应该更新Alexa SDK的版本。

您可以使用州解决是,无意图。

例如,在返回语音输出之前,您可以存储下一步的状态。

使用属性来存储状态和所有内容。

attributes.state ="YES_MODE"

在Yes Intent中,

你应该检查,是否匹配状态然后允许进行操作。

这是代码:

attributes.data = "You can store the data. What you want to repeat"
attributes.state = "YES_MODE"

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

在是意图:

 const YesIntentHandler = {
 canHandle(handlerInput) {
const attributes = handlerInput.attributesManager.getSessionAttributes();
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
  && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.YesIntent'
  && attributes.state === "YES_MODE";
},
handle(handlerInput) {
const attributes = handlerInput.attributesManager.getSessionAttributes();
let data = attributes.data

//write your logic 

return handlerInput.responseBuilder
  .speak(speechText)
  .reprompt(repromptText)
  .getResponse();
},
};
© www.soinside.com 2019 - 2024. All rights reserved.