Skip to content

Instantly share code, notes, and snippets.

Created March 7, 2012 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1991945 to your computer and use it in GitHub Desktop.
Save anonymous/1991945 to your computer and use it in GitHub Desktop.
Java IRC Bot (PircBot + Jsoup)
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.IrcException;
import org.jsoup.Jsoup;
public class InfoBot extends PircBot {
protected final Pattern urlPattern;
public InfoBot() throws IOException, IrcException {
this.urlPattern = Pattern.compile("(https?://[^\\s]+)");
this.setName("TestBot");
this.setVerbose(true);
this.connect("irc.efnet.nl");
this.joinChannel("#test");
}
public void onMessage(final String channel, final String sender, final String login, final String hostname, final String message) {
final Matcher matcher = this.urlPattern.matcher(message);
while (matcher.find()) {
final String url = matcher.group();
try {
final String title = Jsoup.connect(url).get().select("title").first().text();
this.sendMessage(channel, title);
} catch (final IOException e) {
System.out.println("Could not fetch URL: " + url);
}
}
}
public static void main(final String[] args) throws IOException, IrcException {
final InfoBot infoBot = new InfoBot();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment