Subscribe to News

How to make a XMPP Bot

Author : Equizz

From TechnologicalWiki

Jump to: navigation, search


Contents

[edit] Introduccion

This is a bot that can be configured for doing actions like response in Google Talk chat. They are used as dictionaries, or to get information about something... but the main thing is that they can read and write XMPP events, then we can make programs to manage those events.

XMPP (Extensible Messaging and Presence Protocol) is an open protocol that is inspired on XML. Built to be extensible, the protocol has accumulated features over time such as Voice over IP and file transfer signaling. Google Talk provides XMPP gateways to its service.

[edit] Installing Mobicents

This article is aimed to develop a bot using java. We will use mobicents (an open sorce JSLEE application server) and a resource adaptor for XMPP. Here you can find more info about how to start with JSLEE. Let's start downloadig mobicents, then we will install it and set the enviroment variable MOBICENTS_HOME.

[edit] Running JBoss

JBoss is the server that mobicents uses, so we have to download and install it to run that service. Don't forget to set the enviroment variable JBOSS_HOME.

c:\JBOSS_PATH\bin\run -mc

[edit] Deploying the RA

The xmppra is the resource adaptor of XMPP protocol, and it's included with mobicents. The first that you have to do is to get in the ra directory:

cd C:\MOBICENTS_PATH\ra\xmppra

Now, we have to compile and deploy the RA.

ant

[edit] Deploying the SBB

There is an example that cames with mobicents, to test the xmpp-ra. We are taking this as the base of our bot, so let's configure it:

  • You need to create a valid GMail account before.
  • Edit src\org\mobicents\examples\googletalk\GoogleTalkBot-sbb-jar.xml and enter the username and password of the gmail account. You can add this lines:
<env-entry>
    <env-entry-name>username</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>my.bot.account</env-entry-value>
</env-entry>

<env-entry>
    <env-entry-name>password</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>mypassword</env-entry-value>
</env-entry>
  • Then add this account to your regular GTalk buddy list.
  • Now we have to compile and deploy c:\mobicents\mobicents-examples\googletalk-bot typing ant at this directory.

[edit] Modifying the logic

Ensure this example works nice, it sould response with a world count when you write some thing for it in the gtalk chat. Now it's time for change this logic with our own one in GoogleTalkBotSbb.java.

OnPresence is the method that triggers when you start the session. This code sends a hello message to all your friends connected.

public void onPresence(org.jivesoftware.smack.packet.Presence packet,
		       ActivityContextInterface aci) {
    // check if this event is "to" me
    if (StringUtils.parseBareAddress(packet.getTo()).equals(username + "@" + serviceName)) {
        logger.info("!!!! XMPP Presence event type " + packet.getType()
		  + ". Sent by " + packet.getFrom() + " !!!!");
	// reply hello msg if receives notification of available presence state
	if (packet.getType() == Presence.Type.AVAILABLE) {
	    Message msg = new Message(packet.getFrom(), Message.Type.CHAT);
	    msg.setBody("Hello!");
	    xmppSbbInterface.sendPacket(connectionID, msg);
	} else if (packet.getType() == Presence.Type.SUBSCRIBE){
	    Presence msg = new Presence(Presence.Type.SUBSCRIBED);
            xmppSbbInterface.sendPacket(connectionID, msg);
        }
    }
}

OnMessage is the method that triggers when the bot receives a message form some friend. Here I made a code to return the character count of the message and to return the current time if the user types "time". This is an example, so you can add here your code for the bot to respond the messages automatically.

public void onMessage(org.jivesoftware.smack.packet.Message message,
	              ActivityContextInterface aci) {
    try {
	Order order = OrderParser.parseMessage("<feed>hola");
    } catch(OrderException oe) {}

    // check if this event is "to" me and it's not an error
    if (StringUtils.parseBareAddress(message.getTo()).equals(username + "@" + serviceName)
        && message.getType().equals(Message.Type.ERROR)) {
	logger.info("!!!! XMPP Message event type !!!!");
	logger.info("XMPP Message body: " + message.getBody());
	String body = null;
	if (message.getBody() != null) {
	    if (message.getBody().toLowerCase().startsWith("time")) {
		Date d = new Date();
		body = "My system time is " + d.toString();
	    } else {
		body = "You typed " + message.getBody().length() + " characters";
	    }
	}
	Message msg = new Message(message.getFrom(), message.getType());
	msg.setBody(body);
	xmppSbbInterface.sendPacket(connectionID, msg);
    }
}
Main Collaborators