Build a “Management” Chat Bot in 5 steps with Google AppEngine

Google AppEngine provides very cool features. One of these features is the XMPP API, that allows you to write apps that can interact with users through XMPP clients, e.g. Jabber or GTalk.

Here’s a quick description how to create a chat bot which will help you to work for efficient. I think it has the potential to replace at least some managers. ;-)

1. Create a new web application project “de.vogella.gae.java.chatbot”. Rename the generated servlet “ChatbotServlet” and adjust web.xml
2. Open the servlet class and add a new attribute that provides access to the XMPP service

      private static final XMPPService xmppService =
           XMPPServiceFactory.getXMPPService();

3. Add a doPost method which will be called when a new message is received. In this method I will parse the incoming message and generate a reply, in our case a simple echo.

      public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws IOException {
      Message message = xmppService.parseMessage(request);
      Message reply = new MessageBuilder()
              .withRecipientJids(message.getFromJid())
              .withMessageType(MessageType.NORMAL)
              .withBody(message.getBody())
              .build();
      xmppService.sendMessage(reply);
      }

4. Now we need to enable the XMPP service for our app. Open the appengine-web.xml file in the war/WEB-INF folder and add the following tag:

<inbound-services>
      <service>xmpp_message</service>
</inbound-services>

5. As the final step we need to hook our servlet to a standard URL pattern that is used for the XMPP service. Open the web.xml file in the WEB-INF folder and change the existing servlet mapping:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
	<servlet>
		<servlet-name>chatbot</servlet-name>
		<servlet-class>de.vogella.gae.java.chatbot.ChatbotServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>chatbot</servlet-name>
    <url-pattern>/_ah/xmpp/message/chat/</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

That’s it! Deploy the application to the Google App Engine to be able to chat with it.

Every application has it’s own Jabber/GTalk ID: appid@appspot.com or anything@appid.appspot.com. Add that ID to your GTalk list and you will be able to interact with the application.

References: http://code.google.com/appengine/docs/java/xmpp and http://blog.appenginefan.com/2009/09/my-first-attempt-at-xmpp-in-java-app.html.

I would like to thank Ralf Güldemeister for providing most of the content of this blog entry.

To learn more about the Google App Engine please have a look at my Google App Engine Java Tutorial. You can also follow me on Twitter.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Web and tagged , . Bookmark the permalink.

One Response to Build a “Management” Chat Bot in 5 steps with Google AppEngine

  1. Luzifer1984 says:

    Very useful!

    Thx!!

Comments are closed.