Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var buildSpeechletResponse = (outputText, shouldEndSession) => {
- return {
- outputSpeech: {
- type: "PlainText",
- text: outputText
- },
- shouldEndSession: shouldEndSession
- }
- }
- var generateResponse = (speechletResponse, sessionAttributes) => {
- return {
- version: "1.0",
- sessionAttributes: sessionAttributes,
- response: speechletResponse
- }
- }
- function getRandomInt(max) { 'intialising random number generation'
- return Math.floor(Math.random() * Math.floor(max));
- }
- exports.handler = (event, context, callback) => {
- var attributes = event.session.attributes;
- if ( typeof attributes === 'undefined' ) {
- attributes = {};
- }
- switch (event.request.type) {
- case "LaunchRequest":
- attributes.c0 = (getRandomInt(52));
- attributes.c1 = (getRandomInt(52));
- context.succeed(generateResponse(buildSpeechletResponse(`Your first number is ${attributes.c0}. Higher or Lower?`, false), attributes))
- break;
- case "IntentRequest":
- var result;
- switch (event.request.intent.name) {
- case "higher":
- if (attributes.c1 < attributes.c0) {
- result = `Take a drink! It was ${attributes.c1}. `;
- } else {
- result = `Correct! It was ${attributes.c1}. `;
- }
- attributes.c0 = attributes.c1;
- attributes.c1 = (getRandomInt(52));
- context.succeed(generateResponse(buildSpeechletResponse(result+`Higher or Lower?`, false), attributes))
- break;
- case "lower":
- if (attributes.c1 > attributes.c0) {
- result = `Take a drink! It was ${attributes.c1}. `
- } else {
- result = `Correct! It was ${attributes.c1}. `
- }
- attributes.c0 = attributes.c1;
- attributes.c1 = (getRandomInt(52));
- context.succeed(generateResponse(buildSpeechletResponse(result+`Higher or Lower?`, false), attributes))
- break;
- case "lastnumber":
- context.succeed(generateResponse(buildSpeechletResponse(`The number is ${attributes.c0}. Higher or Lower?`, false), attributes))
- break;
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement