Building an IRC Bot with Cinch
Bot Tech
![© Lead Image © Chris Modarelli, 123RF.com © Lead Image © Chris Modarelli, 123RF.com](/var/linux_magazin/storage/images/issues/2014/160/building-an-irc-bot/po-22900-123rf-chris_modarelli_123rf-roboter_mit_zettel_und_bleistift_resized.png/607192-1-eng-US/PO-22900-123RF-Chris_Modarelli_123RF-Roboter_mit_Zettel_und_Bleistift_resized.png_medium.png)
© Lead Image © Chris Modarelli, 123RF.com
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 |
|
Willie |
Python |
EFL |
|
PHP IRC Bot |
PHP |
CC-BY-3.0 |
|
Jsircbot |
JavaScript |
GPLv2 |
|
Java IRC Bot |
Java |
GPLv2 |
![](/var/linux_magazin/storage/images/issues/2014/160/building-an-irc-bot/figure-1/607195-1-eng-US/Figure-1_large.png)
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.
![](/var/linux_magazin/storage/images/issues/2014/160/building-an-irc-bot/figure-2/607198-1-eng-US/Figure-2_large.png)
Listing 1
hellobot.rb
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
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
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.