Contract Work

Monday, December 9, 2013

Building a Bot

I recently mentioned in my mentorship goals that one of the things I wanted to quickly dive into were databases. There are a variety of database types and ways to work with them. I was able to get an introductory handle by looking over Learn SQL the Hard Way and doing some additional research but databases are difficult to understand until you really start doing work with them.

At the same time, the bot in an IRC channel I often use was acting up. Basically, the bot was/is pretty much broken and needs to be restarted but no one knows who is actually able to restart the bot. Chris and I decided that a great first project for us to work on via the mentorship program would be to build a bot and to add a database component to it.

So first, what is a bot? A bot is a program that you can connect into a chat room (in this case an IRC channel) which will then respond to different conversations or things happening in the chat room. Most workplaces I’ve spoke to that utilize hipchat, campfire, or other internal chat communication tools have some sort of bot that hangs out in the room as well and makes it more entertaining. It’ll pop up funny gifs or respond to things people say. It can also be useful by providing information about testing or alert the room when someone deploys code.

My bot’s name is Rosie. She’s awesome. I’ll write up a few posts in the future that describe more about what she does but for now, I just want to go over the setup. First, I used cinch. Cinch is a great, easy way to set up a bot and gives pretty straightforward instructions that get you moving quickly.

This is what the setup code looks like.

bot = Cinch::Bot.new do 
  configure do |c| 
    c.server = 'irc.freenode.net' 
    c.realname = 'Rosie' 
    
      c.channels = ['#rosie'] 
      c.user = 'rosie_' 
      c.nick = c.user 
  end

Now, to walk through each line.
The first line bot = Cinch::Bot.new do creates a new bot using cinch. This new method takes a block.
In the next line configure do |c| , we are in the block configuring the method. This method also takes a block and passes in the cinch configuration for the bot which means it passes in a reference to itself that you can configure.
The third line c.server = 'irc.freenode.net' shows the server we are connecting to.
Then c.realname = 'Rosie' is the real name that will show up when someone asks who is in the IRC channel.
Next, c.channels = ['#rosie'] is the channel that the bot connects to. Right now I’m just testing her out, so I have her connecting to an independent channel that no one is using.
Coming to the end, the 2nd to last line c.user = 'rosie_' gives the nick name of the bot that will show up in the channel.
And finally, c.nick = c.user sets the nickname to the same thing as the username.

So, there’s the initial setup. I’ll do future posts on both functionality and integration of a database, as well as one on what is IRC in general for those not familiar with it.

1 comment: