In this hack, we use the JMegaHal package to learn sentences and
generate replies. We will then combine it with the PircBot package
to create an IRC bot
that can learn from other IRC users and hold wacky conversations with
them.
A simple example
The JMegaHal package is supplied in a JAR
file, so it is easy to integrate into any Java program, including an
IRC bot. Make sure you import
org.jibble.jmegahal.*; in any classes that will
use it.
JMegaHal hal = new JMegaHal( );
// Teach it some sentences.
hal.add("Hello, my name is Paul.");
hal.add("This is a sentence.");
// Note that the more sentences you add, the more sense it will make...
// Make JMegaHal generate a sentence.
String sentence1 = hal.getSentence( );
// Get another sentence, with the word "jibble" in it (if possible).
String sentence2 = hal.getSentence("jibble");
Notice the getSentence method can take a String as
an optional argument. If you want to generate random sentences all
about pie, then you might want to call
hal.getSentence("pie"). This ensures that all
generated sentences include the word
"pie" somewhere. If JMegaHal has
not yet learned anything about pie, it will choose some other random
token.
One thing to consider with a chat bot is telling it when to respond.
If you make it respond to every message, the other users in the
channel will very quickly get fed up with it and kick it out. One
safe option is to make it respond only to messages that contain its
nickname but to learn from all other messages.
Combining JMegaHal and PircBot
Combining JMegaHal and PircBot is very easy. If
anybody utters the bot's nickname, it should respond
with a randomly generated sentence from the JMegaHal object. All
other messages that do not contain the bot's
nickname will be used to teach it more sentences. In the following
simple implementation, the bot is seeded with a starting sentence in
the constructor, so it will always be able to respond with this if it
has not learned anything else.
Create a file called
JMegaHalBot.java:
import org.jibble.pircbot.*;
import org.jibble.jmegahal.*;
public class JMegaHalBot extends PircBot {
private String name;
private JMegaHal hal;
public JMegaHalBot(String name) {
setName(name);
hal = new JMegaHal( );
// Add at least one sentence so the bot can always form a reply.
hal.add("What is the weather like today?");
}
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
if (message.toLowerCase( ).indexOf(getNick( ).toLowerCase( )) >= 0) {
// If the bot's nickname was mentioned, generate a random reply.
String sentence = hal.getSentence( );
sendMessage(channel, sentence);
}
else {
// Otherwise, make the bot learn the message.
hal.add(message);
}
}
}
Now you just need to create a main method to construct the bot and
tell it what nickname to use. The main method will also tell the bot
which server to use and which channel to join.
This bot can run in multiple channels, but bear in mind that
sentences learned in one channel could end up appearing in other
channels.
Create the fileJMegaHalBotMain.java:
public class JMegaHalBotMain {
public static void main(String[] args) throws Exception {
JMegaHalBot bot = new JMegaHalBot("Chatty");
bot.setVerbose(true);
bot.connect("irc.freenode.net");
bot.joinChannel("#irchacks");
}
}