首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google与WorldWeatherOnline API混淆

Google与WorldWeatherOnline API混淆
EN

Stack Overflow用户
提问于 2017-08-26 17:51:44
回答 1查看 566关注 0票数 0

所以我刚开始为Google助手创建api代理。我跟踪了api.ai文档中https://api.ai/docs/getting-started/basic-fulfillment-conversation的所有内容,以便下面的代理获取天气并返回响应。这是一个有用的代码,但我想在响应中获得创造性。我要做的是让这个国家从反应中得到答案,并决定是显示摄氏还是华氏相等于返回的温度。但是在控制台上测试的时候,它继续使用摄氏。我也想返回友好的提醒,根据温度,我将在以后添加。

代码语言:javascript
运行
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);
  });
});
 });
}
EN

回答 1

Stack Overflow用户

发布于 2017-08-28 13:03:35

与试图获取用户来自哪个国家不同,您可以使用getUserLocale获取用户的区域设置,用户可以根据自己的意愿进行更改,并且独立于他们的位置,这可能更好地反映他们喜欢的温度等级。

使用该方法,您将收到一个字符串,如'en-US''en-GB',这些字符串与一个国家相关联,因此您可以映射到特定的温度范围。使用本地化的示例可以找到这里

在Google模拟器上的操作中,您可以使用语言下拉列表更改区域设置,这样您就可以使用不同的地区测试应用程序。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45901072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档