0

I am following the Wit AI tutorial and I got to a point where I am stuck. I am trying to extend the quick start weather tutorial to call an actual weather API and I am having no luck.

Here is my modified getForecast method. The original can be found here: https://wit.ai/docs/quickstart

 var weather = require('weather-js');

    const actions = {
  send(request, response) {
    const {sessionId, context, entities} = request;
    const {text, quickreplies} = response;
    console.log('sending...', JSON.stringify(response));
  },
  getForecast({context, entities}) {
    var location = firstEntityValue(entities, 'location');
    weather.find({search: location, degreeType: 'F'}, function(err, result) {
        if(err){
            context.missingLocation = true;
            delete context.forecast;
        }else{
            var temperature = result[0].current.temperature;
            context.forecast = temperature+ ' degrees in '+location; // we should call a weather API here
            delete context.missingLocation;
            fs.writeFile("weather.json",JSON.stringify(result));
        }
        return context;
    });
  },
};
nab0310
  • 1
  • 1

1 Answers1

0

So you want to do a synchronous call to weather api but node doesn't encourage it. The way to get a synchronous call is to use Promises.

Ref - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

Also below is the link to wit's own implementation of weather bot, you can see how promises are used

https://github.com/wit-ai/witty-weather/blob/master/src/createEngine.js

Swapnesh Khare
  • 109
  • 1
  • 10