Creating a XMPP Chat Bot with Apache Camel

October 12th, 2014 by

Apache Camel not only is one of my favourite frameworks ever but it also allows the humble developer to create a full blown chat bot within a few lines of code and using the Camel XMPP component.

In the following tutorial, we’re going to create a simple chat bot and since Atlassian’s HipChat basic plan is now free for unlimited users, we’re using HipChat as our play- and testing ground for the bot.

XMPP Bot connected to HipChat

XMPP Bot connected to HipChat


Project Setup / Dependencies

Using Maven, we’re adding three dependencies to our pom.xml: Camel Core, Camel XMPP Components and Config-Builder for our configuration management.


We’re using Camel with Java in this tutorial – but if you prefer using Scala, porting the following examples shouldn’t be a problem – please feel free to have a look at my blog article “Using Apache Camel with Scala and the Camel Scala DSL” for further details about the Camel Scala DSL.

HipChat Setup (optional)

HipChat with the basic plan is free with unlimited users and no need to enter credit card information or stuff like this.

So basically to create a HipChat instance for our bot we need to do..

  • Register a new account at
  • Create a new room
  • Create a user account for the bot
  • Make sure, that the bot-account may access the room

That’s all …

Creating the Bot

Now that the playground for our bot conversations is ready we’re ready to write the bot application with a few lines of code.

Managing Configuration

First of all, we’re using a property file to hold our configuration details. To access this information, I’m using the config-builder library here that allows us to bind fields of a POJO to the elements of the properties file using simple annotations.

If you’re interested in some more details here, please feel free to have a look at my blog article “Using Java Config-Builder to assemble your Application Configuration“.

This is our configuration class:

import com.tngtech.configbuilder.annotation.propertyloaderconfiguration.PropertiesFiles;
import com.tngtech.configbuilder.annotation.valueextractor.PropertyValue;
public class Config {
	private final int port = 5222;
	private String host;
	private String user;
	private String password;
	private String room;
	private String nickname;
	// getter, setter ommitted ...

This is our config file src/main/resources/ fill the valid properties according to your environment or HipChat account:

nickname=hasCode Bot

Bot Logic and Camel Routes

Basically we’re doing all the work with a single route. We’re able to create an XMPP endpoint using an URL like this: “xmpp://user@host:port?room=chatroom&password=xxx&nickname=Bot“.

When a new message arrives from the XMPP endpoint we’re first filtering for messages containing a valid bot command – valid commands are:

  • bot date: displays the current date
  • bot help: displays a usage hint

Having filtered the chat messages for valid bot commands, we’re now using a content-based-router to delegate to the appropriate command handling.

Finally we’re sending some output to the XMPP endpoint so that the bot talks to the designated HipChat room.

import java.time.Instant;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.xmpp.XmppMessage;
import org.apache.camel.main.Main;
import org.jivesoftware.smack.packet.Message;
import com.tngtech.configbuilder.ConfigBuilder;
public class Bot {
	public static void main(final String[] args) throws Exception {
		new Bot().run();
	private void run() throws Exception {
		Config config = new ConfigBuilder<Config>(Config.class).build();
		String chatEndpoint = String.format("xmpp://%s@%s:%s?room=%s&password=%s&nickname=%s", config.getUser(), config.getHost(), config.getPort(), config.getRoom(), config.getPassword(),
		Main main = new Main();
		main.addRouteBuilder(new RouteBuilder() {
			public void configure() {
				.filter(exchange -> isValidBotCommand(exchange))
					.when(exchange -> getSmackMessage(exchange).getBody().contains("date"))
						.setBody(constant("It's " +
						.setBody(constant("Available commands: bot date, bot help"))
	private boolean isValidBotCommand(final Exchange exchange) {
		return getSmackMessage(exchange).getBody().matches("bot (date|help)");
	private Message getSmackMessage(final Exchange exchange) {
		XmppMessage xmppMessage = (XmppMessage) exchange.getIn();
		Message smackMessage = xmppMessage.getXmppMessage();
		return smackMessage;

The Bot in Action

Running the bot should not be a problem, we simply need to fill the with our account information and should afterwards be able to do some bot-conversation as shown in the following screen-shot.

Run it from your IDE or using the following Maven command:

mvn exec:java
XMPP Bot connected to HipChat

XMPP Bot connected to HipChat

Tutorial Sources

Please feel free to download the tutorial sources fromĀ my Bitbucket repository, fork it there or clone it using Git:

git clone


Article Updates

  • 2014-11-04: Added Maven snippet to run the bot.