我正在尝试执行API.AI教程,为Google智能助理构建一个天气机器人(这里的那个:https://dialogflow.com/docs/getting-started/basic-fulfillment-conversation)
我成功完成了一切,在API中创建了机器人,创建了Fulfillments,在我的电脑上安装了NodeJS,连接了Google Cloud Platform等。
然后我通过使用来自世界天气组织的API密钥(参见下文)复制它在API.ai教程中的说明来创建index.js文件。
但是当我使用机器人时,它不起作用。在Google Cloud Platform上,错误始终相同:
错误: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云平台并没有帮助。
那个我真的无法调试。有人可以帮忙吗?
以下是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);
});
});
});
}
答案 0 :(得分:5)
答案 1 :(得分:0)
我尝试打数据库时遇到了同样的问题。计费不是解决方法,因为我已经启用了计费功能。
对我来说,这是为MySql设置的knexfile.js
-特别是connection
对象。在该对象中,应将host
键替换为socketPath
;并在值前加上/cloudsql/
。这是一个示例:
connection: {
// host: process.env.APP_DB_HOST, // The problem
socketPath: `/cloudsql/${process.env.APP_DB_HOST}`, // The fix
database: process.env.APP_DB_NAME,
user: process.env.APP_DB_USR,
password: process.env.APP_DB_PWD
}
process.env.APP_DB_HOST
是您的实例连接名称。
PS:我想即使在不使用Knex
的情况下,典型的数据库连接字符串的host
或server
参数在以下情况下也必须称为socketPath
连接到Google Cloud SQL。