Building an IRC Bot with Cinch

Bot Tech

© Lead Image © Chris Modarelli, 123RF.com

© Lead Image © Chris Modarelli, 123RF.com

Article from Issue 160/2014
Author(s):

Chat rooms aren't just for people. We'll show you how to access an IRC channel using an automated bot.

IRC, the Internet Relay Chat has existed for 20 years, and it is still a popular communication channel in open source projects and businesses. For almost as long, automated bots have listened on IRC channels and responded to user commands. The article shows how IRC bots can make themselves useful by helping you manage tickets and prepare documentation.

Spoiled for Choice

To develop an IRC bot today, you no longer need to learn Tcl to activate the bot ancestor Eggdrop [1]. Frameworks for bots are available in virtually any language (Table 1). Although I focus here on Cinch [2], a framework written in Ruby under the MIT license (Figure 1), you can easily adapt the code examples presented in this article to other languages.

Table 1

A Selection of IRC Bots

Name of Bot

Programming Language

License

Website

Autumn

Ruby

Freeware

https://github.com/RISCfuture/autumn

Willie

Python

EFL

http://willie.dftba.net

PHP IRC Bot

PHP

CC-BY-3.0

http://wildphp.com

Jsircbot

JavaScript

GPLv2

http://code.google.com/p/jsircbot/

Java IRC Bot

Java

GPLv2

http://sourceforge.net/projects/jircb/

Figure 1: The simplified structure of the Cinch Ruby bot.

Cinch comes with an object-oriented API and a modular plugin system. Thanks to many unrelated plugins, a single bot can perform all kinds of tasks. To an IRC server, Cinch – as is typical of IRC bots – appears as a normal client. Therefore, it does not matter which system the bot runs on, whether on the same system as the IRC server or remotely on a developer machine. Nor does it matter which server software you use, as long as the server is a compliant implementation of IRC.

Bot on Board

Assuming you have a Ruby installation in place, you can install Cinch as follows:

gem install cinch

A simple bot named hellobot.rb (Figure 2) that responds to salutations in the form of !hello is presented in Listing 1.

Figure 2: A perpetual responder in its simplest form.

Listing 1

hellobot.rb

01 # -*- coding: utf-8 -*-
02 require "cinch"
03
04 class Greeter
05     include Cinch::Plugin
06
07     match /hello$/, method: :greet
08     def greet(m)
09     m.reply "Hi there"
10     end
11 end
12
13 bot = Cinch::Bot.new do
14     configure do |c|
15         c.nick = "<OurBot>"
16         c.server = "<IRC_server_address>"
17         c.channels = ["<#a_channel>", "<#another_channel>"]
18         c.plugins.plugins = [Greeter]
19     end
20 end
21
22 bot.start
23

Typing ruby hellobot.rb brings the bot to life; it then connects with the IRC server <IRC_server_address> and joins the channels <#a_channel> and <#another_channel>. If an IRC user types !hello in one of these channels, the bot responds with a rather indifferent Hi there.

The sample code consists of two parts: The main part is the Greeter class (lines 4-11). Each class represents a single plugin that responds to one or more commands from the user; a command must begin with an exclamation mark. To match the commands, bot developers rely on regular expressions. For example, /(\d+)/ is a single argument comprising one or multiple numbers.

The second part (lines 13-20) takes care of configuring the bot. The program receives a name and an address at which it can log in. Line 18 also explains what plugins the bot should use. The configuration of the file does not change in the remainder of the article, so I can now move on to the actual plugin classes. Developers can easily extend the array of plugins; for more information on the API, check out the documentation [3].

GitHub Connection

Developers spend a huge amount of time processing tickets; little wonder their conversations often revolve around this topic. This article will show you how to build a bot that will control the GitHub ticket system. A plugin will support opening, closing, and finding tickets. At the same time, I want the bot to respond to references of the type repository/gh-ticket_number in messages by displaying the title status of the ticket in the channel.

GitHub's API [4] is based on HTTP and JSON and allows both read and write access to large parts of GitHub – including tickets. You can experiment with access using curl at the command line; Listing 2, for example, requests ticket number 13069 from the Rails project.

Listing 2

Ticket Request with curl

01 $ curl https://api.github.com/repos/rails/rails/issues/13069
02 {
03     "title": "Requires JSON gem version 1.7.7 or above as it contains an important security fix.",
04     "user": {
05         "login": "chancancode",
06         <[...]>
07     },
08     "labels": [],
09     "state": "closed",
10     "created_at": "2013-11-27T06:20:30Z",
11     "updated_at": "2013-11-27T10:07:54Z",
12     "closed_at": "2013-11-27T10:07:54Z",
13     "body": "See [here](https://github.com/flori/json/blob/master/CHANGES).",
14     [...]
15 }

This (abridged) example shows how GitHub structures the information the bot requires to display tickets. The developer can access both public information (such as tickets from open source projects) as well as private tickets [5]. In the second case, however, you do need to authenticate. The API allows authentication either via O-Auth or classic HTTP, which is usually sufficient for an IRC bot. For in-house installations of GitHub, access to the API looks like this: http://<IP_address>/api/v3/.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: CMS with GitHub

    With its easy-to-use web interface, GitHub can be put to totally different uses than archiving code. For example, Perlmeister Mike Schilli used GitHub to deploy a content management system for simple websites.

  • Perl: Travis CI

    A new service on travis-ci.org picks up GitHub projects, runs new code through test suites, and notifies the owners if the build fails. Its API enables Perl scripts to gather historical build data, including who-broke-the-build tabulations.

  • Trouble Ticket Software

    If your help line serves outside users, keeping track of support requests can mean the difference between a repeat customer and a lost customer. If the line serves inside employees, an efficient response means better productivity. Fortunately,several Linux-based applications offer help for your help desk or hotline.

  • Perl: Jenkins CI Server

    Instead of configuring the Jenkins continuous integration server in the browser with mouse clicks and text input for builds, programmers can store the necessary data in the source control system and let a Perl script do the work.

  • Linux with Active Directory

    Microsoft's Active Directory system provides centralized user management and single sign-on. If you're ready for a few manual steps, Linux can leverage this potential.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News

njobs Europe
What:
Where:
Country:
Njobs Netherlands Njobs Deutschland Njobs United Kingdom Njobs Italia Njobs France Njobs Espana Njobs Poland
Njobs Austria Njobs Denmark Njobs Belgium Njobs Czech Republic Njobs Mexico Njobs India Njobs Colombia