Created
March 7, 2012 08:36
-
-
Save anonymous/1991945 to your computer and use it in GitHub Desktop.
Java IRC Bot (PircBot + Jsoup)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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