This site runs best with JavaScript enabled.

Python IRC Bot Using Twisted (Rollbot)

Photo by Alex Knight on Unsplash

A simple IRC bot that will show the page titles of links posted

I wrote my first IRC bot recently. It turned out to be a simpler task than I thought it would be.

Here's the back story: At work, our programming team uses an IRC channel for all our internal communication throughout the day. What more would you expect from a bunch of programmers?

Recently I've been taking delight in Rebecca Rolling (worse than being Rick Roll'd) every each Friday. Someone made a comment that it would be nice to have a bot to detect such things. I had been looking for an excuse to try and write an IRC bot, so I took the challenge.

Basically, the majority of the script was created following Eric Florenzano's blog post, so I won't cover all that again. Just go check it out yourself.

Basically, the part I modified was the privmsg function:

1def privmsg(self, user, channel, msg):
2 # use regex to find posted URLs
3 matches = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', msg)
4 if matches:
5 for url in matches:
6 u = urllib.urlopen(url)
7 # get the mime type
8 urltype = u.headers.gettype()
9 #print urltype
10 try:
11 # use BeautifulSoup to get the page title
12 soup = BeautifulSoup.BeautifulSoup(u)
13 title = re.sub("\s+", ' ', soup.title.string).strip()
14 self.msg(, "Title: %s" % str(title))
15 except (AttributeError, HTMLParseError):
16 # if we have an error getting the title, show the mime type
17 self.msg(, \
18 "NO TITLE FOUND (%s)" % urltype)

Comments in the code above should explain what is going on. If you want the full source to the Rollbot, check it out on github.

Hackers: Note that it is entirely possible to hack this. Just create a basic html page, set your own title, and redirect with JavaScript. My co-workers still don't trust me...

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter

Dustin Davis