首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单独的文件中创建意图,并在index.js中导入和使用它们

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

Stack Overflow用户
提问于 2018-04-12 03:20:43
回答 1查看 310关注 0票数 1

我正在尝试让我的文件管理变得更容易一些,并将每个意图放在它自己的文件中。我如何将其包含进来,以便我的index.js使用该意图。下面是我尝试过的例子。

代码语言:javascript
复制
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());
}

我想在这个文件中使用午餐建议。您如何做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2018-04-20 12:17:42

在你的./Intents/GetLunchSuggestions.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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();
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49782969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档