Building an IRC Bot with Cinch
Tell Your API
At its core, the plugin communicates with the GitHub API. It retrieves the data via HTTP and translates JSON into Ruby structures. Since the standard libraries of Ruby provide everything you need, the implementation is easy.
Lines 7-10 in Listing 3 handle the configuration of the plugin. BaseURL
is the base address of the GitHub API and requires some adjustment for in-house installations. This entry is followed by the Username
and Password
for the bot on GitHub. The account must be authorized to access tickets in repositories, but it should not have more rights than necessary.
Listing 3
issues.rb
To simplify the interaction with the bot, the assumption is that all the repositories at GitHub belong to a single Organization
. The request()
method in line 13 is used by all of the plugin's functions to make requests to the API. request()
issues an HTTP request and converts the response, which is formatted in JSON, to a structure that Ruby understands. The method
argument is used to specify the HTTP command. GitHub uses, for example, GET
to retrieve data and PATCH
to edit data.
The plugin's functions are restricted to calling this method along with the right arguments. You then extend the GithubIssues
class with the methods from Listings 4 through 6 in the file issues.rb
. The first two functions, which open and close tickets, essentially consist of the call to the correct request()
method.
Listing 4
Supplement 1 for issues.rb
Listing 5
Supplement 2 for issues.rb
To search for tickets (Figure 3), the developer needs to do some more work. Although the actual search consists of one simple call to the request()
method, the code needs to present the search results appropriately. The program should not show all the results – there could be up to 100 – and it should show only the relevant information for each ticket, such as the ticket number, the title, the link to the ticket, and the status (open or closed).
The final feature (Listing 6) finds all references of the type Repository/gh-Ticket_number
in chat messages and outputs the title and status of the referenced tickets. Ruby's scan()
method finds all references to a message so that a single message can also cover several tickets (Figure 4). The special use_prefix: false
option ensures that the bot does not look for an exclamation mark at the start of the command.
Listing 6
Supplement 3 for issues.rb
Caveats
The plugin developed here makes it much easier for developers and admins to work through IRC chatrooms by performing targeted searches for information about certain tickets. However, many improvements are possible. For example, the plugin assumes that the repositories and tickets exist and that GitHub's API never has any problems. In short, the plugin does not provide error handling. Additionally, the number of requests to GitHub's API is limited to 5,000 per hour. Very active projects and large companies would have to cache requests or otherwise comply with the limits.
What the Bot Knows
Of course, logs of the conversations on IRC can be referenced in case of questions. It is also possible to collect FAQ knowledge on IRC and retrieve this information as needed. Bots can manage the FAQ. Inspired by Infobot [6] the electronic aids connect snippets of text – such as links or references – with retrievable keywords. If the developer saves these questions and answers in a compact SQLite database, other tools can access the data.
The implementation of the knowledgebase function is similar to that of the GitHub link. The task starts with establishing a database connection, and the actual functions follow. Install the database with:
gem install sqlite3
Listing 7 opens a connection to the SQLite database, which is located in the home directory of the user. The bot developer's plugin creates a table with three columns for the ID, the keyword, and the associated information, assuming this table does not yet exist. The plugin is then used without any manual intervention.
Listing 7
infobot.rb
Listing 8 implements two methods for creating and retrieving information. The first method, remember()
, is always used when somebody uses !rem keyword = information
to feed new snippets of information to the bot – or to overwrite existing information and thus correct an error.
Listing 8
Supplement for infobot.rb
The second method, lookup()
, activates the bot when it sees a message of the !keyword
type. If lookup()
recognizes the keyword
, it outputs its meaning; otherwise it remains silent. A typical interaction with the bot will look something like Listing 9, which demonstrates how IRC users store and retrieve information. Because the data also resides in a schematically simple SQLite database, access to knowledge is not limited to the bot. A console client could list the keywords, and data could be imported from existing sources, such as wikis. However, whole paragraphs tend to get in the way on IRC. Infobots are intended to output short sentences but not novels.
Listing 9
Knowledgebase on IRC
« Previous 1 2 3 Next »
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.
News
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.
-
Linux Foundation Adopts OMI to Foster Ethical LLMs
The Open Model Initiative hopes to create community LLMs that rival proprietary models but avoid restrictive licensing that limits usage.
-
Ubuntu 24.10 to Include the Latest Linux Kernel
Ubuntu users have grown accustomed to their favorite distribution shipping with a kernel that's not quite as up-to-date as other distros but that changes with 24.10.