Alexa-App在单独的文件中创建意图并在index.js中导入和使用它们

时间:2018-04-11 19:20:43

标签: node.js alexa alexa-app

我正在努力使我的文件管理更容易,并将每个意图都放在自己的文件中。我如何包含它,以便我的index.js使用该意图。继承人和我尝试过的例子。

var alexa = require('alexa-app');
var app = new alexa.app();
var GetLunchSuggestions = require('./Intents/GetLunchSuggestions');
app.launch(function(request, response) {
response.say('Welcome I am built to handle your lunch requests');
response.shouldEndSession(false);
});

app.use(GetLunchSuggestions);

// Connect to lambda
exports.handler = app.lambda();
if (process.argv.length === 3 && process.argv[2] === 'schema') {
console.log(app.schema());
console.log(app.utterances());
}

我想在此文件中使用午餐建议意图。你是怎么做到的?

1 个答案:

答案 0 :(得分:0)

./Intents/GetLunchSuggestions.js

module.exports = {
  'AMAZON.HelpIntent': function () {
      const speechOutput = 'I'm a handler from different file.';

      this.response.speak(speechOutput).shouldEndSession(isLaunched);
      this.emit(':responseReady');
   }
}

然后在index.js

const GetLaunchSuggestions = require('./Intents/GetLunchSuggestions');

exports.handler = function (event, context, callback) {
  const alexa = Alexa.handler(event, context, callback);
  alexa.appId = APP_ID; // APP_ID is your skill id which can be found in the Amazon developer console where you create the skill.
  alexa.registerHandlers(
    handlers, // this where some of your handlers being defined. You can remove this if it was not define it your code.
    GetLaunchSuggestions // this is your handler from different file.
  );
  alexa.execute();
};