简体   繁体   English

当用户在this.emit(“:ask”,speech)之后什么都没输入到Alexa时,我该如何解决?

[英]How can I account for when the user inputs nothing to Alexa after this.emit(“:ask”, speech)?

I'm writing a game for Alexa but the user input is required. 我正在为Alexa编写游戏,但需要用户输入。 If there is no answer, then the game should end with a unique message of the user's score. 如果没有答案,则游戏应以用户分数的唯一消息结束。 Currently, I have the Alexa prompting the user like this. 目前,我有Alexa这样提示用户。

this.emit(":ask", speech);

However, if the user chooses not to answer, the Alexa ends without any sort of message. 但是,如果用户选择不回答,则Alexa结束时不会发出任何消息。 I checked whether I could account for this in the Stop or Cancel handler, but the program doesn't seem to exit that way. 我检查了是否可以在Stop或Cancel处理程序中解决此问题,但该程序似乎未退出该方式。 How can I account for no input and specify a exit message? 我该如何不输入任何内容并指定退出消息? Here is my full code for reference. 这是我的完整代码供参考。

'use strict';
const Alexa = require('alexa-sdk');

//Messages

const WELCOME_MESSAGE = "Welcome to three six nine. You can play three six nine with just me or with friends. Say help for how to play";

const START_GAME_MESSAGE = "OK. I will start!";

const EXIT_SKILL_MESSAGE = "Better luck next time!";

const HELP_MESSAGE = "Three six nine is a game where we take turns counting up from one. If the number is divisible by three, you're going to say quack. If the number has a three, six, or nine anywhere, you're going to say quack";

const speechConsWrong = ["Argh", "Aw man", "Blarg", "Blast", "Boo", "Bummer", "Darn", "D'oh", "Dun dun dun", "Eek", "Honk", "Le sigh",
"Mamma mia", "Oh boy", "Oh dear", "Oof", "Ouch", "Ruh roh", "Shucks", "Uh oh", "Wah wah", "Whoops a daisy", "Yikes"];

const states = {
    START: "_START",
    GAME: "_GAME"
};

//Game Variables
var counter = 1; 
var numPlayers = 1; //By default is one

const handlers = {
    //Game goes straight to play currently 
     "LaunchRequest": function() {
        this.handler.state = states.START;
        this.emitWithState("Start");
     },
    "PlayIntent": function() {
        this.handler.state = states.GAME;
        this.emitWithState("NextNumber");
    },
    "PlayWithFriends": function() { 
         const itemSlot = this.event.request.intent.slots.numFriends.value; 
         numPlayers = itemSlot;
         this.handler.state = states.GAME; 
         this.emitWithState("NextNumber"); 
    },
    "AMAZON.HelpIntent": function() {
        this.response.speak(HELP_MESSAGE).listen(HELP_MESSAGE);
        this.emit(":responseReady");
    },
    "Unhandled": function() {
        this.handler.state = states.START;
        this.emitWithState("Start");
    }
};


//When skill is in START state
const startHandlers = Alexa.CreateStateHandler(states.START,{
    "Start": function() {
        this.response.speak(WELCOME_MESSAGE).listen(HELP_MESSAGE);
        this.emit(":responseReady");
    },
    "PlayIntent": function() {
        this.handler.state = states.GAME;
        this.emitWithState("NextNumber");
    },
     "PlayWithFriends": function() { 
         const itemSlot = this.event.request.intent.slots.Item;
         numPlayers = itemSlot; //set number to slot value
         this.handler.state = states.GAME; 
         this.emitWithState("NextNumber"); 
    },
    "AMAZON.StopIntent": function() {
        this.response.speak(EXIT_SKILL_MESSAGE);
        this.emit(":responseReady");
    },
    "AMAZON.CancelIntent": function() {
        this.response.speak(EXIT_SKILL_MESSAGE);
        this.emit(":responseReady");
    },
    "AMAZON.HelpIntent": function() {
        this.response.speak(HELP_MESSAGE).listen(HELP_MESSAGE);
        this.emit(":responseReady");
    },
    "Unhandled": function() {
        this.emitWithState("Start");
    }
});


const gameHandlers = Alexa.CreateStateHandler(states.GAME,{
    "Game": function() {
        let speech = ""; 
        let turnDivisible = (counter-1) % (numPlayers+1); 
        if (turnDivisible != 0) { 
            this.emit(":ask", speech);
        } else { 
            this.emitWithState("NextNumber");    
        }
    },
    "NextNumber": function() {

        //If the counter is at 1, the game is beginning with Alexa 
        if (counter == 1) {
            this.attributes.response = START_GAME_MESSAGE + " ";
        } else { 
            this.attributes.response = " ";    
        }

        //check if number contains three, six, nine or divisible by nine
        let speech = " ";
        let divisible = counter % 3; 
        let counterString = counter.toString(); 
        if (counterString.indexOf('3') > - 1 || counterString.indexOf('6') > - 1 || counterString.indexOf('9') > - 1 || divisible === 0) {
            speech = this.attributes.response + "quack"; 
        } else { 
            speech = this.attributes.response + counter;
        }

        //update variables 
        counter++;
        this.emit(":ask", speech);
    },
    "AnswerIntent": function() {
        let correct = checkAnswer(this.event.request.intent.slots, counter);
        //Game continues when you get the correct value
        if (correct) {
            counter++;
            this.emitWithState("Game");
        }
        //Game ends when the value is incorrect
        else {
            let speechOutput = endGame();
            this.response.speak(speechOutput);
            this.emit(":responseReady"); 
        }
    },
    "AMAZON.StopIntent": function() {
        this.response.speak(EXIT_SKILL_MESSAGE);
        endGame();
        this.emit(":responseReady");
    },
    "AMAZON.CancelIntent": function() {
        this.response.speak(EXIT_SKILL_MESSAGE);
        endGame();
        this.emit(":responseReady");
    },
    "AMAZON.HelpIntent": function() {
        this.response.speak(HELP_MESSAGE).listen(HELP_MESSAGE);
        this.emit(":responseReady");
    },
    "Unhandled": function() {
        this.emitWithState("Game");
    }
});

function checkAnswer(slots, value)
{
        for (var slot in slots) {  
        if (slots[slot].value !== undefined)
        {
            let slotValue = slots[slot].value.toString().toLowerCase(); 
            let counterValue = value.toString();

            let divisible = value % 3; 
            if (divisible === 0) { 
                if (slotValue == "quack") { 
                    return true;    
                } else { 
                    return false;    
                }
            }

            else if (counterValue.indexOf('3') > - 1 || counterValue.indexOf('6') > - 1 || counterValue.indexOf('9') > - 1) {
                if (slotValue == "quack") {
                    return true; 
                } else { 
                    return false; 
                }
            }

            else if (slotValue == value.toString().toLowerCase())
            {
                return true;
            } 
            else 
            { 
            return false; 
            }
        }
}
    return false;
}


function endGame() { 
    let speechOutput = "";
    let response = getSpeechCon(false);
    response += "your final score is " + counter;
    speechOutput = response + ". " + EXIT_SKILL_MESSAGE;
    counter = 1; 
    numPlayers = 1; 
    return speechOutput; 
}

function getSpeechCon(type) {
    return "<say-as interpret-as='interjection'>" + speechConsWrong[getRandom(0, speechConsWrong.length-1)] + " </say-as><break strength='strong'/>";
}

function getRandom(min, max) {
    return Math.floor(Math.random() * (max-min+1)+min);
}

exports.handler = (event, context) => {
    const alexa = Alexa.handler(event, context);
    //alexa.appId = APP_ID;
    alexa.registerHandlers(handlers, startHandlers, gameHandlers);
    alexa.execute();
};

You should look for the SessionEndedRequest when the session ends, so you can listen for that and respond accordingly. 您应该在会话结束时寻找SessionEndedRequest,这样您就可以侦听并做出相应的响应。 It should be request.type == 'SessionEndedRequest'. 它应该是request.type =='SessionEndedRequest'。 Much like LaunchRequest, it is not actually an intent, and I see you are already handling LaunchRequest, so it should be easy to add. 就像LaunchRequest一样,它实际上并不是意图,我看到您已经在处理LaunchRequest,因此应该很容易添加。

暂无
暂无

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

相关问题 使用 Alexa-SDK 如何让 this.emit() 方法返回 JSON 数组中的多个对象? - Using the Alexa-SDK how can I get this.emit() method to return multiple objects in a JSON array? 异步HTTP请求完成后,AWS Lambda Node.js执行this.emit - AWS Lambda Node.js execute this.emit after async HTTP request completes 当用户提出某些请求时,我如何获得我的 Alexa 技能来发送 Gmail? - How Can I Get My Alexa Skill To Send Gmails When The User Makes Certain Requests? 当用户输入“否”时,如何结束此过程 - How can I make this process end when user inputs “no” 如何发出带有回调的 Flask-SocketIO 请求,在用户重新加入并且他们的 sid 更改后仍然有效? - How can I emit Flask-SocketIO requests with callbacks that still work after a user rejoins and their sid changes? 如果输入为空,我该怎么做,什么都不会发生 - How i can do that if inputs are empty nothing will happen 用户 (1) 删除其帐户后,如何从用户 (2) 选项卡中删除用户 (1) 的书签帖子? - How can I remove bookmarked posts of user (1) from user (2) tab after user (1) deletes his account? 如何使用不同的用户帐户更新解析用户帐户 - How can i update a parse user account with different user account 在Alexa lambda JavaScript代码中,如何获取当前帐户的名称? - In Alexa lambda javascript code, how can I get the name of the current account? 登录谷歌帐户后什么都没有发生 - Nothing is happening after i sign in to a google account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM