Posts Tagged ‘XMPP’

Episode 2 : Update : Communicating to another XMPP Account via your Bot

October 9, 2009 6 comments

I received a good comment on my second post, where we saw how to write your own XMPP Bot and deploying it on the Google App Engine for Java (GAEJ).

It asked whether one can communicate to another Jabber account from the XMPP Bot hosted on the GAEJ. The confusion arose because I covered the usage of Google Talk, which is an IM client itself. And it looked from my post that you can only use Google Talk as the client to talk the XMPP agent that you wrote.

The short answer is that yes, even if you are using another Instant Messaging (IM) client like Spark or Pidgin, it is possible to communicate to the XMPP bot that you have written and which is running inside the GAEJ cloud.

However, it turned out that I made a mistake in writing the code for the XMPP bot. The Google App Engine XMPP documentation clearly states that you can communicate to any other Jabber ID but it is not possible for their infrastructure to check for the presence of a Jabber ID on another network except their Google Talk network. This is fair enough. By presence, we are simply trying to see if the user is online or not.

So , we had some code in our XMPP bot that we saw earlier, which went something like this. This code fragment was at the very end when we are sending back the echo for our Agent.

if (xmpp.getPresence(fromJid).isAvailable()) {
SendResponse status = xmpp.sendMessage(replyMessage);
messageSent = (status.getStatusMap().get(fromJid) == SendResponse.Status.SUCCESS);

What we are doing here is that we are checking for the presence of another JabberID i.e. making sure that is online before sending the message. This works fine on the Google Network but not on other XMPP Networks like So for e.g. our bot Jabber Id was And if the Jabber Id which is talking to it is, then it will not be possible for your bot running inside of the Google network to determine if is online or not. As a result of this, the code will never enter the block and the message does not get sent.

So, all you have to do to send the message across to another network is to remove the check for the isAvailable() condition and simply call the sendMessage(…) method.

I have verified this by doing the  following:

  1. Used another IM client instead of Google Talk. I used the IM client called Spark
  2. I have an account at which I used to login as shown below in the Spark IM client:post4-3
  3. I added as a contact in Spark IM client.post4-1
  4. I sent a chat message across to the gaejxmpptutorial bot and got back the echo as shown below:post4-2

Hope this clarifies that it is possible to communicate to your bot over XMPP from other XMPP Servers. Let me know if any of you are still having issues and I welcome your comments.

Categories: Uncategorized Tags:

Episode 2 : GAEJ + XMPP and rolling your own Agent

September 25, 2009 39 comments

I hope all of you have got started with development of GAEJ applications using the nifty Google Eclipse plugin. If not, I strongly recommend that you read it here. In this episode I will be discussing one significant enhancement that Google has brought about to their latest GAEJ release and that is XMPP support.

XMPP Protocol has for long been the backbone for a lot of systems till now and most likely in the future. It stands for Extensible Messaging and Presence Protocol. Whew! That was quite a complicated expansion if you ask me. But fear not. It emerged from the Jabber protocol and is in use today in a lot of Instant Messaging Systems. Google Talk is a prime example of an application that utilizes XMPP as its backbone. Several other IM clients utilize XMPP too. Several XMPP APIs in a variety of languages are available too for you to hack your XMPP client any time.

Agent in Action

What are we going to build today, you ask? We are going to a build a XMPP Agent that we can add in IM clients like Google and talk to it. We are going to cover here all that is needed for you as a Java developer to use GAEJ and get a basic XMPP Agent application working.

To see it in action, I suggest you do the following:

  1. Start Google Talk on your machine.
  2. Add as a friend. If it asks you to add itself as a friend, please do so.
  3. Send it any message you like. For e.g. Hello.
  4. It will reply back with whatever you typed.

Here is a running sample screenshot of what we will be achieving today:


We will cover how to write the same XMPP Agent (gaejxmpptutorial) using GAEJ. Once you get the mechanics in place, the world is your oyster when it comes to writing Agents that do what you want it to do. All you need to then write is your Agent logic and GAEJ platform does the rest for you.

First things first

Here are a few things that I shall assume:

  1. You have an appropriate Eclipse setup along with the GAEJ for Eclipse plugin configured correctly
  2. You have a Google Account and know what it means to register an application under your account

If you are unclear, I suggest to read up my first episode.

Our “Hello World” XMPP Agent

At a high level, we will be creating a XMPP Agent that will do the following:

  • The Agent will be hosted in the GAEJ system. The application name will be any application id that you select for your account (More on that while deploying).
  • This Agent can be added as your friend/buddy in Google Talk or any XMPP IM client
  • You can type out a message to the Agent and it will simply send back the message that you typed.

Alright, let’s go.

Create a New Project

The first thing to do is to create a New Google Web Application Project. Follow these steps:

  1. Either click on File –> New –> Other or press Ctrl-N to create a new project. Select Google and then Web Application project. Alternately you could also click on the New Web Application Project Toolbar icon as part of the Google Eclipse plugin.
  2. In the New Web Application Project dialog, deselect the Use Google Web Toolkit and give a name to your project. I have named mine GAEJXMPPTutorial. I suggest you go with the same name so that things are consistent with the rest of the article, but I leave that to you.
  3. Click on Finish

This will generate the Google Web Application Project, which consists of a sample Servlet that prints out “Hello, World”.

Understand the GAEJ XMPP API

Before we write our Agent, we need to understand first the Google XMPP API. I will cover it in brief over here and the rest of the details you can understand from the link provided, once you need to dig deeper. Make no mistake, the API is detailed and you will need to refer to the finer points as the scope of your XMPP interaction increases but for our starter version, we do not need to know too much.

Think for a moment that you are the Agent that people are going to add to your Google Talk IM client. What do you need to do at a basic level. It all boils down to this:

  1. Receiving a message
  2. Interpreting it and composing an appropriate response message (This is where your Application logic will come in).
  3. Sending a message.

Let us look at each of the 3 areas now and what the API looks like. Please note that all XMPP API classes are present in* packages.

Receiving a message

Since XMPP support is now part of the GAEJ infrastructure, all GAEJ applications are capable of receiving an incoming XMPP Message. So once your application is hosted in the GAEJ cloud, the application can receive messages from other XMPP clients like Google Talk. To enable this receiving of XMPP messages from a code level, you need to do two things:

– Enable your application to receive XMPP messages by configuring the XMPP Service in your application. This is straightforward and all you need to do is add the following element to the appengine-web.xml file. The appengine-web.xml file as you know is specific to the Google Java Web Application project and is used for configuring certain services, XMPP being one of them. It is found in the war\WEB-INF folder of your Web Application Project. The XML fragment to add at the end but before the </appengine-web-app> element.


We will cover this in detail again later, but this is sufficient for the moment.

– Configure and code a Java Servlet that will receive the incoming Message. All XMPP messages to your application are delivered via POST to following URL path in your application: /_ah/xmpp/message/chat/. So you will need to configure the servlet like the following snippet in the web.xml file, present in the war\WEB-INF folder of your Web Application Project.



In the above snippet, you will find the fixed URL path /_ah/xmpp/message/chat/ configured as the <url-pattern/>. And then I have a Java Servlet class com.gaejexperiments.xmpptutorial.XMPPAgentServlet as the <servlet-class>.

Now, all we have to do is write our Servlet. As mentioned, the incoming XMPP Message will be POSTed to our Servlet, so we need a simple doPost(…) implemented in our Servlet. The skeleton is shown below:

 package com.gaejexperiments.xmpptutorial;
 //Other imports
 public class XMPPAgentServlet extends HttpServlet {

 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
 //get the incoming message from the Request object i.e. req
 // interpret it and compose a response
 //send the response message back

Interpreting the incoming message and composing a response

To interpret the incoming message, we need to extract out the message that has been passed to us in the HTTP Request. It is done in the following manner:

XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);

The msg object will contain the XMPP Message. There are several elements in the XMPP message that are of interest to us and chief among them being who has sent the message and what is the text of the message that has been sent to us. This is extracted out by the getter methods available on the Message class as shown below:

JID fromJid = msg.getFromJid();
String body = msg.getBody();

The JID class represents the Jabber ID of the sender whereas the body will contain text of the message sent to us.

Once you have the message, you can interpret it and then create a XMPP message that you need to send back i.e. respond with. To compose an XMPP Message, here is the API:

Message replyMessage = new MessageBuilder()

The above method is straightforward and we will see it more in the next section.

Sending a message

Finally, we have our message and all we need to do is send it. To do that, here is the API. We first check if the recipient is available i.e. Online and then send the message using the sendMessage method.

XMPPService xmpp = XMPPServiceFactory.getXMPPService();
//Compose the Message Object i.e. replyMessage
if (xmpp.getPresence("JABBER_ID_OF_RECIPIENT").isAvailable()) {
SendResponse status = xmpp.sendMessage(replyMessage);
//Take appropriate action based on status SUCCESS or FAIL i.e. log an error, update database, etc

Write our Servlet and configure it

Create a new Java Servlet class named XMPPAgentServlet in the package com.gaejexperiments.xmpptutorial. You can use your own namespace and Java class name if you wish. The entire source code for the Agent is shown below:

package com.gaejexperiments.xmpptutorial;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.logging.Level;
import java.util.logging.Logger;


//STEP 1
public class XMPPAgentServlet extends HttpServlet {
public static final Logger _log = Logger.getLogger(XMPPAgentServlet.class.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
try {
String strStatus = "";
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
//STEP 2
Message msg = xmpp.parseMessage(req);
JID fromJid = msg.getFromJid();
String body = msg.getBody();"Received a message from " + fromJid + " and body = " + body);
//STEP 3
String msgBody = "You sent me : " + body;
Message replyMessage = new MessageBuilder()
//STEP 4
boolean messageSent = false;
if (xmpp.getPresence(fromJid).isAvailable()) {
SendResponse status = xmpp.sendMessage(replyMessage);
messageSent = (status.getStatusMap().get(fromJid) == SendResponse.Status.SUCCESS);
//STEP 5
if (messageSent) {
strStatus = "Message has been sent successfully";
else {
strStatus = "Message could not be sent";
} catch (Exception e) {

The code should be familiar now but let us go through each of the steps above:

  1. We have written a Java Servlet that extends the HttpServlet class and implements the doPost(…) method since the XMPP message will be delivered via POST to the application.
  2. In this step, we extract out the XMPP Message object from the HTTP request. Then we invoke a couple of getter methods on the Message object to get the Jabber Id from where we got the message  and also the body of the message. We log this into the log file.
  3. We compose a string named msgBody that simply echoes back what was sent by the sender and then we compose a XMPP Message object named replyMessage to be sent to the Jabber Id that sent us this message i.e. fromJid along with the text that we want to send in the message i.e. msgBody
  4. We detect if the recipient that we are sending the response to i.e. fromJid is available i.e. online. If yes, we send the message using the sendMessage method. Then we determine if the message was sent successfully or not i.e. messageSent.
  5. Finally, depending on the message status, we log the appropriate message in the log file.

To complete our Servlet development, we will also need to add the <servlet/> and <servlet-mapping/> entry to the web.xml file. The necessary elements to be added to your web.xml file are shown below. Please note that you can use your own namespace and servlet class. Just modify it accordingly if you do so. But make sure that the fixed path URL i.e. /_ah/xmpp/message/chat/ is correctly mentioned in the <url-pattern/>, since that is the path over which the Google XMPP Infrastructure will deliver the message to you via a POST.



Finally, we have used the INFO level to log if the message was sent out successfully or not, so we will have the change the logging level by modified the file present in the war\WEB-INF folder. The necessary line after modification is shown below:

# Set the default logging level for all loggers to INFO
.level = INFO

Configure the XMPP Service for our Application

To enable your application to receive XMPP messages, you need to configure the XMPP service in your application. This is done by adding the XMPP service to the appconfig-web.xml file that is found in the war\WEB-INF folder of the Web Application project. Shown below is appconfig-web.xml and the relevant portion that you need to add to the file.

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="">

Deploy the Application

To deploy the application, you will need to first create your Application ID. The Application Identifier can be created by logging in at with your Google Account. You will see a list of application identifiers already registered under your account (or none if you are just getting started). To create a new Application, click on the Create Application button and provide the Application Identifier as requested. Please note this name down since you will be using it for deployment.

For e.g. I have registered an application identifier named gaejxmpptutorial.

To deploy the application, click on the Deploy Icon in the Toolbar. This will present the dialog as shown below:


You will need to provide your Email and Password. Do not click on Deploy button yet. Click on the App Engine Project settings link. This will lead you to the screen as shown below, where you will need to enter your Application ID [For e.g. shown below is my Application Identifier gaejxmpptutorial]


Click on OK. You will be lead back to the previous screen, where you can click on the Deploy button. This will start deploying your application to the GAEJ cloud. You should see several messages in the Console window as shown below as the application is being deployed:


You should see the message “Deployment completed successfully” as shown above. This means that you application is ready to serve.

See it in Action

Now that your Agent application has been deployed, it is time to communicate with it. To do that you will need to add your Agent as a friend/contact in your IM application. To do that you will need to know the ID of the Agent. This is made simple for you by GAEJ. If your Application ID is for e.g. myagent and your application is XMPP enabled, then the ID for your application will be In other words the ID is where APPLICATION_ID is the application identifier for your application that you registered.

In my case, it was gaejxmpptutorial, so all I did was start up Google Talk and clicked on Add Contact to add the Agent as shown below.


Once you click on Next and finish the invitation, you will receive a message in your Google Talk as shown below, where the Agent is requested to add you as friend. It is important that you accept that , so that it can send messages to you.


Once you do that, you will find that the Agent is available as shown below. The Agent is available because your Application is online and running in the cloud. Cool, isn’t it?


Now you can start the conversation and see your Agent in action as shown below:


Moving forward

You should now be in a position to think, code and deploy XMPP Agents of your choice using GAEJ. I would love to see you write a few and sharing it out here with all of us. You could write Agents that could do the following:

  • Play a word game
  • Provide weather, sports, traffic,etc  updates
  • Examination Results
  • Ticket Confirmation
  • Train, Air Tickets routes… almost anything that you can think of.

If you would like to get an idea of a sample XMPP Agent written using GAEJ, look no further than the Mumbai Blood Bank Locator Agent that I wrote a while back. Take a look and inspire all of us with your next cool Agent 007. Till the next episode …

Categories: Uncategorized Tags: