From zero to Docker: Your IRC bot with Node.js
Did you know that IRC, an application protocol created in 1988, is still quite alive and well? Despite not having the gazilion users it used to have, plenty of people still use it everyday - many comunities keep it as a central place to gather, and twitch.tv uses it to power all of its site’s chats.
An integral part of IRC chatting are the bots that do stuff for you so you don’t need to leave that awesome text-only interface. So why not set up your own, and mix the old-style IRC with the new-era Node.JS and Docker?
First things first: You’ll need Node.js installed and in your path. I’m also running nodeclipse to make everything easier.
Having done that, start your node project and open package.json
for editing. We’ll use node-irc (docs) and irc-colors, so set them up as dependencies:
And then run npm install
to download.
ProTip: installing nodeclipse lets you start a terminal window inside eclipse by pressing
Ctrl+Alt+T
We’ll be using the dockerfile/nodejs-runtime
image, and it assumes a server.js
file as entrypoint - create/rename your current file to match that. Using the library itself is quite simple if you are already familiar with asynchronous languages:
Just because you are running javascript does not mean you can be lazy about code organizing, so let’s create a madruga.js
file to do all the command parsing and stuff.
In this case we want all our commands to start with a .
; They can make the bot say a simple phrase or can contain parameters to run more complex methods.
That finishes the code, but you need one more step to link it with your docker image. Create a Dockerfile
text file on your root:
Yes, that simple - the parent image uses the ONBUILD
command, so everything else that needs to be done is already defined.
Commit it to your favorite git provider (I’ve uploaded MadrugaBot to BitBucket), because we’ll use the autobuilds offered by the official Docker Registry. After setting up your account and linking it to BitBucket/GitHub, go to your repositories and add a new Automated Build. Link it with your bot’s project, and wait - this may take a couple of minutes if their server is too busy.
Once complete, just ssh
into your server and things could not be simpler:
Every time you commit changes to your git repository, Docker’s registry will re-generate the image with your new code. To make sure you have the latest version before restarting your container, just use docker pull marcelofs/madrugabot
!