Transcript
Page 1: Building Social IDC Bots with Node.js and MongoDB

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

Page 2: Building Social IDC Bots with Node.js and MongoDB

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

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

presented by

/ ryan jarvinen @ryanj

Page 3: Building Social IDC Bots with Node.js and MongoDB

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

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

Open Platform Evangelistat

Red Hat

[email protected]

Page 4: Building Social IDC Bots with Node.js and MongoDB

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

Page 5: Building Social IDC Bots with Node.js and MongoDB

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

Page 6: Building Social IDC Bots with Node.js and MongoDB

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

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

IRCis

just

multiplayer notepad

Page 7: Building Social IDC Bots with Node.js and MongoDB

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

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

“Hello, nice to troll you!”

Page 8: Building Social IDC Bots with Node.js and MongoDB

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

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

IRCis

just multiplayer notepada drama club for nerds

Page 9: Building Social IDC Bots with Node.js and MongoDB

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

Page 10: Building Social IDC Bots with Node.js and MongoDB

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/

Page 11: Building Social IDC Bots with Node.js and MongoDB

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

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

THE CYPOSIUM?

http://www.cyposium.net/

Page 12: Building Social IDC Bots with Node.js and MongoDB

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

Page 13: Building Social IDC Bots with Node.js and MongoDB

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

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

IRC BOTS

Page 14: Building Social IDC Bots with Node.js and MongoDB

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)

Page 15: Building Social IDC Bots with Node.js and MongoDB

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

Page 16: Building Social IDC Bots with Node.js and MongoDB

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

Page 17: Building Social IDC Bots with Node.js and MongoDB

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

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

IRC IS

OPEN

Page 18: Building Social IDC Bots with Node.js and MongoDB

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

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

OPEN CLIENTS

XCHAT, COLLOQUY

Page 19: Building Social IDC Bots with Node.js and MongoDB

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

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

WEB CLIENTSGive it a try:

http://webchat.freenode.net/

Page 20: Building Social IDC Bots with Node.js and MongoDB

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

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

OPEN SERVERS

FREENODEOpenShift on

/join #openshift

freenode IRC

Page 21: Building Social IDC Bots with Node.js and MongoDB

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

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

HOSTING OPTIONS

DIY / ON PREMISEPUBLIC IRC(FREENODE)

CLOUD SERVERS

Page 22: Building Social IDC Bots with Node.js and MongoDB

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.

Page 23: Building Social IDC Bots with Node.js and MongoDB

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

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

OPENSHIFT IS

OPEN

Page 24: Building Social IDC Bots with Node.js and MongoDB

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

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

OPEN CLIENTINTERFACE

https://github.com/openshift/rhc

Page 25: Building Social IDC Bots with Node.js and MongoDB

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

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

OPEN CARTRIDGEFORMAT

Page 26: Building Social IDC Bots with Node.js and MongoDB

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

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

Page 27: Building Social IDC Bots with Node.js and MongoDB

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

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

PAAS SOURCE

OPENSHIFT ORIGIN

Page 28: Building Social IDC Bots with Node.js and MongoDB

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

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

Page 29: Building Social IDC Bots with Node.js and MongoDB

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

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

OPENSHIFT HOSTING

DIY / ORIGINONLINE

ENTERPRISE

Page 30: Building Social IDC Bots with Node.js and MongoDB

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/

Page 31: Building Social IDC Bots with Node.js and MongoDB

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

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

THE CLOUD LANDSCAPE

Page 32: Building Social IDC Bots with Node.js and MongoDB

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

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

OPENSHIFT IS...

Page 33: Building Social IDC Bots with Node.js and MongoDB

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

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

Page 34: Building Social IDC Bots with Node.js and MongoDB

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

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

THE BIG PICTURE

Page 35: Building Social IDC Bots with Node.js and MongoDB

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

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

Page 36: Building Social IDC Bots with Node.js and MongoDB

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

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

OPENSTACK

IS OPENand

FOCUSED ON IAAS

Page 37: Building Social IDC Bots with Node.js and MongoDB

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

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

BUZZ WORDS

PUBLIC CLOUD +PRIVATE CLOUD =

HYBRID CLOUD

Page 38: Building Social IDC Bots with Node.js and MongoDB

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

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

GETTING STARTED WITH OPENSHIFTPRE-REQUISITES:

ruby, rubygemsgit

Page 39: Building Social IDC Bots with Node.js and MongoDB

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

Page 40: Building Social IDC Bots with Node.js and MongoDB

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

Page 41: Building Social IDC Bots with Node.js and MongoDB

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

Page 42: Building Social IDC Bots with Node.js and MongoDB

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

Page 43: Building Social IDC Bots with Node.js and MongoDB

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'...

Page 44: Building Social IDC Bots with Node.js and MongoDB

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://[email protected]/~/git/ircbot.git/ SSH: [email protected]

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

RESULT:Application ircbot was created.

Page 45: Building Social IDC Bots with Node.js and MongoDB

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

Page 46: Building Social IDC Bots with Node.js and MongoDB

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

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

BUILDING YOUR BOT cd ircbot

Page 47: Building Social IDC Bots with Node.js and MongoDB

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/

Page 48: Building Social IDC Bots with Node.js and MongoDB

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});

Page 49: Building Social IDC Bots with Node.js and MongoDB

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!'); }});

Page 50: Building Social IDC Bots with Node.js and MongoDB

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!"); }});

Page 51: Building Social IDC Bots with Node.js and MongoDB

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'

Page 52: Building Social IDC Bots with Node.js and MongoDB

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

Page 53: Building Social IDC Bots with Node.js and MongoDB

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/

Page 54: Building Social IDC Bots with Node.js and MongoDB

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...

Page 55: Building Social IDC Bots with Node.js and MongoDB

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

Page 56: Building Social IDC Bots with Node.js and MongoDB

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

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

TEACHING YOUR BOT TOKEEP SCORE

Page 57: Building Social IDC Bots with Node.js and MongoDB

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

Page 58: Building Social IDC Bots with Node.js and MongoDB

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

Page 59: Building Social IDC Bots with Node.js and MongoDB

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.

Page 60: Building Social IDC Bots with Node.js and MongoDB

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

Page 61: Building Social IDC Bots with Node.js and MongoDB

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

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

ADDING A MONGOJS DEPENDENCYnpm install -S mongojs

Page 62: Building Social IDC Bots with Node.js and MongoDB

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']);

Page 63: Building Social IDC Bots with Node.js and MongoDB

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"); }); } }); }});

Page 64: Building Social IDC Bots with Node.js and MongoDB

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); } }); }});

Page 65: Building Social IDC Bots with Node.js and MongoDB

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

Page 66: Building Social IDC Bots with Node.js and MongoDB

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

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

SOCIAL REWARDS

Page 67: Building Social IDC Bots with Node.js and MongoDB

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

Page 68: Building Social IDC Bots with Node.js and MongoDB

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

Page 69: Building Social IDC Bots with Node.js and MongoDB

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

Page 70: Building Social IDC Bots with Node.js and MongoDB

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

Page 71: Building Social IDC Bots with Node.js and MongoDB

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

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

Thanks for following along! --ryanj

Page 72: Building Social IDC Bots with Node.js and MongoDB

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


Top Related