为了代码可移植性,我想将 intentHandlers 的实现放在单独的 .js 文件中。代码在同一文件中时工作文件。要分离我尝试过的代码:
- 将函数放入单独的文件(wikipediaIntent.js)
- 把'var wikipediaIntent = require('./wikipediaIntent');' 在基本文件中(intentHandlers.js)
- 把'module.exports = handleFirstEventRequest;' 在辅助 js 文件(wikipediaIntent.js)中。
但我得到意外的异常 TypeError: Object function handleNextEventRequest(intent, session, response) { ... has no method 'handleFirstEventRequest'
我把我认为的相关代码放在下面。可以找到文件的完整代码(完全基于 Amazons 示例代码):intentHandlers.js - http://pastebin.com/Z5R1p4UP和 wikipediaIntent.js - http://pastebin.com/bYY21g2C
intentHandlers.js:
var wikipediaIntent = require('./wikipediaIntent');
var registerIntentHandlers = function (intentHandlers, skillContext) {
intentHandlers.GetFirstEventIntent = function (intent, session, response) {
handleFirstEventRequest(intent, session, response);
},
... // see http://pastebin.com/Z5R1p4UP for complete file
};
exports.register = registerIntentHandlers;
和维基百科Intent.js
'use strict';
var https = require('https');
var urlPrefix = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&explaintext=&exsectionformat=plain&redirects=&titles=';
var paginationSize = 3;
var delimiterSize = 2;
function handleFirstEventRequest(intent, session, response) {
var daySlot = intent.slots.day;
var repromptText = "With History Buff, you can get historical events for any day of the year. For example, you could say today, or August thirtieth. Now, which day do you want?";
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var sessionAttributes = {};
// Read the first 3 events, then set the count to 3
sessionAttributes.index = paginationSize;
var date = "";
// If the user provides a date, then use that, otherwise use today
// The date is in server time, not in the user's time zone. So "today" for the user may actually be tomorrow
if (daySlot && daySlot.value) {
date = new Date(daySlot.value);
} else {
date = new Date();
}
var prefixContent = "For " + monthNames[date.getMonth()] + " " + date.getDate() + " ";
var cardContent = "For " + monthNames[date.getMonth()] + " " + date.getDate() + " ";
var cardTitle = "Events on " + monthNames[date.getMonth()] + " " + date.getDate();
getJsonEventsFromWikipedia(monthNames[date.getMonth()], date.getDate(), function (events) {
var speechText = "",
i;
sessionAttributes.text = events;
session.attributes = sessionAttributes;
if (events.length == 0) {
speechText = "There is a problem connecting to Wikipedia at this time. Please try again later.";
cardContent = speechText;
response.tell(speechText);
} else {
for (i = 0; i < paginationSize; i++) {
cardContent = cardContent + events[i];
speechText = speechText + events[i];
}
speechText = speechText + "Wanna go deeper in history?";
var speechOutput = prefixContent + speechText;
var repromptOutput = repromptText;
response.askWithCard(speechOutput, cardTitle, cardContent);
}
});
}
... //other support functions see http://pastebin.com/bYY21g2C for complete file
module.exports = handleFirstEventRequest;