2016-03-21 11 views
0

コードの移植性についてintentHandlersの実装を別々の.jsファイルに入れたいと思います。コードは同じファイル内にあるときにファイルを処理しました。私が試したコードを分離するために:Alexa intentHandlersを別々の.jsファイルにどのように置くのですか?

  • 別のファイルにして機能を置く(wikipediaIntent.js)
  • を入れて 'VARをwikipediaIntent =必要(' ./ wikipediaIntentを ');'ベースファイル(intentHandlers.js)
  • 'module.exports = handleFirstEventRequest;'セカンダリjsファイル(wikipediaIntent.js)にあります。

しかし、私は予期しない例外TypeError例外を取得します。オブジェクト関数handleNextEventRequest(意図、セッション、応答){...は、方法はありません「handleFirstEventRequest」

私は、以下の関連するコードと思われるもの置きます。 (完全にアマゾンのサンプル・コードに基づいて)ファイルの完全なコードを見つけることができます: 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

それはwikipediaIntent.jsを変更する必要ほとんどの答えを思わ。

私は次のように、最も単純な、しかし安全ではないオプションを使いました。

  • wikipediaIntent.js - 「厳密 を使用する」元に戻し、その後、コメントアウト。
  • intentHandlers.js - 'use strict'をコメントアウトしました。追加されました:

    var fs = require( 'fs');

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

関連する問題