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 one 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 on how to acquire a Google Maps API key are available from here.

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, 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 Darksky module.

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

To register for the Darksky module here to create an account. After creating an account, when you login you will see your API key shown on the Account page. Copy this key and plug it into the plugin registration page in the designer, then save and close.

The designer layout should now look like this:

Now we need to pass the output from the Geocode module into the Darksky module.
As the Geocode module references the latitude and longitude as separate fields, we need to reference them by a comma. 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 Darksky.

Almost there, but to make this truly international we should show the temperature in Celsius, as well as Fahrenheit, which is received from the Darksky API. To do that we can convert the temperature Converse AI’s Maths functions.

The math behind this conversion consists of two steps: Subtraction and Division --> (F-32)/1.8 = C. 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 Darksky, 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.

We now should have the current temperature available to us in Fahrenheit and Celsius, and your workflow should look similar to the example below.

We also want to show the forecasted maximum and minimum temperatures in Celsius as well. To avoid confusion of running the same modules twice in the same State, we will need to add an individual State for each of these, ‘Weathermin’ and ‘Weathermax’

First create ‘Weathermax’ by dragging ‘New State’ over from the side bar

Under your newly added State, add the Subtract module, set the Remove 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 a Divider of 1.8 and a Number reference of {{states.Weathermax.maths.subtract}}

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

Now drag a new State over, then call it Weathermin and perform the same steps as above, except use the following handlebars referencing the Weathermin States:

Subtract: {{states.City.weather.forecast.daily.data.0.apparentTemperatureMin}}
Divide: {{states.Weathermin.maths.subtract}}
Round: {{states.Weathermin.maths.divide}}

Your design should look similar to this.

Okay, now we 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 full text below, as you will be able to see it references the data that 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: