我从github获得的lambda函数允许创建Alexa链接到Twine 2的游戏手册。它运行得很好。我想使用Alexa的新APL语言使其具有多种模式,但我陷入困境。
我已经使用APL代码创建了一个main.json文件,该文件现在位于我的lambda函数的文件夹中。我要求在index.js中将此文件用于:
const main = require('./main.json');
我认为我应该添加以下内容:
return handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
varsion: '1.0',
document: main,
datasources: {}
})
响应特定意图的构建器,但是在这个特定的lambda中,我很难理解在何处以及如何插入它。
这是我的Lambda的一部分:
'use strict';
const main = require('./main.json');
const Alexa = require('alexa-sdk');
const story = 'Time Travel Adventure.html';
const TableName = story.replace('.html','').replace(/\s/g, "-");
var $twine = null;
const linksRegex = /\[\[([^\|\]]*)\|?([^\]]*)\]\]/g;
module.exports.handler = (event, context, callback) => {
console.log(`handler: ${JSON.stringify(event.request)}`);
// read the Twine 2 (Harlowe) story into JSON
var fs = require('fs');
var contents = fs.readFileSync(story, 'utf8');
var m = contents.match(/<tw-storydata [\s\S]*<\/tw-storydata>/g);
var xml = m[0];
// because Twine xml has an attribute with no value
xml = xml.replace('hidden>', 'hidden="true">');
var parseString = require('xml2js').parseString;
parseString(xml, function(err, result) {
$twine = result['tw-storydata']['tw-passagedata'];
});
// prepare alexa-sdk
const alexa = Alexa.handler(event, context);
// APP_ID is your skill id which can be found in the Amazon developer console
// where you create the skill. Optionally set as a Lamba environment variable.
const APP_ID ='amzn1.ask.skill.601c478e-8c48-410c-8907-f7e5146eaab5';
alexa.appId = process.env.APP_ID;
alexa.dynamoDBTableName = TableName;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'LaunchRequest': function() {
console.log(`LaunchRequest`);
if (this.event.session.attributes['room'] !== undefined) {
var room = currentRoom(this.event);
var speechOutput = `Hello, you were playing before and got to the room called ${room['$']['name']}. Would you like to resume? `;
var reprompt = `Say, resume game, or, new game.`;
speechOutput = speechOutput + reprompt;
var cardTitle = `Restart`;
var cardContent = speechOutput;
var imageObj = undefined;
console.log(`LaunchRequest: ${JSON.stringify({
"speak": speechOutput,
"listen": reprompt,
"card" : {
"title": cardTitle,
"content": cardContent,
"imageObj": imageObj
}
})}`);
this.response.speak(speechOutput)
.listen(reprompt)
.cardRenderer(cardTitle, cardContent, imageObj);
this.emit(':responseReady');
} else {
this.emit('WhereAmI');
}
},
// ...
我的APL文件现在应该只返回一个空白屏幕:
{
"type": "Alexa.Presentation.APL.RenderDocument",
"datasources": {},
"document": {
"type": "APL",
"version": "1.0",
"import": [],
"resources": [],
"styles": {},
"layouts": {},
"mainTemplate": {}
}
}
如果一切正常,打开该技能时,我应该有一个空白的空白屏幕。但是我不知道如何修改lambda使其起作用。