“Hello World”, chatbot version – Complete example
Tags: helloworld, chatbots, example, java
Published: 2020-11-23 10:48:51

The <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" target="_blank" rel="noopener noreferrer">Hello World</a> program is the typical first example you see when learning any programming language since it was first used in a tutorial to learn B (predecessor of the C language) <a href="https://blog.hackerrank.com/the-history-of-hello-world/" target="_blank" rel="noopener noreferrer">in 1973</a>.  It is often the first program written by people learning to code. Its success resides in its simplicity. Writing its code is very simple in most programming languages. It's also used as a <a href="https://en.wikipedia.org/wiki/Sanity_check" target="_blank" rel="noopener noreferrer">sanity test</a> to make sure the editor, compiler,... is properly installed and configured. For these same reasons, it makes sense to have <strong>a "Hello World" version for chatbots</strong>. Such bot could be defined as follows:
<blockquote> A <em>Hello World</em> chatbot is a chatbot that replies "Hello World" every time the user greets the bot</blockquote>
So something as this:

<img class="aligncenter wp-image-119324" src="https://xatkit.com/wp-content/uploads/2020/11/helloworld.gif" alt="Hello World chatbot" width="282" height="404" />

While this chatbot is indeed simple (compared with any other chatbot), it's much more deceitful than its Hello World counterparts for programming languages. That's because of the<strong> <a href="https://en.wikipedia.org/wiki/Essential_complexity" target="_blank" rel="noopener noreferrer">essential complexity</a> of chatbot development. </strong>Even the simplest chatbot is a complex system that needs to interact with communication channels (on the "front-end") and the Text Processing / NLP engine (in the "backend"), among, potentially, other external services. Clearly, creating and deploying a Hello World chatbot is not exactly your typical Hello World exercise.

<img class="wp-image-119323" src="https://xatkit.com/wp-content/uploads/2020/11/chatbots_complex_systems.png" alt="Chatbots are complex systems" width="714" height="314" /> Chatbots are complex systems

But don't be scared, let me show you how to build your first chatbot with our open-source platform <a href="https://xatkit.com/" target="_blank" rel="noopener noreferrer">Xatkit</a>. Our <a href="https://xatkit.com/fluent-interface-building-chatbots-bots/" target="_blank" rel="noopener noreferrer">Fluent API</a> will help you to create and assemble the different parts of the chatbot. Let's see the chatbot code you need to write.

<h2>Recognizing when the user says "Hi"</h2>
The chatbot needs to detect when the user is greeting it. This is the only intention we need to care about. So it's enough to define a single <em>Intent </em>with a few training sentences. Any NLP Provider (e.g. <a href="https://dialogflow.com/" target="_blank" rel="noopener noreferrer">DialogFlow</a> or <a href="https://xatkit.com/chatbots-catalan-language-nlpjs-xatkit/" target="_blank" rel="noopener noreferrer">nlp.js</a>) would do a good job with this simple intent.

{% gist https://gist.github.com/jcabot/92a2ab0b77645f946b3914f77f1f1203 %}

<h2>Replying Hello World</h2>
To process the user greetings text, we need at least one state that replies by printing the "Hello World" text. But to keep the bot in a loop (who knows, maybe many users want to say Hi!), we'll use a couple of them.

{% gist https://gist.github.com/jcabot/e1f3f312bc6c7529567d07c5081dcfbf %}

<h2>Configuring the chatbot</h2>
As we mentioned above, chatbots come with some inherent essential complexity. At the very least, they need to wait and listen to the user on some channel and then reply to the same channel. In Xatkit, we use the concept of Platform for this. In the code below, we indicate that the bot is displayed as a widget on a webpage and that it will get both events (e.g. the page loaded event) and user utterances via this platform.

{% gist https://gist.github.com/jcabot/06b205099880624b30d74693c537d6f1 %}

And this is basically all you need for your Hello World chatbot!. Feel free to clone our <a href="https://github.com/xatkit-bot-platform/xatkit-bot-template" target="_blank" rel="noopener noreferrer">Xatkit bot template</a> to get a Greetings Bot ready to use and play with.

Of course, this is a very simple Hello World chatbot (e.g. what about if the user does not say Hi but something else?) but I think it's the closer we can get to the Hello World equivalent you're so used to see for other languages. Remember you can head to our main <a href="https://github.com/xatkit-bot-platform/xatkit" target="_blank" rel="noopener noreferrer">GitHub Repo for more details on Xatkit </a>or check some of our <a href="https://github.com/xatkit-bot-platform/xatkit-examples" target="_blank" rel="noopener noreferrer">other bot examples</a>.