0
votes

I am following step by step the starter guide of API.AI docs from their website, now i am stuck on the "fulfillment" section.

Everything is working but when i try to retrieve request info from my Actions on Google ,weather-app, then its not giving me response from fulfillment that I deployed. It still answer the default response that i hardcoded in the Weather-app agent.

'use strict';

const http = require('http');
const host = 'api.worldweatheronline.com';
const wwoApiKey = '***i used key here from their website***';
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 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);
        });
    });
  });
}
1
This looks like the code directly from the api.ai sample - what isn't working about it? It sounds like you have figured out the key, so I'm not sure why the title of your question talks about it. You mentioned Actions on Google, but didn't tag it - are you using Actions or not? Further clarifying your question to explain what you're doing and what changes you made or didn't make would really help. - Prisoner
when i upload and attach sample webhook example from api.ai through google cloud platform on the 'action on google' console,it works fine.but when i use the next example of Weather api i given code on doc of api.ai. and follow the steps as given when i again upload the updated webhook and attach it with my api.ai agent,then weather api is not responding.i am new to stack overflow,i am learning how to use this platform too - Hassan
Welcome to StackOverflow. Typically, as much as possible, you should edit your question to provide more details. As much as you can indicate that would help others reproduce or help you solve your problems. In this case, if you can update your question to show code that works, and code that doesn't work, and explain how you are running each, it might help us further to help you. - Prisoner
thanks to give me some kind of confidence to share my future problems here,it took me few days but i figured out that problem.i used Yahoo weather api in my assistant app and it works properly.I am glad that you responds to my problem. - Hassan

1 Answers

0
votes

I had the same issue as of Aug 2018, and the go-to answer always seemed to come back to inputting "api.worldweatheronline.com".

I was able to get mine working by typing in the address below. Make sure you input your two key correctly too.

 api.worldweatheronline.com/premium/v1/weather.ashx?key=yourwwokey

This seems to do the trick, although I don't know why the syntax of the address has changed from one to the other.