对于代码可移植性,我想将intentHandlers的实现放在单独的.js文件中。代码在同一文件中处理文件。要分开我尝试过的代码:
但是我得到了意外异常TypeError:对象函数handleNextEventRequest(意图,会话,响应){...没有方法'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;
和wikipediaIntent.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;
答案 0 :(得分:0)
似乎大多数答案都需要更改wikipediaIntent.js。
我选择了最简单但不安全的选项,如下所示。
intentHandlers.js - 注释掉'use strict';并补充说:
var fs = require('fs');
的eval(fs.readFileSync( './ wikipediaIntent.js ')+'');