getaddrinfo ENOTFOUND API Google Cloud [英] getaddrinfo ENOTFOUND API Google Cloud

查看:230
本文介绍了getaddrinfo ENOTFOUND API Google Cloud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行API.AI教程,以为Google Assistant构建天气机器人(此处为:

I'm trying to execute API.AI tutorial for building a weather bot for Google Assistant (the one here: https://dialogflow.com/docs/getting-started/basic-fulfillment-conversation)

我成功完成了所有工作,在API中创建了漫游器,创建了实现,在我的电脑上安装了NodeJS,并连接了Google Cloud Platform等.

I made everything successfully, created the bot within API, created the Fulfillments, installed NodeJS on my pc, connected Google Cloud Platform, etc.

然后,我通过使用世界天气组织的API密钥(请参见下文)完全按照API.ai教程中的说明进行复制来创建index.js文件.

Then I created the index.js file by copying it exactly how it's stated on API.ai tutorial with my API key from World Weather Organisation (see below).

但是当我使用机器人时,它不起作用.在Google Cloud Platform上,错误始终相同:

But when I use the bot, it doesn't work. On the Google Cloud Platform the error is always the same:

错误:getaddrinfo ENOTFOUND api.worldweatheronline.com api.worldweatheronline.com:80

Error: getaddrinfo ENOTFOUND api.worldweatheronline.com api.worldweatheronline.com:80

    at errnoException (dns.js:28)
    at GetAddrInfoReqWrap.onlookup (dns.js:76)

无论我多久执行一次,我都会遇到相同的错误.所以我实际上没有到达API.我试图查看WWO方面是否有任何更改(URL等),但显然没有.我更新了NodeJS,仍然是同样的问题.我完全刷新了Google Cloud平台,但没有帮助.

No matter how often I do it I get the same error. So I don't actually reach the API. I tried to see if anything changed from WWO side (URL, etc.) but apparently no. I updated NodeJS and still same issue. I refreshed the Google Cloud platform completely and didn't help.

我真的无法调试.有人可以帮忙吗?

That one I really can't debug. Could anyone help?

这是API.ai中的代码:

Here's the code from API.ai:

'use strict';
const http = require('http');
const host = 'api.worldweatheronline.com';
const wwoApiKey = '[YOUR_API_KEY]';
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 Dialogflow
    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);
      });
    });
  });
} 

推荐答案

哦,男孩,原因实际上是有史以来最愚蠢的.我没有在Google Cloud Platform上启用计费"功能,这就是它阻止所有内容的原因(即使我正在使用API​​的免费测试).他们只是想要我的信用卡号.现在可以使用

Oh boy, in fact the reason was most stupid ever. I didn't enable "billing" on Google Cloud Platform and that's why it blocked everything (even though I'm using a free test of the API). They just wanted my credit card number. It works now

这篇关于getaddrinfo ENOTFOUND API Google Cloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆