I am trying to follow the tutorial for the high low game for the alexa sdk v2, but even following the tutorial, I am getting an error.
link to tutorial: https://github.com/alexa/skill-sample-nodejs-highlowgame/blob/master/instructions/cli.md
this is the code in the index.js:
const Alexa = require('ask-sdk');
const ddbAdapter = require('ask-sdk-dynamodb-persistence-adapter'); // included in ask-sdk
// TODO: The items below this comment need your attention.
const SKILL_NAME = 'High Low Game';
const ddbTableName = 'High-Low-Game';
const FALLBACK_MESSAGE_DURING_GAME = `The ${SKILL_NAME} skill can't help you with that. Try guessing a number between 0 and 100. `;
const FALLBACK_REPROMPT_DURING_GAME = 'Please guess a number between 0 and 100.';
const FALLBACK_MESSAGE_OUTSIDE_GAME = `The ${SKILL_NAME} skill can't help you with that. It will come up with a number between 0 and 100 and you try to guess it by saying a number in that range. Would you like to play?`;
const FALLBACK_REPROMPT_OUTSIDE_GAME = 'Say yes to start the game or no to quit.';
const LaunchRequest = {
canHandle(handlerInput) {
// launch requests as well as any new session, as games are not saved in progress, which makes
// no one shots a reasonable idea except for help, and the welcome message provides some help.
return handlerInput.requestEnvelope.session.new || handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
async handle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const responseBuilder = handlerInput.responseBuilder;
const attributes = await attributesManager.getPersistentAttributes() || {};
if (Object.keys(attributes).length === 0) {
attributes.endedSessionCount = 0;
attributes.gamesPlayed = 0;
attributes.gameState = 'ENDED';
}
attributesManager.setSessionAttributes(attributes);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const gamesPlayed = attributes.gamesPlayed.toString()
const speechOutput = requestAttributes.t('LAUNCH_MESSAGE', gamesPlayed);
const reprompt = requestAttributes.t('LAUNCH_REPROMPT');
return responseBuilder
.speak(speechOutput)
.reprompt(reprompt)
.getResponse();
},
};
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('EXIT_MESSAGE'))
.getResponse();
},
};
const SessionEndedRequest = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const HelpIntent = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechOutput = 'I am thinking of a number between zero and one hundred, try to guess it and I will tell you' +
' if it is higher or lower.';
const reprompt = 'Try saying a number.';
return handlerInput.responseBuilder
.speak(speechOutput)
.reprompt(reprompt)
.getResponse();
},
};
const YesIntent = {
canHandle(handlerInput) {
// only start a new game if yes is said when not playing a game.
let isCurrentlyPlaying = false;
const request = handlerInput.requestEnvelope.request;
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
if (sessionAttributes.gameState &&
sessionAttributes.gameState === 'STARTED') {
isCurrentlyPlaying = true;
}
return !isCurrentlyPlaying && request.type === 'IntentRequest' && request.intent.name === 'AMAZON.YesIntent';
},
handle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
sessionAttributes.gameState = 'STARTED';
sessionAttributes.guessNumber = Math.floor(Math.random() * 101);
return handlerInput.responseBuilder
.speak(requestAttributes.t('YES_MESSAGE'))
.reprompt(requestAttributes.t('HELP_REPROMPT'))
.getResponse();
},
};
const NoIntent = {
canHandle(handlerInput) {
// only treat no as an exit when outside a game
let isCurrentlyPlaying = false;
const request = handlerInput.requestEnvelope.request;
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
if (sessionAttributes.gameState &&
sessionAttributes.gameState === 'STARTED') {
isCurrentlyPlaying = true;
}
return !isCurrentlyPlaying && request.type === 'IntentRequest' && request.intent.name === 'AMAZON.NoIntent';
},
async handle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const responseBuilder = handlerInput.responseBuilder;
const sessionAttributes = attributesManager.getSessionAttributes();
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
sessionAttributes.endedSessionCount += 1;
sessionAttributes.gameState = 'ENDED';
attributesManager.setPersistentAttributes(sessionAttributes);
await attributesManager.savePersistentAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('STOP_MESSAGE'))
.getResponse();
},
};
const UnhandledIntent = {
canHandle() {
return true;
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('UNHANDLED_RESPONSE'))
.reprompt(requestAttributes.t('UNHANDLED_RESPONSE'))
.getResponse();
},
};
const NumberGuessIntent = {
canHandle(handlerInput) {
// handle numbers only during a game
let isCurrentlyPlaying = false;
const request = handlerInput.requestEnvelope.request;
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
if (sessionAttributes.gameState &&
sessionAttributes.gameState === 'STARTED') {
isCurrentlyPlaying = true;
}
return isCurrentlyPlaying && request.type === 'IntentRequest' && request.intent.name === 'NumberGuessIntent';
},
async handle(handlerInput) {
const { requestEnvelope, attributesManager, responseBuilder } = handlerInput;
const guessNum = parseInt(requestEnvelope.request.intent.slots.number.value, 10);
const sessionAttributes = attributesManager.getSessionAttributes();
const targetNum = sessionAttributes.guessNumber;
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
if (guessNum > targetNum) {
return handlerInput.responseBuilder
.speak(requestAttributes.t('TOO_HIGH_MESSAGE', guessNum.toString()))
.reprompt(requestAttributes.t('TOO_HIGH_REPROMPT'))
.getResponse();
} else if (guessNum < targetNum) {
return handlerInput.responseBuilder
.speak(requestAttributes.t('TOO_LOW_MESSAGE', guessNum.toString()))
.reprompt(requestAttributes.t('TOO_LOW_REPROMPT'))
.getResponse();
} else if (guessNum === targetNum) {
sessionAttributes.gamesPlayed += 1;
sessionAttributes.gameState = 'ENDED';
attributesManager.setPersistentAttributes(sessionAttributes);
await attributesManager.savePersistentAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('GUESS_CORRECT_MESSAGE', guessNum.toString()))
.reprompt(requestAttributes.t('GUESS_CORRECT_REPROMPT'))
.getResponse();
}
return handlerInput.responseBuilder
.speak(requestAttributes.t('FALLBACK_MESSAGE_DURING_GAME'))
.reprompt(requestAttributes.t('FALLBACK_REPROMPT_DURING_GAME'))
.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
console.log(`Error stack: ${error.stack}`);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('ERROR_MESSAGE'))
.reprompt(requestAttributes.t('ERROR_MESSAGE'))
.getResponse();
},
};
const FallbackHandler = {
canHandle(handlerInput) {
// handle fallback intent, yes and no when playing a game
// for yes and no, will only get here if and not caught by the normal intent handler
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' &&
(request.intent.name === 'AMAZON.FallbackIntent' ||
request.intent.name === 'AMAZON.YesIntent' ||
request.intent.name === 'AMAZON.NoIntent');
},
handle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const sessionAttributes = attributesManager.getSessionAttributes();
if (sessionAttributes.gameState &&
sessionAttributes.gameState === 'STARTED') {
// currently playing
return handlerInput.responseBuilder
.speak(requestAttributes.t('FALLBACK_MESSAGE_DURING_GAME'))
.reprompt(requestAttributes.t('FALLBACK_REPROMPT_DURING_GAME'))
.getResponse();
}
// not playing
return handlerInput.responseBuilder
.speak(requestAttributes.t('FALLBACK_MESSAGE_OUTSIDE_GAME'))
.reprompt(requestAttributes.t('FALLBACK_REPROMPT_OUTSIDE_GAME'))
.getResponse();
},
};
const LocalizationInterceptor = {
process(handlerInput) {
const localizationClient = i18n.use(sprintf).init({
lng: handlerInput.requestEnvelope.request.locale,
resources: languageStrings,
});
localizationClient.localize = function localize() {
const args = arguments;
const values = [];
for (let i = 1; i < args.length; i += 1) {
values.push(args[i]);
}
const value = i18n.t(args[0], {
returnObjects: true,
postProcess: 'sprintf',
sprintf: values,
});
if (Array.isArray(value)) {
return value[Math.floor(Math.random() * value.length)];
}
return value;
};
const attributes = handlerInput.attributesManager.getRequestAttributes();
attributes.t = function translate(...args) {
return localizationClient.localize(...args);
};
},
};
function getPersistenceAdapter(tableName) {
// Determines persistence adapter to be used based on environment
// Note: tableName is only used for DynamoDB Persistence Adapter
if (process.env.S3_PERSISTENCE_BUCKET) {
// in Alexa Hosted Environment
// eslint-disable-next-line global-require
const s3Adapter = require('ask-sdk-s3-persistence-adapter');
return new s3Adapter.S3PersistenceAdapter({
bucketName: process.env.S3_PERSISTENCE_BUCKET,
});
}
// Not in Alexa Hosted Environment
return new ddbAdapter.DynamoDbPersistenceAdapter({
tableName: tableName,
createTable: true,
});
}
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.withPersistenceAdapter(getPersistenceAdapter(ddbTableName))
.addRequestHandlers(
LaunchRequest,
ExitHandler,
SessionEndedRequest,
HelpIntent,
YesIntent,
NoIntent,
NumberGuessIntent,
FallbackHandler,
UnhandledIntent,
)
.addRequestInterceptors(LocalizationInterceptor)
.addErrorHandlers(ErrorHandler)
.lambda();
This is exactly as it says when the repository is downloaded, and the DynamoDB is set up exactly as they said too, but it still gets an error. When I try to test it, all that comes out is "There was a problem with the requested skill's response".
The errors that I get, and there are a few for a tutorial, in the cloudWatch Management Console are:
I am new to Alexa SDK and going back and forth between V1 and V2 is getting really confusing. If anyone knows a good tutorial that is maybe not the above link, it would be most appreciated.
If there is anything I can help with please ask. Thank you
This sample is broken as it fails to add a require for i18n in index.js, it's not defining i18next as a dependency in package.json and it's not doing a require of languageStrings. Add this to the beginning of index.js:
const i18n = require('i18next');
const sprintf = require('i18next-sprintf-postprocessor');
const languageStrings = {
'en' : require('./languages/en')
}
and this to the dependencies in your package.json:
"i18next": "^10.5.0",
"i18next-sprintf-postprocessor": "^0.2.2"
then deploy the back-end again.