你如何把Alexa intentHandlers放在单独的.js文件中?

时间:2016-03-21 10:29:35

标签: node.js alexa-skills-kit

对于代码可移植性,我想将intentHandlers的实现放在单独的.js文件中。代码在同一文件中处理文件。要分开我尝试过的代码:

  • 将函数放入单独的文件(wikipediaIntent.js)
  • 把'var wikipediaIntent = require('./ wikipediaIntent');'在basefile(intentHandlers.js)
  • put'module.exports = handleFirstEventRequest;'在辅助js文件(wikipediaIntent.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;

1 个答案:

答案 0 :(得分:0)

似乎大多数答案都需要更改wikipediaIntent.js。

我选择了最简单但不安全的选项,如下所示。

  • wikipediaIntent.js - 恢复原状,然后注释掉“使用” 严格';
  • intentHandlers.js - 注释掉'use strict';并补充说:

    var fs = require('fs');

    的eval(fs.readFileSync( './ wikipediaIntent.js ')+'');