2016-09-09 14 views
1

私はGitHubのwit.aiのmessenger.jsの変更にいくつか問題があります。リアルタイムの天気を得るためにいくつかのAPI呼び出しを追加しましたが、結果をボットの返信(context.forecast)に渡すことができません。 context.forecast行は、私の気象データを「見る」ことができません。Wit.aiはチャットにデータを解析していません応答

ほとんどの該当する行は次のようになります。

context.forecast = 'Todays forecast is: ' + hourlyWeather +' in ' + location; 

hourlyWeatherのようなデータを持っている「この夕方まで小雨。」ボットが返信して返信します。

ここに関連するコードがありますが、私は誤った場所/順序で何かを持っていて、私がノードに慣れていないと思っています。私は助けに感謝します。

// Our bot actions 
const actions = { 
send({sessionId}, {text}, request) { 
//const {context, entities} = request; 
// Our bot has something to say! 
// Let's retrieve the Facebook user whose session belongs to 
const recipientId = sessions[sessionId].fbid; 

if (recipientId) { 
    // Yay, we found our recipient! 
    // Let's forward our bot response to her. 
    // We return a promise to let our bot know when we're done sending 
    return fbMessage(recipientId, text) 
    .then(() => null) 
    .catch((err) => { 
    console.error(
     'Oops! An error occurred while forwarding the response to', 
     recipientId, 
     ':', 
     err.stack || err 
    ); 
    }); 
} else { 
    console.error('Oops! Couldn\'t find user for session:', sessionId); 
    // Giving the wheel back to our bot 
    return Promise.resolve() 
} 
}, 



// You should implement your custom actions here 
    // See https://wit.ai/docs/quickstart 



//Problems start here. ---------------- 

    getForecast({context, entities}) { 
    return new Promise(function(resolve, reject) { 
     var location = firstEntityValue(entities, 'location') 
     if (location) { 
     // we should call a weather API here 



     //API call to convert city name to longitude latitude 
     const requestw = require('request'),url = 'http://nominatim.openstreetmap.org/search?q='+location+'&format=json' 

     request(url, (error, response, body)=> { 
      if (!error && response.statusCode === 200) { 
      const fbResponse = JSON.parse(body) 
      //console.log("Got a response: ", fbResponse) 
      //convert JSON to array 
      var arr = []; 
      for(var x in fbResponse){ 
      arr.push(fbResponse[x]); 
     } 
      //find latitude and longitude in array and store for later weather api call 
      if(arr[0].hasOwnProperty('lat')){ 
      var lat = arr[0].lat; 
     } 
      if(arr[0].hasOwnProperty('lon')){ 
      var lon = arr[0].lon; 
     } 
      console.log(lat, lon) 

      //API call for weather status 
      forecast.get([lat, lon], function(err, weather) { 
      if(err) return console.dir(err); 
      //console.dir(weather); 
//Store weekly and daily report - Cant get to show up in forecast string. -------------- 
       console.log(weather.hourly.summary) 
      var hourlyWeather = weather.hourly.summary; 
       console.log(weather.daily.summary) 
      var dailyWeather = weather.daily.summary; 

      }); 
      } else { 
      console.log("Got an error while grabbing coordinates of city: ", error, ", status code: ", response.statusCode) 
      } 
     }) 

//Cant get hourlyWeather or dailyWeather to show here. Location works fine. Have tried declaring the variables elsewhere. ----------- 
     //context.forecast = hourlyWeather; 
     context.forecast = 'Todays forecast is: ' + hourlyWeather +' in ' + location; 



     delete context.missingLocation; 
     } else { 
     context.missingLocation = true; 
     delete context.forecast; 
     } 
     return resolve(context); 
    }); 
    }, 
}; 

ボットの返信には、「今日の予定はアトランタでのみ」と表示されます。

答えて

0

コンテキストに対してhourlyWeatherを格納する必要があります。

var hourlyWeather = weather.hourly.summary; 
context.hw = hourlyWeather; 

次に、コンテキストを介して天気にアクセスします。

context.forecast = 'Todays forecast is: ' + context.hw +' in ' + location;