Create a chatbot that tells you the weather forecast for any city in under 100 LoC

In particular, today, we show how you can create a chatbot with Xatkit that queries OpenWeather to give you the weather forecast in any city in the world. All this in less than 100 lines of code. You have the full code of the bot in our examples repository but we’ll see in this post all its key components.

Declaring the weather forecast intent

The first step is defining the asking for weather forecast intent. This is how you do it in Xatkit.

Note how as part of the intent we collect the name of the desired city. Also to help the NLP Engine, we declare that the City name should be part of the predefined city entity, comprising the list of all (major) cities in the world. Xatkit maps this to the corresponding city entity available in all the popular Intent Recognition Providers. So, make sure you configure the bot to be used with DialogFlow or Nlp.js (and remember that your choice of NLP Engine does not affect the bot code, just its property file).

Reacting to the weather forecast request

Once the intent has been matched, the bot must call the REST API of OpenWeather passing the requested city as parameter.

We first wait for the intent to match in an initial state for the bot

And once the match takes place the transition to the printWeather state to actually process the request. See the full code of this state below.

Remember that Xatkit comes with a REST platform abstraction that you can use to simplify the query and response processing of any external REST API. Thus, getJsonRequest helps you build the exact URL request to send to the API and collects the answer in the response object you can then use to check for the success status of the query and the easy access to the returned information.

If successful, from this response object, we build the reply to print back to the user. And go back to the initial state in case the user wants to ask for another city.

The weather forecast chatbot

You can see the resulting bot in action here.

Note that the bot is able to understand the user questions even if they are not an exact match of the training sentences as we using our DialogFlow connector as NLP provider in this case. Note that the bot is also giving you a nice reason to come live in Barcelona :-).

Obviously, this is a minimal example you could build on to expand the conversation or provide additional information. You have the full power of our Fluent API and state machine semantics backing you up!.

Photo by Wim van ‘t Einde on Unsplash




ICREA Research Professor at IN3 (UOC). Talking about software engineering, open source, AI and how the three of them can help each other.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Enhancing the Django “redirects” app

What Constitutes a Successful QMS?

How to write RFP requirements

Jinja2 — Server Side Template Injection (SSTI)



Kubernetes Cluster Automation

.NET: Prepare your company for the future

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jordi Cabot

Jordi Cabot

ICREA Research Professor at IN3 (UOC). Talking about software engineering, open source, AI and how the three of them can help each other.

More from Medium

Autocorrect Using NLP

KR-GEC: Need for Grammatical Error Correction Model for the Korean Language

How NLP is Transforming Supply Chain

“Can you believe what you read?” or “How well your model summarizes a text?”