Java chat bot – Free open source chat robot

Introduction

MajBot is a Java-Based chat bot which responses based on the messages that it receives. It uses an external data file (XML file) to make it easier for users to extend its features and its domain. MajBot uses dynamic features to retrieve weather information from Yahoo Weather API. It allows multiple messages per state to make it possible to display random message for similar questions to make the bot more interactive.
Each state can expect multiple keywords or no keywords which returns user to the default state. It also supports regular expressions for better user input processing. MajBot can learn new keywords by asking questions from user and provide their answer for them in the future questions.

License: GPL, download link is available at the bottom of this article.

At a glance

It is easy to customize the MajBot by configuring data.xml file, this file contains different states and keywords inside each state, once a keyword is matched, user will be transferred to the target state, MajBot stats from State “0” by default, here is a simple configuration file:

In the above example, If user enters happy keyword, bot moves to State “1″ and display one of state “1″ messages randomly, If user enters any other strings, one of the invalid messages will appear.

Keyword tag contains multiple attributes to make it possible to customize the bot according to the requirements.

Navigation

To navigate user between different states, keyword tag is being used with target attribute, the matching keyword will transfer user to its target, it is possible to use multiple keywords and multiple messages for each state, in this case everytime a random message appears. It is easy to use multiple keywords by separating the keywords by comma, if any of these keywords matches, user will be transferred to the target state:

To use weather feature, keyword tag should contain two attributes, className and arg, className should set to Weather and arg should set to “today”, “tomorrow” or “dayaftertomorrow” keywords. Here is an example to get today’s weather:

<keywordclassName="Weather" arg="today">weather</keyword>

Result:

You: how is the weather
Bot: I think today is Fog

Catch all

To transfer user to a custom state when user enters an unexpected keyword, “*” symbol can be used:

Regular Expressions

Using Regular Expression makes it possible to process user’s input and extract data from their inputs; MajBot stores these data into a dictionary and it is possible to use these dynamic data in different states, following example demonstrates how to ask a user’s name and response with a greeting message with user’s name in the greeting:

Bot: Hello, My name is MajBot, what is your name?
You: Majid
Bot: Hello majid, nice to see you!

In the above example “([a-zA-Z )+)" regex accepts any characters betwen a to z (case insensitive) including spaces and store the matching word to name variable, then by using [name] in the message, user’s name will be replaced with this tag. (For more information about Regex syntax refer to Java Regex Documentation).

Adding priorities to keywords

By default the longest matching keyword will be the best match, however sometimes we need to give extra points to some of keywords so in case both of the keywords matches, the keywords with higher points selected for the best match, in this case we can use points attribute:

Bot: Hello, My name is MajBot, what is your name?
You: my name is Majid
Bot: Hello majid, nice to see you!

In the above example we are adding 2 points for the first keyword so if user enters “my name is Majid”, it only gets the name instead of returning “My name is Majid” as name.

Learning new words

It is possible to ask the user about unknown keywords and add dynamic keywords and states for the new keyword, if in the future user enters same keyword, his answer will be replied back to the user. For this purpose we can use variable tag to get user’s keyword first and then asking a question about that subject and storing the subject result by using learn attribute:

Bot: What do you want to talk about?
You: football
Bot: What is football?
You: it is a good game
Bot: I see...
Bot: What do you want to talk about?
You: football
Bot: it is a good game

As it is shown in the above example, bot learns about football keyword and then send the response back to the user when user sends same keyword again.

How to run MajBot

MajBot includes 7 classes and one data.xml file. It also uses Apache HttpdClient libraries to connect to Yahoo Weather API. All of the classes are compiled into MajBot.jar file and you can run the jar file using provided run.sh file which contains:

java -cp MajBot.jar bot.Main

To run MajBot you can simply type:

sh run.sh

If you are using windows, you can run run.bat file instead.

After starting the bot, user can enter a message and start chating with MajBot.