The Complete Guide to Building a Weather Data Chatbot – Part 1

Chatbots have become ubiquitous across online services. They can retrieve standard information, direct users to helpful resources, and connect customers to live agents. Here, we’ll explore how to create a simple weather chatbot that can help your users answer questions about weather conditions across The 93-mile Wonderland Trail in Mount Rainier, using IBM Watson to parse the intent and select the right responses, and the weather API from Tomorrow.io to provide the most hyper-accurate weather data.

Our goal is to create a simple interface that allows users to input a weather query and fetch a weather timeline forecast across any location on the trail. The massive trail circumference and significant elevation changes between campsites provide variation in the weather from campsite to campsite. Our chatbot’s design allows it to retrieve and display weather data for specific camps given the time, day, and interval provided by users.

1. Download server template

Open up your command-line interface for your OS. In the directory where you want to place the project, enter the following command. 

$ git clone https://github.com/jarrett-retz-tech-services/starting-repo-tomorrow-io-chatbot-nodejs-server.git

Then change directories into the downloaded folder, and run the commands below to install dependencies and create a new environment folder.

$ cd starting-repo-tomorrow-io-chatbot-nodejs-server 
$ npm install
$ echo TOMORROWIO_API_KEY= >> .env

2. Creating and training Watson Assistant

Sign in to IBM Cloud to get started for free, and search for “Watson Assistant” in the cloud catalog. Once you’ve selected that and set it up, you’ll see two side navigation items in the Watson Assistant dashboard: “Assistants” and Skills.” This is where we’ll build our skills of what responses we want, and then train our chatbot assistant to provide the appropriate responses. Set up two entities, one for an interval and one for the campground. Both will be passed to the Tomorrow.io API as parameters later.

Follow the instructions from IBM on how to complete setting up the chatbot for training, by creating your first dialog flow, adding dialogs, and then creating the Weather Timeline dialog flow. Here are examples of what the user might say, to train the assistant on this model:

  • Can you help me look up the forecast
  • I want to look up the weather?
  • I would like to know the weather
  • Weather forecast
  • What’s the forecast today?
  • What’s the weather today?
  • Will it rain today at Mount Rainier?

You’ll also need to upload the CSV file that is included as part of the NodeJS Server. That’s how the bot will be able to send coordinates to the Node server. Once you’ve set up your dialog flows, set it up to call a webhook, and created your entities, you’re ready for the next step.

3. Set up NodeJS Server

There’s a route located at /tomorrow-io in routes.js. This endpoint on our server is where we are going to send the data collected with the chatbot. Variables are hardcoded, pulled in from the environment, and accessed on the incoming request object. All we have to do is call the Tomorrow.io API and return a response. We use the campground coordinates for the latlong string as the location, replacing any spaces. Replace all the code in the try block with the function code below: 

...
try {
       // Call the tomorrow.io weather api
       const response = await axios.get(baseUrl, { params, headers: { apikey: apiKey } })

       // Check for the interval values from the requested timeline
       const intervals = response?.data?.data?.timelines[0]?.intervals;

       // Create a readable string for the user
       const stringInterval = intervals.map(item => {
           return `${new Date(item.startTime).toLocaleTimeString()} there is a ${item.values.precipitationProbability}% chance of precipitation. Temperature of ${item.values.temperature} degrees Fahrenheit.`
       }).reduce((item1, item2) => item1 + " ---- " + item2)

       return res.status(200).json({response: stringInterval})
} catch (e) (
...

The code has comments to explain what is happening during the essential steps of the API call. Next, we need to add our Tomorrow.io API key to the .env file. Copy your secret API key from Tomorrow.io and inside of .env, paste the key as the value for the TOMORROWIO_API_KEY variable. Save the file. 

Now it’s time for the exciting part – calling the Tomorrow.io weather API timeline endpoint from our chatbot. Check out Part 2 as we complete the chatbot and set up the interface to start responding to weather queries, or sign up for the Tomorrow.io API to get started for yourself!

Weather API Blog Signup

More from Tomorrow.io