我被困在 API 中"world weather online" Webhook 代码实现上。他们的文档页面上给出的人工智能



我逐步遵循其网站的api.ai文档的起始指南,现在我被困在"满足"部分。

一切都在起作用,但是当我尝试从Google,Weather-App上的操作中检索请求信息时,它并没有给我所部署的满足回复。它仍然回答我在天气应用代理中进行了硬编码的默认响应。

'use strict';
const http = require('http');
const host = 'api.worldweatheronline.com';
const wwoApiKey = '***i used key here from their website***';
exports.weatherWebhook = (req, res) => {
  // Get the city and date from the request
  let city = req.body.result.parameters['geo-city']; // city is a required param
  // Get the date for the weather forecast (if present)
  let date = '';
  if (req.body.result.parameters['date']) {
    date = req.body.result.parameters['date'];
    console.log('Date: ' + date);
  }
  // Call the weather API
  callWeatherApi(city, date).then((output) => {
    // Return the results of the weather API to API.AI
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
  }).catch((error) => {
    // If there is an error let the user know
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
  });
};
function callWeatherApi(city, date) {
  return new Promise((resolve, reject) => {
    // Create the path for the HTTP request to get the weather
    let path = '/premium/v1/weather.ashx?format=json&num_of_days=1' +
      '&q=' + encodeURIComponent(city) + '&key=' + wwoApiKey + '&date=' + date;
    console.log('API Request: ' + host + path);
    // Make the HTTP request to get the weather
    http.get({ host: host, path: path }, (res) => {
        let body = ''; // var to store the response chunks
        res.on('data', (d) => { body += d; }); // store each response chunk
        res.on('end', () => {
            // After all the data has been received parse the JSON for desired data
            let response = JSON.parse(body);
            let forecast = response['data']['weather'][0];
            let location = response['data']['request'][0];
            let conditions = response['data']['current_condition'][0];
            let currentConditions = conditions['weatherDesc'][0]['value'];
            // Create response
            let output = `Current conditions in the ${location['type']}
              ${location['query']} are ${currentConditions} with a projected high of
              ${forecast['maxtempC']}°C or ${forecast['maxtempF']}°F and a low of
              ${forecast['mintempC']}°C or ${forecast['mintempF']}°F on
              ${forecast['date']}.`;
            // Resolve the promise with the output text
            console.log(output);
            resolve(output);
        });
        res.on('error', (error) => {
            reject(error);
        });
    });
  });
}

我遇到了与2018年8月相同的问题,而回答似乎总是回到输入" api.worldweatheronline.com"。

我能够通过在下面的地址打字来使我的工作。确保您也正确输入两个密钥。

 api.worldweatheronline.com/premium/v1/weather.ashx?key=yourwwokey

这似乎可以解决问题,尽管我不知道为什么地址的语法从一个地址变为另一个。

最新更新