我試圖執行API.AI教程爲構建天氣對機器人谷歌助理(這裏的一個:https://dialogflow.com/docs/getting-started/basic-fulfillment-conversation)的getaddrinfo ENOTFOUND API谷歌雲
我成功了一切,創建API中的機器人,創建配置,在我的電腦上安裝了NodeJS,連接了Google Cloud Platform等。
然後我創建了index.js文件,它完全按照API.ai教程中的說明使用我的世界氣象組織的API密鑰進行了複製(請參見下文)。
但是,當我使用機器人,它不起作用。在Google雲端平臺上,錯誤總是相同的:
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平臺,並沒有幫助。
那個我真的不能調試。誰能幫忙?
下面的代碼從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);
});
});
});
}