有没有一种巧妙的方法来组织 Node.js Alexa 代码。随着我在交互模型中的意图增加,lambda 的 index.js 中的代码行数增加到无法管理的程度。是否有一个如何构建 Alexa Node.js 代码的示例。
问问题
126 次
1 回答
0
我通常将所有状态处理程序放入它们自己的文件中,然后将它们导出。我还将常用的数据/配置放入“资源文件”并导出。
资源.js
const ACCESS_TOKEN = 'sometoken';
const GAME_STATES = {
TRIVIA: "_TRIVIAMODE", // Asking trivia questions.
START: "_STARTMODE", // Entry point, start the game.
HELP: "_HELPMODE", // The user is asking for help.
};
const APP_ID = 'appid';
const HOST = 'http://localhost:8000';
const API_ROUTES = {
QUIZZES: HOST + '/api/quizzes/', //all quizzes
CATEGORIES: HOST + '/api/categories/' //all categories
};
const API_OPTIONS = {
url: '',
headers: {
'Accept': 'application/json',
"Authorization" : "Bearer " + ACCESS_TOKEN
}
};
module.exports = {
ACCESS_TOKEN: ACCESS_TOKEN,
GAME_STATES: GAME_STATES,
APP_ID: APP_ID,
API_OPTIONS: API_OPTIONS,
API_ROUTES: API_ROUTES
};
我的 index.js 只需要这些文件,并且是应用程序的简单入口点。
index.js
'use strict';
const Alexa = require('alexa-sdk');
const helpers = require('./helpers');
const resources = require('./resources');
const newsessionhandlers = require('./newsessionhandlers');
const helpstatehandlers = require('./helpstatehandlers');
const triviastatehandlers = require('./triviastatehandlers');
const startstatehandlers = require('./startstatehandlers');
const APP_ID = resources.APP_ID;
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.resources = resources.languageString;
alexa.registerHandlers(newsessionhandlers.newSessionHandlers, startstatehandlers.startStateHandlers, triviastatehandlers.triviaStateHandlers, helpstatehandlers.helpStateHandlers);
alexa.execute();
};
于 2018-02-23T10:46:42.003 回答