Building a Jabber Bot
Posted February 28th, 2009 by Paul CoddingThe 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.
7 Responses to “Building a Jabber Bot”
February 19th, 2010 at 7:28 pm
This is rad. Thanks for getting me started in the right direction.
March 22nd, 2010 at 7:35 pm
“Smack is the client library that we will use for this project. Spark can be …”
Should ‘Spark’ be ‘Smack’ or have I missed something?
Informative article, Thank you!
March 22nd, 2010 at 8:49 pm
Thanks for the comment Ben. You’re right, Spark is a typo.
April 3rd, 2010 at 9:05 pm
Thanks for the article Paul. It helped me a lot!
November 23rd, 2010 at 1:31 pm
Perfect just what I was looking for! .
December 4th, 2010 at 12:12 am
Hello,
Really good thing. Can you suggest some PHP alternative or some alternative to imified.com – bot ? I want to build a gtalk bot.
August 27th, 2011 at 10:26 am
Good & clean work. THX!
Leave a Reply