with
node.js
and
MongoDB
https://github.com/openshift-quickstart/irc-leaderboard-quickstart
just
multiplayer notepad
An emote is an entry into a text-based chat client that indicates an action taking place. Unlike emoticons, they are not text art, and instead describe the action using words.
To perform an action,
type '/me' followed by a description of an action:
/me rolls eyes at IRC over-actors
Give it a try:
/join #openshift
OpenShift on freenode IRC
IRC is built on an open standard, allowing you to host it as a service (on your own hardware), or from within a private network (for added security).
OpenShift was designed with many of the same characteristics in mind, minus the drama.
Sign up for an OpenShift Online account:
http://openshift.redhat.com/sudo gem install rhc
RHC client tool installation
https://openshift.redhat.com/community/developers/rhc-client-tools-install
Openshift Getting Started guide
https://openshift.redhat.com/community/developers/get-started
rhc setup
Will automatically:
rhc app create APP_NAME APP_CARTRIDGE
rhc app create ircbot nodejs-0.6
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'...
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.
You now have a basic node.js app up an running on OpenShift!
Your gear is now configured with:
cd ircbot
Using Node's npm package manager:
npm install irc -S
Include the `-S` flag in order to save this dependency to your application's `package.json` file.
Add 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
});
Listen 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!');
}
});
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!");
}
});
Commit your changes locally
git commit -m 'adding irc npm dependency, initializing irc library, adding a few jokes'git push
Done! Your bot should now be live on IRC.
/join #botzoo
If you don't already have an IRC client, you can connect to freenode on the web:
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...
If your bot decides to quit mid-conversation, it may be suffering from a bug.
Checking it's logs may reveal what set it off:
rhc tail ircbot
Or, debug it while running locally:
npm start
rhc cartridge add mongodb-2.2
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
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.1
The rockmongo username and password will be the same as the MongoDB credentials above.
rhc cartridge add rockmongo-1.1
(optional)
npm install -S mongojs
var 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_MONGODB_DB_PORT + '/' +
process.env.OPENSHIFT_APP_NAME;
}
var db = mongojs(connection_string, ['scoreboard']);
bot.addListener('message', function(from, to, message) {
if( message.indexOf('++') > -1 ){
var subject = message.slice(0,message.indexOf('++'));
db.scoreboard.findAndModify({
query: { name: subject },
update: { $inc: { score:1 }},
new: true,
upsert: true
}, function(err, doc) {
bot.say(to, "score: " + doc.score );
});
}
});
bot.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);
}
});
}
});
git add package.json server.js
git commit -m 'adding a social scoreboard'
git push
term++
[raphael] Anyone down for pizza?
[donatello] pizza++
[ircbot] score: 43
[raphael] Let's order two
[leonardo] Good idea, Raph! raphael++
[ircbot] score: 23
ircbot scoreboard
[ircbot] ircbot: 7
[ircbot] ryanj: 4
[ircbot] doris: -2
rhc app create ircbot nodejs-0.6 mongodb-2.2 --from-code=https://github.com/openshift-quickstart/irc-leaderboard-quickstart.git
You can find much more information about this npm IRC library here:
Thanks for following along! --ryanj