Over a million developers have joined DZone.

IRC Notification Bot with Node.js

· Web Dev Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

This bot is a product of a simple need: notifying people when their friends log in to a particular channel:

The need :

  • Simple IRC bot, that would notify its master.

  • Easy to configure.

  • Easy maintainability.

The answer was to do it with node.js!

To get up and running :

  • Indeed you must have node.

  • npm install irc

  • Check out the code from the repo and edit config.js as per need.

  • Finally start the bot node notify.js and the pm notifications ;)

The code :

The config file : [ Power of JS objects FTW! ]

var config = {};
 
config.chans   = "#yeoman, #html5"; 
config.server  = "irc.freenode.net";
config.botName = "notify-bot";
config.friends = "firend1, firend2, firends3";
config.master  = "hemanth";
module.exports = config;

The bot:

#!/usr/bin/env node
var irc = require('irc');
var config = require('./config');
 
var chans   = config.chans.split("/, */");
var server  = config.server;
var botName = config.botName;
var friends = config.friends.split(/, */);
var master  = config.master;
 
var bot = new irc.Client(server,botName, {
  channels: chans
});
 
bot.addListener('join', function (channel, who) {
  console.log(who);
  console.log(friends.indexOf(who));
  console.log(friends);
  if(friends.indexOf(who) >=0){
    bot.say(master,who + " joined "+channel); 
  } 
});

Hope this helps, as it's helping me. Your suggestions are welcome -- do check out l33ty.

 

The Web Dev Zone is brought to you by Stormpath—offering a complete, pre-built User Management API for building web and mobile applications, and APIs. Download our new whitepaper: "Build Versus Buy: Customer Identity Management for Web and Mobile Applications".

Topics:

Published at DZone with permission of Hemanth HM , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}