Build A Real-Time Weather Alert Chatbot in 3 Sections Using Weather API

AmbeeAmbee
4 min read

Chatbots simplify the communication between users and the program. Nowadays, innovation in chatbot platforms by combining them with AI and IoT is popularly used to minimize the steps of a process.
Many chatbot platforms are collaborating with data sources to create chat interfaces that can seamlessly provide information to their users. This latest innovation is called open data chatbots. These are great options as they provide immediate responses round the clock.

In this blog, we will implement an open data chatbot that uses Ambee’s accurate weather information to provide a conversational chatbot. This can give you weather alerts on interacting with it.

This chatbot implementation can be divided into three different technology frameworks:

  1. Ambee Weather API
  2. RASA - provided ML intelligence in the form of automatic conversation
  3. React Native - the look and feel of the chatbot feed

1. Weather data

To fetch the weather data, we are using Ambee’s weather API, which allows a user to make API calls. Once the user is authorized, they will be given an API key which can be used to make the call.

2. Rasa

This ML package uses Python for its setup. The following steps need to be followed to create a virtual environment and perform the implementation

pip install rasa

To cross-check that we have installed the package,

pip show rasa.

Ensure that you are in the root directory to initialize the package

rasa init

This initialization is used to train and start the package.

Once the initialization is done, we can see multiple files created in the same directory, which are config.yml, domain.yml, endpoints.yml, and action.yml.

Now we need to customize certain aspects of these files based on our requirements. This simple implementation focuses on providing weather data, so firstly, in the endpoint file, we must uncomment the endpoint so that the chatbot application can be hosted in that particular location.

In this directory, you can also see a file named nlu.md.

NLU is Natural Language Understanding which deals with comprehending what needs to be done.

In this file, we will add the possible ‘intents’ so that once the model comes across this user input it will know the respective output to deliver.

## intent:ask_weather
- what is the current weather
- may i know the weather

Based on how we want the chatbot to respond, we can make custom changes to domain.yml in the following manner

intents:

                       - greet
                       - goodbye
                       - ask_weather

actions:

                       - action_ask_weather

response:

                      utter_greet:
                      - text: "Hey! How are you?"
                      utter_goodbye:
                          - text: “Goodbye” 

session_config:
 session_expiration_time: 60
 carry_over_slots_to_new_session: true

The change made to stories.md consists of the output action to be made.

## weather
* ask_weather
 - action_ask_weather

The implementation code exists in actions.py. Find it by typing import Any, Text, Dict, List from rasa_sdk import Action, Tracker from rasa_sdk.executor, import CollectingDispatcher import requests

weather_dict = {'rain': 'It is raining outside.', 'rain_light': 'Light rain around the area.', 'drizzle': 'It is drizzling outside.', 'cloudy': 'It is cloudy outside.', 'mostly_cloudy': 'The sky is covered with clouds.', 'partly_cloudy': 'It is partly cloudly outside', 'mostly_clear': 'Sunny with presence of small clouds.', 'clear': 'It is clear and sunny outside.'}
url = "https://api.ambeedata.com/latest/by-lat-lng"
querystring = {"lat":" {}","lng":"{}",x-api-key = ””}

class ActionAskWeather(Action):
    def name(self) -> Text:
        return "action_ask_weather"

    def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

   response = requests.request("GET", url, params=querystring)
   result = ""

json_data = response.json()

result += 'Average temperature is %s%s while the humidity is about %s%s.' % (json_data['temp'], json_data['humidity'])

The parameters latitude, longitude and api-key needs to be added based on the case. Once this code has been saved, the rasa model needs to be trained.

rasa train

To perform the action once the request is made on the server,we need to run the command.

rasa run actions

Once the action server is running, to activate the virtual environment, go back to the directory where the files exist and run the command.

rasa run --enable-api --cors "*"

Cors - error handler

3. React-based Chatroom

This section involved the UI of the chatbot. There are multiple UI frameworks that are readily available. Here, the framework created by ScalableMinds has been used by cloning their repository. The only prerequisite for the UI is to have Node.js installed on the system.

When we move to the chatroom-master directory there is a file called package.json. Here we can specify what we are going to use for the UI.

"prepare": "npm build",

Then, we install the necessary modules,

npm install

The CSS and HTML files pre-exist within the chatroom-master directory. Note: Before you build the files, make sure that you are using port 5005 for the Rasa Server.

To build the files, we should run the following command

npm run build

Once the code has been built, a minimal version ‘dist’ gets created and the output of all the HTTP server URLs is displayed. Open the index.html on that localhost and it loads the chatbot interface that can now be tested. Finally, based on the intent that we have specified in the domain.yml, it can generate answers to only those questions in real-time.

4
Subscribe to my newsletter

Read articles from Ambee directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ambee
Ambee

Ambee provides hyperlocal environment and climate data for locations across the world. Using proprietary data science techniques, Ambee delivers location-specific, real-time data and actionable insights on air quality, weather, pollen, and various other environmental factors. Ambee’s data is trusted by businesses and administrators across the globe. Founded with an aim to democratize access to environmental data and tools, Ambee aims to enable a better and healthier living experience for all.