11 min read

In this article by Eduardo Freitas, the author of the book Building Bots with Node.js, we will learn about Internet Relay Chat (IRC). It enables us to communicate in real time in the form of text. This chat runs on a TCP protocol in a client server model. IRC supports group messaging which is called as channels and also supports private message.

(For more resources related to this topic, see here.)

IRC is organized into many networks with different audiences. IRC being a client server, users need IRC clients to connect to IRC servers. IRC Client software comes as a packaged software as well as web based clients. Some browsers are also providing IRC clients as add-ons. Users can either install on their systems and then can be used to connect to IRC servers or networks. While connecting these IRC Servers, users will have to provide unique nick or nickname and choose existing channel for communication or users can start a new channel while connecting to these servers.

In this article, we are going to develop one of such IRC bots for bug tracking purpose. This bug tracking bot will provide information about bugs as well as details about a particular bug. All this will be done seamlessly within IRC channels itself. It’s going to be one window operations for a team when it comes to knowing about their bugs or defects.

Great!!

IRC Client and server

As mentioned in introduction, to initiate an IRC communication, we need an IRC Client and Server or a Network to which our client will be connected. We will be using freenode network for our client to connect to. Freenode is the largest free and open source software focused IRC network.

IRC Web-based Client

I will be using IRC web based client using URL(https://webchat.freenode.net/). After opening the URL, you will see the following screen,

Building Bots with Node.js

As mentioned earlier, while connecting, we need to provide Nickname: and Channels:.

I have provided Nickname: as Madan and at Channels: as #BugsChannel. In IRC, channels are always identified with #, so I provided # for my bugs channel. This is the new channel that we will be starting for communication. All the developers or team members can similarly provide their nicknames and this channel name to join for communication. Now let’s ensure Humanity: by selecting I’m not a robot and click button Connect.

Building Bots with Node.js

Once connected, you will see the following screen.

Building Bots with Node.js

With this, our IRC client is connected to freenode network. You can also see username on right hand side as @Madan within this #BugsChannel. Whoever is joining this channel using this channel name and a network, will be shown on right hand side.

In the next article, we will ask our bot to join this channel and the same network and will see how it appears within the channel.

IRC bots

IRC bot is a program which connects to IRC as one of the clients and appears as one of the users in IRC channels. These IRC bots are used for providing IRC Services or to host chat based custom implementations which will help teams for efficient collaboration.

Creating our first IRC bot using IRC and NodeJS

Let’s start by creating a folder in our local drive in order to store our bot program from the command prompt.

mkdir ircbot
cd ircbot

Assuming we have Node.js and NPM installed and let’s create and initialize our package.json, which will store our bot’s dependencies and definitions.

npm init

Once you go through the npm init options (which are very easy to follow), you’ll see something similar to this.

Building Bots with Node.js

On your project folder you’ll see the result which is your package.json file.

Building Bots with Node.js

Let’s install irc package from NPM. This can be located at https://www.npmjs.com/package/irc.

In order to install it, run this npm command.

npm install –-save irc

You should then see something similar to this.

Building Bots with Node.js

Having done this, the next thing to do is to update your package.json in order to include the “engines” attribute. Open with a text editor the package.json file and update it as follows.

"engines": {
    "node": ">=5.6.0"
}

Your package.json should then look like this.

Building Bots with Node.js

Let’s create our app.js file which will be the entry point to our bot as mentioned while setting up our node package.

Our app.js should like this.

var irc = require('irc');
var client = new irc.Client('irc.freenode.net', 'BugTrackerIRCBot', {
  autoConnect: false
});
client.connect(5, function(serverReply) {
  console.log("Connected!n", serverReply);
  client.join('#BugsChannel', function(input) {
    console.log("Joined #BugsChannel");
    client.say('#BugsChannel', "Hi, there. I am an IRC Bot which track bugs or defects for your team.n I can help you using following commands.n BUGREPORT n BUG # <BUG. NO>");
  });
});

Now let’s run our Node.js program and at first see how our console looks. If everything works well, our console should show our bot as connected to the required network and also joined a channel. Console can be seen as the following,

Building Bots with Node.js

Now if you look at our channel #BugsChannel in our web client, you should see our bot has joined and also sent a welcome message as well. Refer the following screen:

Building Bots with Node.js

If you look at the the preceding screen, our bot program got has executed successfully. Our bot BugTrackerIRCBot has joined the channel #BugsChannel and also bot sent an introduction message to all whoever is on channel. If you look at the right side of the screen under usernames, we are seeing BugTrackerIRCBot below @Madan

Code understanding of our basic bot

After seeing how our bot looks in IRC client, let’s look at basic code implementation from app.js.

We used irc library with the following lines,

var irc = require('irc');

Using irc library, we instantiated client to connect one of the IRC networks using the following code snippet,

var client = new irc.Client('irc.freenode.net', 'BugTrackerIRCBot', {
  autoConnect: false
});

Here we connected to network irc.freenode.net and provided a nickname as BugTrackerIRCBot. This name has been given as I would like my bot to track and report the bugs in future. Now we ask client to connect and join a specific channel using the following code snippet,

client.connect(5, function(serverReply) {
  console.log("Connected!n", serverReply);
  client.join('#BugsChannel', function(input) {
    console.log("Joined #BugsChannel");
    client.say('#BugsChannel', "Hi, there. I am an IRC Bot which track bugs or defects for your team.n I can help you using following commands.n BUGREPORT n BUG # <BUG. NO>");
  });
});

In preceeding code snippet, once client is connected, we get reply from server. This reply we are showing on a console. Once successfully connected, we ask bot to join a channel using the following code lines:

client.join('#BugsChannel', function(input) {

Remember, #BugsChannel is where we have joined from web client at the start. Now using client.join(), I am asking my bot to join the same channel. Once bot is joined, bot is saying a welcome message in the same channel using function client.say().

Hope this has given some basic understanding of our bot and it’s code implementations.

In the next article, we will enhance our bot so that our teams can have effective communication experience while chatting itself.

Enhancing our BugTrackerIRCBot

Having built a very basic IRC bot, let’s enhance our BugTrackerIRCBot.

As developers, we always would like to know how our programs or a system is functioning. To do this typically our testing teams carry out testing of a system or a program and log their bugs or defects into a bug tracking software or a system. We developers later can take a look at those bugs and address them as a part of our development life cycle. During this journey, developers will collaborate and communicate over messaging platforms like IRC. We would like to provide unique experience during their development by leveraging IRC bots.

So here is what exactly we are doing. We are creating a channel for communication all the team members will be joined and our bot will also be there. In this channel, bugs will be reported and communicated based on developers’ request. Also if developers need some additional information about a bug, chat bot can also help them by providing a URL from the bug tracking system.

Awesome!!

But before going in to details, let me summarize using the following steps about how we are going to do this,

  • Enhance our basic bot program for more conversational experience
  • Bug tracking system or bug storage where bugs will be stored and tracked for developers

Here we mentioned about bug storage system. In this article, I would like to explain DocumentDB which is a NoSQL JSON based cloud storage system.

What is DocumentDB?

I have already explained NoSQLs. DocumentDB is also one of such NoSQLs where data is stored in JSON documents and offered by Microsoft Azure platform.

Details of DocumentDB can be referred from (https://azure.microsoft.com/en-in/services/documentdb/)

Setting up a DocumentDB for our BugTrackerIRCBot

Assuming you already have a Microsoft Azure subscription follow these steps to configure DocumentDB for your bot.

Create account ID for DocumentDB

Let’s create a new account called botdb using the following screenshot from Azure portal. Select NoSQL API as of DocumentDB. Select appropriate subscription and resources. I am using existing resources for this account. You can also create a new dedicated resource for this account. Once you enter all the required information, hit Create button at the bottom to create new account for DocumentDB.

Building Bots with Node.js

Newly created account botdb can be seen as the following,

Building Bots with Node.js

Create collection and database

Select a botdb account from account lists shown precedingly. This will show various menu options like Properties, Settings, Collections etc.

Under this account we need to create a collection to store bugs data. To create a new collection, click on Add Collection option as shown in the following screenshot,

Building Bots with Node.js

On click of Add Collection option, following screen will be shown on right side of the screen. Please enter the details as shown in the following screenshot:

Building Bots with Node.js

In the preceding screen, we are creating a new database along with our new collection Bugs. This new database will be named as BugDB. Once this database is created, we can add other bugs related collections in future in the same database. This can be done in future using option Use existing from the preceding screen. Once you enter all the relevant data, click OK to create database as well as collection. Refer the following screenshot:

Building Bots with Node.js

From the preceding screen, COLLECTION ID and DATABASE shown will be used during enhancing our bot.

Create data for our BugTrackerIRCBot

Now we have BugsDB with Bugs collection which will hold all the data for bugs. Let’s add some data into our collection. To add a data let’s use menu option Document Explorer shown in the following screenshot:

Building Bots with Node.js

This will open up a screen showing list of Databases and Collections created so far. Select our database as BugDB and collection as Bugs from the available list. Refer the following screenshot:

Building Bots with Node.js

To create a JSON document for our Bugs collection, click on Create option. This will open up a New Document screen to enter JSON based data. Please enter a data as per the following screenshot:

Building Bots with Node.js

We will be storing id, status, title, description, priority,assignedto, url attributes for our single bug document which will get stored in Bugs collection. To save JOSN document in our collection click Save button. Refer the following screenshot:

Building Bots with Node.js

This way we can create sample records in bugs collection which will be later wired up in NodeJS program. Sample list of bugs can be seen in the following screenshot:

Building Bots with Node.js

Summary

Every development team needs bug tracking and reporting tools. There are typical needs of bug reporting and bug assignment. In case of critical projects these needs become also very critical for project timelines. This article showed us how we can provide a seamless experience to developers while they are communicating with peers within a channel.

To summarize so far, we understood how to use DocumentDB from Microsoft Azure. Using DocumentDB, we created a new collection along with new database to store bugs data. We also added some sample JSON documents in Bugs collection.

In today’s world of collaboration, development teams who would be using such integrations and automations would be efficient and effective while delivering their quality products.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here