Dialogflow内联编辑器中的异步调用。 “未设置任何回应...”

时间:2019-01-21 14:07:14

标签: node.js firebase google-cloud-functions dialogflow

我正在尝试在Dialogflow内联编辑器中调用天气API,但出现错误,我认为与异步调用有关:“未设置响应。是否在未使用异步调用中使用作为对意图处理程序的承诺返回的?”

我正在尝试使用return和callback函数对其进行修复。

    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);

0 个答案:

没有答案