Building Social IDC Bots with Node.js and MongoDB

Preview:

DESCRIPTION

Improve your team's productivity and encourage participation with social IRC bots! Find out how to create scoreboards, encourage positive communication by awarding points, and bring your bot to life using OpenShift, Red Hat's free auto-scaling Platform as a Service. We'll power our bot using an OpenShift "gear", then add a MongoDB for memory, and teach it to speak using a javascript-based IRC library for node.js.

Citation preview

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 1/72

SOCIAL IRC BOTS IN THE CLOUD

with

node.js

and

MongoDB

http://bit.ly/11Qft8j

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 2/72

presented by

/ ryan jarvinen @ryanj

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 3/72

Open Platform Evangelistat

Red Hat

ryanj@redhat.com

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 4/72

AGENDALearn some IRC basicsCreate a bot and teach it to speakLearn some MongoDB basicsBuild a social leaderboardGo live with a Mongo-powered IRC bot in ~30 mins

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 5/72

CODE DU JOURhttps://github.com/openshift-quickstart/irc-

leaderboard-quickstart

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 6/72

IRCis

just

multiplayer notepad

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 7/72

“Hello, nice to troll you!”

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 8/72

IRCis

just multiplayer notepada drama club for nerds

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 9/72

ACTING / EMOTING“ An emote is an entry into a text-based

chat client that indicates an action takingplace. Unlike emoticons, they are not textart, and instead describe the action using

words.”http://en.wikipedia.org/wiki/Emote

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 10/72

REAL DRAMAThe Hamnet Players

http://www.hambule.co.uk/hamnet/

The Plaintext Playershttp://yin.arts.uci.edu/~players/

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 11/72

THE CYPOSIUM?

http://www.cyposium.net/

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 12/72

MAKING A BIGENTRANCE

To perform an action,

type '/me' followed by a description of an action:

/me rolls eyes at IRC over-actors

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 13/72

IRC BOTS

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 14/72

IRC BOTS AREtalking applicationsscripted actors who appear to be another user in theroomgenerally benevolent (they fight for the users)

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 15/72

IRC BOT ABILITIESmost bots know a few simple jokesmany know how to assist with bug reportsthey may offer links to relevant informationor even fight against spam

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 16/72

IRC ISjust multiplayer notepaddrama club for nerds✓an open protocol for realtime mutiuser communicationvia text ( )✓found at the heart of every high-tech company that I'veworked for✓a serious contender for "most valuable communicationtool" in many organizations✓realtime communication✓

rfc1459

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 17/72

IRC IS

OPEN

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 18/72

OPEN CLIENTS

XCHAT, COLLOQUY

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 19/72

WEB CLIENTSGive it a try:

http://webchat.freenode.net/

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 20/72

OPEN SERVERS

FREENODEOpenShift on

/join #openshift

freenode IRC

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 21/72

HOSTING OPTIONS

DIY / ON PREMISEPUBLIC IRC(FREENODE)

CLOUD SERVERS

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 22/72

IRC is built on an open standard, allowing you to host it asa service (on your own hardware), or from within a

private network (for added security).

OpenShift was designed with many of the samecharacteristics in mind, minus the drama.

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 23/72

OPENSHIFT IS

OPEN

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 24/72

OPEN CLIENTINTERFACE

https://github.com/openshift/rhc

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 25/72

OPEN CARTRIDGEFORMAT

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 26/72

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 27/72

PAAS SOURCE

OPENSHIFT ORIGIN

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 28/72

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 29/72

OPENSHIFT HOSTING

DIY / ORIGINONLINE

ENTERPRISE

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 30/72

DEVELOPER PREVIEWSign up for an OpenShift Online account:

Free-as-in-beer (no cost)Free-as-in-freedom (open source)512 MB RAM / 1 GB storage per gear (3 free)Need more resources? (just ask!)REALLY a Developer Preview (GA this summer)

http://openshift.redhat.com/

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 31/72

THE CLOUD LANDSCAPE

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 32/72

OPENSHIFT IS...

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 33/72

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 34/72

THE BIG PICTURE

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 35/72

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 36/72

OPENSTACK

IS OPENand

FOCUSED ON IAAS

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 37/72

BUZZ WORDS

PUBLIC CLOUD +PRIVATE CLOUD =

HYBRID CLOUD

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 38/72

GETTING STARTED WITH OPENSHIFTPRE-REQUISITES:

ruby, rubygemsgit

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 39/72

INSTALLING THE 'RHC' CLIENT TOOLand

install

sudo gem install rhc

node.js

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 40/72

HELP WITH CLIENT TOOLSRHC client tool installation

Openshift Getting Started guide

https://openshift.redhat.com/community/get-started#cli

https://openshift.redhat.com/community/developers/get-started

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 41/72

CONFIGURING YOUR DEV ENVIRONMENTWill automatically:

set your openshift email addressverify your ssh key configurationconfigure your application namespace

rhc setup

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 42/72

GEARING UP A NEW APP ON OPENSHIFTor

rhc app create APP_NAME APP_CARTRIDGE

rhc app create ircbot nodejs-0.6

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 43/72

Application Options------------------- Namespace: ryanj Cartridges: nodejs-0.6 Gear Size: default Scaling: no

Creating application 'ircbot' ... done

Waiting for your DNS name to be available ... done

Downloading the application Git repository ...Cloning into 'ircbot'...

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 44/72

ircbot @ http://ircbot-ryanj.rhcloud.com/ (uuid: 25918304bf8d406e89802a1e642063a0)--------------------------------------------------- Created: 10:23 PM Gear Size: small Git URL: ssh://25918304bf8d406e89802a1e642063a0@ircbot-ryanj.rhcloud.com/~/git/ircbot.git/ SSH: 25918304bf8d406e89802a1e642063a0@ircbot-ryanj.rhcloud.com

nodejs-0.6 (Node.js 0.6) ------------------------

RESULT:Application ircbot was created.

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 45/72

SUCCESS!You now have a basic node.js app up an running on

OpenShift!

Your gear is now configured with:

it's own git repoit's own web serverssh accessloggingpublicly accessible DNS

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 46/72

BUILDING YOUR BOT cd ircbot

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 47/72

INSTALLING DEPENDENCIESUsing Node's :

Include the `-S` flag in order to save this dependency toyour application's `package.json` file.

npm package managernpm install irc -S

http://npmjs.org/package/irc/

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 48/72

BOT BASICSAdd the following to your server.js to load and configure

the irc library:var irc = require('irc');var bot_name = process.env.OPENSHIFT_APP_NAME || 'ircbot';var bot = new irc.Client('chat.freenode.net', bot_name, { channels: ['#botzoo', '#botwar'], port: 8001, debug: true});

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 49/72

TEACHING YOUR BOT TO SPEAKListen and respond to conversations as they happen:bot.addListener('message', function(from, to, message) { if( message.indexOf('Know any good jokes?') > -1 || message.indexOf('good joke') > -1 ) { bot.say(to, 'Knock knock!'); }});

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 50/72

Getting to the punchline:bot.addListener('message', function(from, to, message) { if( message.indexOf('who is there?') > -1 || message.indexOf("who's there?") > -1 || message.indexOf("Who's there?") > -1 || message.indexOf("Who is there?") > -1 ) { bot.say(to, 'Doris'); }});bot.addListener('message', function(from, to, message) { if( message.indexOf('Doris who?') > -1 || message.indexOf("doris who?") > -1 ) { bot.say(to, "Doris locked, that's why I'm knocking!"); }});

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 51/72

COMMITTING YOUR CHANGESgit add package.json server.jsCommit your changes locally

git commit -m 'adding irc npm dependency, initializing irclibrary, adding a few jokes'

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 52/72

DEPLOYMENTDone! Your bot should now be live on IRC.

git push

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 53/72

INTERACTING WITH YOUR NEW BOTLogin to IRC, and enter one of the channels that your bot

has been configured to join:

If you don't already have an IRC client, you can connect tofreenode on the web:

/join #botzoo

http://webchat.freenode.net/

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 54/72

Ask if anyone knows a good joke:[guest] Know any good jokes?[ircbot] Knock knock![guest] Who is there?[ircbot] Doris[guest] Doris who?[ircbot] Doris locked, that's why I'm knocking![guest] meh...

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 55/72

BOT DEBUGGING AND MAINTAINANCEIf your bot decides to quit mid-conversation, it may be

suffering from a bug.

Checking it's logs may reveal what set it off:

Or, debug it while running locally:

rhc tail ircbot

npm start

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 56/72

TEACHING YOUR BOT TOKEEP SCORE

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 57/72

SETTING UP MONGODB ON OPENSHIFTrhc cartridge add mongodb-2.2

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 58/72

Adding mongodb-2.2 to application 'ircbot' ... Success

mongodb-2.2 (MongoDB NoSQL Database 2.2)---------------------------------------- Connection URL: mongodb://127.6.102.129:27017/ Database Name: ircbot Password: T5xJwCiDZYGn Username: admin

RESULT:Added mongodb-2.2 to application ircbot

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 59/72

Adding mongodb-2.2 to application 'ircbot' ... Success

MongoDB 2.2 database added. Please make note of these credentials:

Root User: admin Root Password: T5xJwCiDZYGn Database Name: ircbot

Connection URL: mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/

You can manage your new MongoDB by also embedding rockmongo-1.1The rockmongo username and password will be the same as the MongoDB credentials above.

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 60/72

ROCKMONGO ON OPENSHIFT(optional)

rhc cartridge add rockmongo-1.1

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 61/72

ADDING A MONGOJS DEPENDENCYnpm install -S mongojs

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 62/72

CONNECTING TO MONGODBvar mongojs = require('mongojs');var connection_string = bot_name;if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){ connection_string = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" + process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" + process.env.OPENSHIFT_MONGODB_DB_HOST + '/' + process.env.OPENSHIFT_APP_NAME;}var db = mongojs(connection_string, ['scoreboard']);

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 63/72

SETTING A SCOREbot.addListener('message', function(from, to, message) { if( message.indexOf('++') > -1 ){ var subject = message.slice(0,message.indexOf('++')); db.scoreboard.find({query: {name: subject}}).limit(1, function(err, doc) { if( typeof(doc)=='object' && (doc instanceof Array) && doc[0] && doc[0].name){ // update db.scoreboard.update({name: subject}, {$inc:{score:1}}, function(err) { bot.say(to, "score: " + ( 1 + doc[0].score )); }); }else{ // insert db.scoreboard.insert({name: subject, score: 1}, function(err) { bot.say(to, "score: 1"); }); } }); }});

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 64/72

SHOWING THE SCOREBOARDbot.addListener('message', function(from, to, message) { if( message.indexOf('scoreboard') > -1 ){ db.scoreboard.find().sort({score:-1}).limit(10).forEach(function(err, doc){ if (doc && doc.name && doc.score ) { bot.say(to, doc.name + ": " + doc.score); } }); }});

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 65/72

UPDATING YOUR APPgit add package.json server.js

git commit -m 'adding a social scoreboard'

git push

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 66/72

SOCIAL REWARDS

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 67/72

ALLOCATING POINTSterm++

[raphael] Anyone down for pizza?[donatello] pizza++[ircbot] score: 43[raphael] Let's order two[leonardo] Good idea, Raph! raphael++[ircbot] score: 23

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 68/72

CHECKING THE SCOREBOARDircbot scoreboard

[ircbot] ircbot: 7[ircbot] ryanj: 4[ircbot] doris: -2

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 69/72

BOTS ON-DEMANDrhc app create ircbot nodejs-0.6 mongodb-2.2 --from-code=https://github.com/openshift-quickstart/irc-leaderboard-quickstart.git

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 70/72

You can find much more information about this npm IRClibrary here:

https://github.com/martynsmith/node-irc

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 71/72

Thanks for following along! --ryanj

3/11/13 reveal.js - The HTML Presentation Framework

ryanjarvinen.com/presentations/irc/#/ 72/72

REFERENCES

Building social IRC Bots with Node.js (Part 1)Leaderboards for IRC - Social Node.js Bots (Part 2)RHC command-line installer helpWhy contact Freenode on port 8001?IRC - It's how hackers talk. Luckily, I speak "leet"node-ircRun any version of Node.js on OpenShift

Recommended