【发布时间】:2018-02-04 15:57:55
【问题描述】:
所以我刚刚开始为 Google 助理创建 api 代理。我在https://api.ai/docs/getting-started/basic-fulfillment-conversation 关注了 api.ai 文档中的所有内容,因此下面的代理获取天气并返回响应。这是一个有效的代码,但我想对响应进行创意。我的目标是从响应中获取国家/地区,并决定是否显示与返回温度相当的摄氏度或华氏度。但是在控制台中对其进行测试后,它继续使用摄氏度。 我还想根据稍后添加的温度返回友好提醒。
'use strict';
const http = require('http');
const host = 'api.worldweatheronline.com';
const wwoApiKey = '[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 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 minTemp = forecast['mintempC'];
let maxTemp = forecast['maxtempC'];
let unit = '°C';
let location = response['data']['request'][0];
//get country
let country = location['query'].split(",")[1];
let conditions = response['data']['current_condition'][0];
let currentConditions = conditions['weatherDesc'][0]['value'];
if(country == "Liberia" || country == "Myanmar" || country == "United States of America")
{
minTemp = forecast['mintempF'];
maxTemp = forecast['maxtempF'];
unit = '°F';
}
// Create response
let output =
`Current conditions in the ${location['type']}
${location['query']} are ${currentConditions} with a projected high of
${maxTemp} ${unit} and a low of
${minTemp}${unit} on
${forecast['date']} in ${country}`;
// Resolve the promise with the output text
console.log(output);
resolve(output);
});
res.on('error', (error) => {
reject(error);
});
});
});
}
【问题讨论】:
-
欢迎来到 Stack Overflow!这些类型的问题(复习)更适合Code Review。除非您有特定问题,否则我建议您在那里发帖(请参阅How to Ask)。
-
嗨!对于那个很抱歉。那我把这个帖子移到那里去。谢谢!
-
看来您实际上并不是在要求进行代码审查,而是在寻求帮助修复您不符合您要求的代码。您应该 edit 您的标题以更好地反映这一点,并帮助防止您的问题被关闭。
标签: node.js dialogflow-es actions-on-google weather-api