Easy 2 Call Weather API in Rasa


Chatbots are the future of this generation and those are capable enough to understand what we say and to respond accordingly. Also, those can work as an assistant for us just like Google Assistant and Alexa. To work as an intelligent assistant, chatbots understand us and help us with our work by taking actions, but how do they do it.

So, this what exactly you will learn in this blog and after completing this blog you will be able to make your bot do it for you. So, let’s get started and learn how to do it.


In this blog, you will learn,

  1. What packages are required to handle API?
  2. How to call an API in python
  3. How to call weather API in rasa chatbot.

Firstly, let’s understand what is an API. API or Application Programming Interface is a software intermediary that allows two applications to communicate with each other. Whenever you are using an application like Facebook or Instagram or twitter to send instant messages or checking the weather on your smartphone, you’re using an API. In other words, your application is capable of taking action to help you instantly connect to your loved ones.

Now, let’s understand how to use and call an API in python. You will use the package name “requests”, that is capable of handling the requests from any API. It fetches and updates the data with the GET/POST methods respectively.


So firstly you have to install the request package in your current rasa environment so that we can implement the calling of weather API in rasa chatbot. To install it enter the following command in the terminal:

$ pip install requests

This will install the requests package in the rasa environment and then you can call an API.

In this blog you will use the weather API, to understand the concept.


Here is the script for calling weather API with the name weather.py that will implement this task,


import requests

def Weather(city):

url = api_address + city
json_data = requests.get(url).json()
format_add = json_data['main']
return format_add

To see it’s practical implementation check this,

In this code above, you can see firstly requests module is imported. Then, we have created a function that takes an argument from the user as a “city” name to check the weather as per the city entered by the user. Inside the function, there is a variable with name api_address which holds the weather API in string format without the city name so that you can modify the API according to the city that the user wants to check for.

After that here comes the main functionality of the requests module that you have installed here. When you have the complete API with the city name added to it, you can send a get request to this API to fetch the data in json format. To do that we used,

json_data = requests.get(url).json()

if JSON is a new term for you, then to understand it considers the data stored in a dictionary in python programming. JSON stores the data in the same way as the dictionary does. When you will have fetched the data from this API then you can slice the data that you require. In our case we will fetch the data with respect to key ‘main’, as the values corresponding to the weather will be stored here so we have used,

format_add = json_data['main']

At last this will return the data with,

return format_add

So, this is the complete script for calling the weather API. Now, it’s time to integrate this script with rasa chatbot to call it from the actions file with respect to the city entered by the user. First of all, build a basic chatbot on your system to have a small conversation with the chatbot to ask for the weather via weather API.

Build your Weather assistant

If you are new to rasa chatbot and don’t know how to build one the click here and build your first chatbot in an easy way. But if you already know how to build one you can directly check this link and get the complete code to get started.

Download Weather API Assistant Full Code

When you have your basic weather assistant chatbot ready with you. Now you can understand how to call the function that you have created above to call an API in actions.py. To do that add this code to your actions.py file,

from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from weather import Weather

class ActionHelloWorld(Action):

def name(self) -> Text:
return "action_weather_api"

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

return []

Here, what exactly we are doing. Your chatbot is asking the user to enter the city name for which the user wants to check the weather by calling the weather API. When the user will enter the value your chatbot will be able to read this text and to further use this city name to find the weather of your city by calling the weather API,


This will give you the temperature value of the entered city name and accordingly you can reply to the user with this value using,


This will call the utter_temp action from the domain file with the temperature value fetched by calling the weather API. Here, are the updated domain, nlu, and the stories file where you have to make the changes.


## intent: weather
- what's the weather
- i want to know the weather of today
- tell me the weather forcast
- hows the weather today

## intent: city
- Delhi
- delhi
- chandigarh
- mohali
- mumbai
- jaipur


- text: "which city you want to check for?"
- text: "Today's temperature is {temp} degree Celcius."
- utter_city
- utter_temp
- action_weather_api
- weather
- city
## happy weather
* greet 
  - utter_greet
* weather 
  - utter_city
* city 
  - action_weather_api

To see the practical implementation check this

After these updations in the file, train the model and run rasa shell,

$ rasa train && rasa shell –debug


$ rasa train && rasa x

parallelly run the actions server in new terminal,

$ rasa run actions

If everything goes right then you will be able to see the output like this,

This image is the output of our chatbot that we have created in this session to call the weather API and use use our chatbot as a weather assistant.

Time to wrap up now. Hope you liked our content on How to integrate weather API with Rasa chatbot. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more Rasa Chatbot contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, Python Programming, etc.
Become a member of our social family on youtube here.

Stay tuned and Happy Learning. 😉

Ashish Saini
Ashish Saini

I am a software developer for Python/Machine Learning. Also, I’m Rasa Hero at Rasa. Also, I’m a youtuber where I regulary uploads the videos to help this generation learn about the technology just like they are playing games.

Leave a Reply