Create a chatbot that tells you the weather forecast for any city in under 100 LoC
Combining chatbot platforms with open data sources is one of the most powerful ways to easily create chat interfaces that provide useful information to its users. This is what we call Open Data chatbots.
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!.