Converse AI

Weather bot with Google Geocode module

This is a guide to building a simple bot to give you the weather forecast for today.

This is built using the Converse.AI designer, and the DarkSky Weather API, which is built into our designer as a Chatflow module.

Firstly, DarkSkys API requires a Latitude/Longitude pair for it to give an accurate weather forecast. I will also use another of our modules, Google Geocode.
When you first go into the designer, a single state is created called ‘startstate’. Each state allows to you to either collect or deliver information, as questions and answers, and also run Chatflow modules as required.

For the first startstate, we want to ask the user the location they need the weather for. So let’s do the following:
a) Rename your startstate to “city”
b) Change the question to be ‘Which city do you require the weather for’
c) Because the input could be almost anything, we want to accept anything as an input at this time, so lets add a context of sys-any, (don’t forget to hit return!)

Now drag the module over called “Geo Address or Lat/Long” from the left hand side (search for Geo if you can’t see it) and drop it underneath the first state.
This tells it to run immediately after getting an answer to the question in the first state.

If you haven’t used the Geocoding module before, it will pop up and ask you to register your API Key:

To do this, you will need a API Key from Google, more details available from here on how to retrieve this.

Once you have your key, Enter it under ‘Api Key’, hit save and close the Plugin window.
Now that the module is registered, we need to tell it how to reference the data collected from the ‘City’ question.

You can do this very simply using handlebars [Link to Handlebars docs here], in the form of {{states.statename.value}}, so in this case {{states.city.value}}.

That’s all you need to do to get the city name Geocoded, now for the weather module.

Drag it over underneath the Geocode module in the designer, and again it will popup and ask you to register.

To register for the weather module go to https://developer.forecast.io create an account, login and you will see your api key at the bottom of the website, copy this into the plugin registration page, save and close.

The designer layout should now look like this:

Now we need to reference the geocoded data from the Geocode module to pass into Forecast.
As the Geocode module references the latitude and longitude as separate fields, we need to reference them both in turn. Luckily the handlebars system is clever enough to cope with strings, tables and any variation, so it’s simple enough to put
{{states.city.geo.geocode.results.0.geometry.location.lat}},{{states.city.geo.geocode.results.0.geometry.location.lng}}

NOTE: There is a comma separating the two sets of handlebars, to make it the classic lat,long format.

So now we have the pieces together to geocode and get the weather data from Forecast.io.

Almost there, but one small problem, to make this truly international, we need to show the weather in Centigrade/Celsius as well as Fahrenheit, which we receive back from Forecast.io. To do that we need to use Converse AI’s maths functions to convert from one to the other.

For anyone that doesn’t know, the standard mechanism for F to C is (F-32)/1.8 = C
So firstly we need to remove 32 from the Fahrenheit value, to do this drag over the Subtract module to the bottom of the designer, and in the remove field enter 32. (Not -32)

In the number field enter the handlebar reference to the current temperature from Forecast.io Which is {{states.city.weather.forecast.currently.temperature}}

Now drag over a divide state and in the Divider field enter 1.8
In the number field enter the handlebar reference to the output from the subtract module {{states.city.maths.subtract}}

Now drag over the round module, set the round to 0 places and enter the handlebar {{states.city.maths.divide}} to tidy it up to a readable number.

With that all done, we now have the current temperature available to us in Fahrenheit and Centigrade

We also want to show the forecasted maximum and minimum temperatures in Centigrade as well. To avoid confusion of running the same modules twice in the same State, we are going to add another state for each of these, ‘weathermin’ and ‘weathermax’

Create ‘weathermax’ first by dragging ‘new state’ over from the side bar

Under your new state add the subtract module, set the subtract amount to 32, and the number reference using handlebars of {{states.city.weather.forecast.daily.data.0.apparentTemperatureMax}} (you will notice this handle bar is slightly different as we are looking for the max temperature for that day)

Now drag over the divide module, with Divider of 1.8 and Number reference of {{states.weathermax.maths.subtract}}

Drag over the round module with a round of 0 and Number reference of {{states.weathermax.maths.divide}}

Now drag a new state over for weathermin, and perform the same steps as above except use the following handlebars:
Subtract: {{states.city.weather.weather.daily.data.0.apparentTemperatureMin}}
Divide: {{states.weathermin.maths.subtract}}
Round: {{states.weathermin.maths.divide}}

Ok, we now have all the pieces together to get the data we need, so we just need to format the answer now. Again, we can do that using handlebars.

In the answer field of the weathermin state copy and paste the text below, as you will be able to see it references the data you have collected through the various states.

The current forecast in {{states.city.value}} is {{states.city.weather.forecast.hourly.summary}} The current temperature is {{states.city.weather.forecast.currently.temperature}}F / {{states.city.maths.round}}C
with a high of {{states.city.weather.forecast.daily.data.0.apparentTemperatureMax}}F / {{states.weathermax.maths.round}}C
and a low of {{states.city.weather.forecast.daily.data.0.apparentTemperatureMin}}F / {{states.weathermin.maths.round}}C

One last thing to do is add a trigger phrase, or phrases in this case I am going to add :
“Weather”
“Weather forecast”
“Forecast”
(To add your trigger phrase click on the icon top right that says “trigger phrases”)

Lets try out our new bot:

Weather bot with Google Geocode module