0

コードの移植性のために、intentHandlers の実装を別の .js ファイルに入れたいと考えています。同じファイル内にある場合、コードはファイルを機能させました。私が試したコードを分離するには:

  • 関数を別のファイルに入れる (wikipediaIntent.js)
  • put 'var wikipediaIntent = require('./wikipediaIntent');' ベースファイル (intentHandlers.js) 内
  • 「module.exports = handleFirstEventRequest;」を置く セカンダリ js ファイル (wikipediaIntent.js) で。

しかし、私は Unexpected exception TypeError: Object function handleNextEventRequest(intent, session, response) { ... has no method 'handleFirstEventRequest' を取得します

関連するコードだと思うものを以下に示します。ファイルの完全なコード (完全に Amazon のサンプル コードに基づく) は次のとおりです

インテントハンドラー.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;
4

1 に答える 1