Writing Javabot Operations

What is an Operation

Simply put, operations are the plugin system for javabot. The bot comes with MANY operations already written, right out of the box. You can always add in your own, this is a tutorial to tell you just how to do that.

What is a BotEvent?

A BotEvent contains important data needed for the bot. It is vital when it comes to writing an operation for javabot.

  • The channel it was sent from.
  • The nick of the sender.
  • The login of the sender. (also known as the username)
  • The hostname of the sender.
  • Lastly but most important message.

There are getters/setters for each of these; but the bot handles setting the data, you just have to retrieve it. Each of these are returned as a String.

Example operation

package javabot.operations;

import javabot.Message;
import javabot.BotEvent;

import java.util.List;
import java.util.ArrayList;

public class MyFirstOperation implements BotOperation {

    public List<Message> handleMessage(BotEvent event) {
        List<Message> messages = new ArrayList<Message>();
        String channel = event.getChannel();
        String message = event.getMessage();
        if("foo".startsWith(message)) {
            messages.add(new Message(channel,event.toString(),false));
            return messages;
        }
        return messages;
    }

    public List<Message> handleChannelMessage(BotEvent event) {
        return new ArrayList<Message>();
    }

}

What it does

What that does is simply call the toString() method in BotEvent, which is where all the information about where the message originated from, and the data about the sender. Such as hostname, login (ident), channel, the nick of the nick of the sender, and the message itself. It's pretty straight forward.

How it's triggered

When you type foo with the response prefix(es) you selected in your configuration, a Linked List of all the operations that are instantiated at runtime, is traversed, it calls handleMessage() in each of the operations in the list. If it does not find the message starts with the appropriate textual prefix, then it returns an empty ArrayList and moves on. That ArrayList contains the response that will be displayed in the channel. However, if the textual prefix of your message matches, it executes the handleMessage() code and adds the results, whatever they may be, to the ArrayList and it is displayed in the channel.

Additionally, there is one other method handleChannelMessage() which will be executed regardless of whether the bot was addressed or not, so if I moved my code to the handleChannelMessage(), it would only execute if the textual prefix was matched in handleChannelMessage() without being specifically addressed. As you can see, the example operation will simply ignore you if you don't address it using the response prefix, it is also proper that a bot only speak when it is spoken to.

Questions

If you have questions about anything, you can feel free to ask me. I'm on ##java regularly under the /nick r0bby. I will be more than happy to answer them.