Amazon dynamodb Alexa SDK V2高-低游戏没有像教程中所说的那样工作

Amazon dynamodb Alexa SDK V2高-低游戏没有像教程中所说的那样工作,amazon-dynamodb,alexa,alexa-skills-kit,alexa-skill,Amazon Dynamodb,Alexa,Alexa Skills Kit,Alexa Skill,我试图遵循alexa sdk v2的高低游戏教程,但即使遵循教程,我也会遇到一个错误 链接到教程: 这是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.

我试图遵循alexa sdk v2的高低游戏教程,但即使遵循教程,我也会遇到一个错误

链接到教程:

这是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();
const i18n = require('i18next');
const sprintf = require('i18next-sprintf-postprocessor');
const languageStrings = {
  'en' : require('./languages/en')
}
const-Alexa=require('ask-sdk');
const ddbAdapter=require('ask-sdk-dynamodb-persistence-adapter');//包含在ask sdk中
//TODO:此评论下面的项目需要您的注意。
const SKILL_NAME=‘高低游戏’;
const ddbTableName='高低游戏';
const FALLBACK\u MESSAGE\u DURING\u GAME=`这个${SKILL\u NAME}技能帮不了你。试着猜一个介于0和100之间的数字;
const FALLBACK\u REPROMPT\u在游戏期间='请猜一个介于0和100之间的数字';
const FALLBACK\u MESSAGE\u OUTSIDE\u GAME=`这个${SKILL\u NAME}技能帮不了你。它会给出一个介于0和100之间的数字,你可以通过说出一个在这个范围内的数字来猜测它。你想玩吗?`;
const FALLBACK\u REPROMPT\u OUTSIDE\u GAME='说是开始游戏,说否退出游戏';
常量启动请求={
canHandle(handlerInput){
//启动请求以及任何新会话,因为游戏未在进行中保存,这会导致
//除了帮助之外,没有人提出合理的想法,欢迎信息提供了一些帮助。
return handlerInput.requestEnvelope.session.new | | handlerInput.requestEnvelope.request.type==='LaunchRequest';
},
异步句柄(handlerInput){
常量AttributeManager=handlerInput.AttributeManager;
const responseBuilder=handlerInput.responseBuilder;
const attributes=await attributemanager.getPersistentAttributes()| |{};
if(Object.keys(attributes).length==0){
attributes.endedSessionCount=0;
attributes.gamesPlayed=0;
attributes.gameState='end';
}
attributesManager.setSessionAttributes(属性);
const requestAttributes=handlerInput.AttributeManager.getRequestAttributes();
const gamesPlayed=attributes.gamesPlayed.toString()
const speechOutput=requestAttributes.t('LAUNCH_MESSAGE',gamesPlayed);
const reprompt=requestAttributes.t('LAUNCH_reprompt');
返回响应生成器
.讲话(语音输出)
.reprompt(reprompt)
.getResponse();
},
};
常数ExitHandler={
canHandle(handlerInput){
const request=handlerInput.requestEnvelope.request;
return request.type==='IntentRequest'
&&(request.intent.name===“AMAZON.CancelIntent”
||request.intent.name==“AMAZON.StopIntent”);
},
句柄(handlerInput){
const requestAttributes=handlerInput.AttributeManager.getRequestAttributes();
返回handlerInput.responseBuilder
.speak(requestAttributes.t('EXIT_MESSAGE'))
.getResponse();
},
};
常量SessionEndRequest={
canHandle(handlerInput){
return handlerInput.requestEnvelope.request.type==='SessionedRequest';
},
句柄(handlerInput){
log(`会话以原因结束:${handlerInput.requestEnvelope.request.reason}`);
返回handlerInput.responseBuilder.getResponse();
},
};
常量HelpIntent={
canHandle(handlerInput){
const request=handlerInput.requestEnvelope.request;
return request.type=='IntentRequest'&&request.intent.name=='AMAZON.HelpIntent';
},
句柄(handlerInput){
const speechOutput='我在想一个介于0和100之间的数字,试着猜一下,我会告诉你'+
“如果它更高或更低。”;
const reprompt='试着说出一个数字';
返回handlerInput.responseBuilder
.讲话(语音输出)
.reprompt(reprompt)
.getResponse();
},
};
常数YESINT={
canHandle(handlerInput){
//只有在不玩游戏时说“是”时才开始新游戏。
让我们现在玩=假;
const request=handlerInput.requestEnvelope.request;
常量AttributeManager=handlerInput.AttributeManager;
const sessionAttributes=attributemanager.getSessionAttributes();
if(sessionAttributes.gameState&&
sessionAttributes.gameState==='STARTED'){
isCurrentlyPlaying=true;
}
return!isCurrentlyPlaying&&request.type=='IntentRequest'&&request.intent.name=='AMAZON.yesinent';
},
句柄(handlerInput){
常量AttributeManager=handlerInput.AttributeManager;
const sessionAttributes=attributemanager.getSessionAttributes();
const requestAttributes=handlerInput.AttributeManager.getRequestAttributes();
sessionAttributes.gameState='STARTED';
sessionAttributes.guessNumber=Math.floor(Math.random()*101);
返回handlerInput.responseBuilder
.speak(requestAttributes.t('YES_MESSAGE'))
.reprompt(requestAttributes.t('HELP\u reprompt'))
.getResponse();
},
};
常数NoIntent={
canHandle(handlerInput){
//在比赛之外,只将“否”视为退出
让我们现在玩=假;
const request=handlerInput.requestEnvelope.request;
常量AttributeManager=handlerInput.AttributeManager;
const sessionAttributes=attributemanager.getSessionAttributes();
if(sessionAttributes.gameState&&
sessionAttributes.gameState==='STARTED'){
isCurrentlyPlaying=true;
}
return!isCurrentlyPlaying&&request.type=='IntentRequest'&&request.intent.name==='AMAZON.NoIntent';
},
异步句柄(handlerInput){
常量AttributeManager=handlerInput.AttributeManager;
const responseBuilder=handlerInput.responseBuilder;
const sessionAttributes=attributemanager.getSessionAttributes();
const requestAttributes=handlerInput.AttributeManager.getRequestAttributes();
sessionAttributes.endedSessionCount+=1;
sessionAttributes.gameState='ENDED';
attributesManager.SetPersistentAttribute(会话属性);
等待AttributeManager.savePersistentAttributes();
返回handlerInput.responseBuilder
.speak(requestAttributes.t('STOP_MESSAGE'))
.getResponse();
},
};
康斯特乌哈
"i18next": "^10.5.0",
"i18next-sprintf-postprocessor": "^0.2.2"