Creating a Twitter IRC bot in Perl

Twitter is an amazing social media platform, and while IRC is a little old it’s still a great place to chat and meet new people with the same interests as you. Today I want to give Twitter & IRC both some love by combining the two into a Twitter IRC bot that will post Twitter feeds right into an IRC channel. The bot will use a JSON configuration file for easy configuration without modifying any of the bot’s source code.
If you are just looking for the bot’s source code, skip to the end of the article where I have posted the full source and configuration file.

For Twitter I decided to use the Perl library AnyEvent::Twitter::Stream. This library works, and seems to be maintained. Unfortunately not every feature of this library is well documented, so I’m just going to be using the streaming API.

First of all, just like any Perl script, you will need to include your libraries

I’m using strict & warnings, as everyone should. I am also using threads. The threads are needed to make the bot read from Twitter at the same time that it’s connected to IRC, so that neither process is blocked by the other. I also use JSON libraries and Path::Tiny for reading, and parsing, the configuration file. This part is actually quite simple as only 1 line of code is actually used to read the config.

In my main loop I have decided to create my IRC connection, and call my Twitter thread. Yes I use a main loop in Perl just because I’m so used to C#. 😛

The first thing I did here was define the accounts to follow, and channels to join/post in. By separating the key value pairs I can use both pieces of information to tell the bot what to do and where to go.
After that the connection information is printed out for debug purposes (you can remove that if you would like) and then the connection to IRC is made by using IO::Socket::INET.

After the IRC connection is made I start my Twitter thread, and then start reading from IRC once that is in the background.

Reading from IRC buffer is pretty simple, there are 2 examples included in the following snippet. The first thing the bot will parse from IRC is PING. When the client receives a PING it must reply with a PONG or else the server will assume the client connection has gone stale. The second thing the bot will parse is numeric 376, which is the end of the MOTD. This part signals to the bot that it is connected and ready to join channels.

When the bot disconnects from IRC it will wait for the Twitter thread to properly finish before exiting.

The Twitter loop is responsible for reading data from Twitter, and printing it out to the appropriate IRC channel. The subroutine also has a signal handler for TERM to make sure that the thread exits when the bot is shutdown.

The Twitter thread, upon starting, will take the user IDs from the configuration and start following them. Whenever one of these users Tweet, the method tweet2irc is called.

The Twitter thread feeds the Tweet info over to the tweet2irc() subroutine, and this subroutine is responsible for posting to IRC (as the name suggests).

The full code and configuration file is included below. If you build anything neat with this please leave a comment about it so that I can check it out. 🙂


I’m a geek, gamer, and writer.
I am also an apache attack helicopter. Hobbies include flying around, shooting things down, and occasionally breaking stuff.