I've been wanting to setup an IRC bot for a while and finally got a chance last weekend. It was both free and easy to do using the Ruby Cinch bot framework, Heroku for hosting, and Freenode for the room. Here's how you can make your own little bot friend as well.
Before getting started, if you don't already have an IRC channel to use it on, follow the instructions in getting started with IRC to set one up. You can make a room for anything.
Creating the basic bot
We're going to use Github and Heroku so setup a Git repository and clone it locally.
Next we'll setup the bot. We will start with a very simple bot that just replies to '!hello' and then give resources on how to add more features after that.
The bot itself only needs a few files and thanks to Cinch they can all be in the root directory of the app.
# File: /Gemfile # ================= source 'https://rubygems.org' gem 'cinch' # There are also Cinch plugins that you can add here later
# File: /bot.rb # =================== # This is the actual bot config and command logic #! /usr/bin/env ruby require 'cinch' bot = Cinch::Bot.new do configure do |c| c.server = 'irc.freenode.net' c.user = 'BotName' c.nick = 'BotName' c.realname = 'My Bot' c.channels = '#your-channel-name' end on :message, "!hello" do |m| m.reply "Why hello there." end end bot.start
# File: /config.ru # ================== # This can literally be empty, but you need to have the file.
# File: /Procfile # ================= # Here we will specify commands for Heroku to start the bot. web: echo "foobar" > /dev/null bot: bundle exec ruby bot.rb
Deploy to Heroku
Next push it up to Heroku and start it up with these commands:
heroku create git push heroku master heroku scale web=0 heroku scale bot=1
Now if you type '!hello' into your IRC channel the bot should reply!
Creating commands for the bot
(aka, what can I do with this?)
Now that you have a working bot that accepts input and gives output back, you can have it do all kinds of things. It's great for work related things or just for having fun. It could make snarky replies to words said in the channel, play games, lookup information on the web, talk to APIs, whatever you want. We're in the internet of things now! Make it tell you when there's a new pot of coffee or when you're roommate is stealing your Ice Cream.
Here are a few resources to give you some ideas:
- Cinch API docs
- Examples on Cinch project
- Other Cinchbots on Github
- Quintus Cinch plugin collection
- A few plugins on Rubygems.org
- Github released their IRC bot Hubot for anyone to use. It's not written in Ruby but has a lot of parts plugins to give you ideas for what you'll want your bot to do. You can see a big list of Hubot plugins here. Everything from "Tell Hubot to send a user a message right now !" to "Fetches an image of the Mint Plaza Blue Bottle Line."
Lastly, this post is about the fun things you can do with good ol' IRC. But if you want to have fun with chat and want to play with something a little more current check out the Slack API. Slack is wonderful, the API has lots of great options.