0

I'm trying to call a weather API inside Dialogflow Inline Editor but I'm getting an error I assume is related to the asynchronous call: "No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?"

I'm trying to fix it with a return and a callback function.

    const functions = require('firebase-functions');
const {dialogflow} = require('actions-on-google');

const http = require('http');
const { WebhookClient } = require('dialogflow-fulfillment');
const { Card, Suggestion } = require('dialogflow-fulfillment');

const WELCOME_INTENT = 'Default Welcome Intent';
const FALLBACK_INTENT = 'Default Fallback Intent';
const WEATHER_INTENT = 'Weather guess';

const app = dialogflow();

function callWeatherAPI() {

  return new Promise((resolve, reject) => {
    let request = require('request');

    let apiKey = '**********';
    let city = 'toronto';
    let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=${apiKey}`;

    request(url, (err, response, body) => {
      if(err){
        return reject(err);
      }
      else{
        let weather = JSON.parse(body);
        return resolve(weather.weather[0].main);
      }
    })
  });
}

function weatherGuess(agent) {
    let input = agent.parameters['WeatherCondition'];
    callWeatherAPI()
    .then( (weather) => {
      agent.ask("The weather today is " + weather);
    })
    .catch( (error) => {
      agent.ask("I ran into an error getting the weather!");
    });
}

app.intent(WELCOME_INTENT, (agent) => {
    agent.ask("Hi, hit me with your best weather guess!");
});

app.intent(FALLBACK_INTENT, (agent) => {
    agent.ask("I didn't get that, sorry.");
});

app.intent(WEATHER_INTENT, (agent) => {
    weatherGuess(agent);
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Luis
  • 44
  • 1
  • 5
  • 17

0 Answers0