I've created this blog to catalog software ideas, code examples and everything in between. I've been in the industry for over 5 years and have worked on a variety of projects from Identity and Access Management, to Web Application Security, to Java/Ruby application development.

Read More

Building a Jabber Bot

The Idea

After a great suggestion from a colleague I was inspired to create what I think is an under-used utility.  That utility is an Instant Messaging Bot.  I know they seem dated, and harken back to the days of AIM bots, but in this world of rapid publishing and communications I think they’re an under utilized tool.  This post is meant to highlight the use of two technologies: Java, and Openfire.

The Technology

Openfire is an open-source Jabber(XMPP) server.  The installation is quite simple, the only thing that is required is a JRE, and if you would like, a MySQL database.  In my installation I have installed Openfire on Ubuntu JeOS that has the sun-java6-jdk and mysqld-server packages installed.  Openfire is distributed as a debian package, so installation is a breeze.  You can read more here on how to install, this post will assume you have a working server and a Java development environment…with maven.

The Application

We want our application to login to the Jabber server, and listen for messages bound for the bot user.  Once a message is received it should be checked for a command, and a proper response should be sent back to the user.  In order to accomplish this we need three classes.  One class will handle logging into the Jabber server and beginning to listen for chat messages.  The other class will be invoked when a chat is initiated, and the other will handle messages sent in that chat session.

Smack is the client library that we will use for this project and can be download from the same site that you downloaded Openfire from.  Smack includes the two interfaces we will use for the class that listens for chat requests, and handles chat messages.  These interfaces are ChatManagerListener, and MessageListener.

The Code

We’ll start by adding our dependency for the Smack client library:

<dependency>
  <groupId>jivesoftware</groupId>
  <artifactId>smack</artifactId>
  <version>3.0.4</version>
</dependency>

Now that we have the necessary dependency we can focus on our three classes. We will start with our App class. This class as stated before is responsible for connecting to the Openfire server. Here is the App.java class.

public class App {
  public static void main(String[] args) {
    // Assumes you have the SRV records setup in DNS
    XMPPConnection conn = new XMPPConnection("yourdomain.com");
    try {
      conn.connect();
      conn.login("IM_BOT", "Your Bot Password");
      ChatManager chatManager = conn.getChatManager();
      chatManager.addChatListener(new BotChatListener());

      System.out.println("Connected");
      while (conn.isConnected()) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      conn.disconnect();
    } catch (XMPPException e) {
      e.printStackTrace();
    }
  }
}

Our next class is the class responsible for listening and handling incoming chat requests and adding a message listener to the new chat. Here is the BotChatListener.java class.

public class BotChatListener implements ChatManagerListener {

  public void chatCreated(Chat chat, boolean local) {
	  chat.addMessageListener(new BotMessageListener());
  }
}

The MessageListener referred to in the BotChatListener is responsible for handling commands passed to the bot and responding to them appropriately. Here is the BotMessageListener.java class.

public class BotMessageListener implements MessageListener {

  public void processMessage(Chat chat, Message message) {
    System.out.println(chat.getParticipant() + " said -> "
        + message.getBody());
    Message botMessage = null;

    try {
      botMessage = new Message();
      if (message.getBody().equals("nothing")) {
        botMessage.setBody("Fine...");
      } else if (message.getBody().equals("make me toast")) {
        botMessage.setBody("Sure thing");
      } else {
        botMessage.setBody("What do you need?");
      }
      chat.sendMessage(botMessage);
    } catch (XMPPException e) {
      e.printStackTrace();
    }
  }
}

And that’s it…the next step is to extend the BotMessageListener class to do whatever it is you want to accomplish with your bot. If you wish, you can make a Ferris Bueller type bot, and use it to log into Google Talk with your Gmail account and respond to your co-workers messages while you’re out doing whatever it is that you do for fun.

Filed under:Java

7 Responses to “Building a Jabber Bot”

Leave a Reply