Created
January 8, 2015 00:55
-
-
Save JamyDev/4d03999ec75dd543f39c to your computer and use it in GitHub Desktop.
Beam.pro chatbot (Chuck Norris)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// YOU NEED NODE.JS TO RUN THIS | |
// Please run the following command first: npm install ws readline request tough-cookie | |
// Then run the program with: node filename.js | |
var WebSocket = require('ws'); | |
var readline = require('readline'); | |
var request = require('request'); | |
// Various options | |
var options = { | |
baseUrl: 'https://beam.pro', | |
ua: 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.45 Safari/537.36' | |
}; | |
// User data will go here | |
var user = {}; | |
// Channel data will go here | |
var channel = {}; | |
var rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
process.on('SIGINT', function () { | |
process.exit(); | |
}); | |
process.on('SIGTERM', function () { | |
process.exit(); | |
}); | |
console.log("Connecting to Beam, please log in."); | |
getChannel(function (err, chan) { | |
getLoginInfo(function (username, password) { | |
channel = chan; | |
beam.login(username, password, function (err, data) { | |
if (err || typeof data === 'string') { | |
console.error(err || data); | |
process.exit(); | |
} | |
user = data; | |
connectToChat(); | |
}); | |
}); | |
}); | |
var beam = { | |
// Logs into Beam | |
login: function (username, password, cb) { | |
request({ | |
method: 'post', | |
url: options.baseUrl + '/users/login', | |
json: { | |
username: username, | |
password: password | |
}, | |
jar: true, | |
headers: { | |
'User-Agent': options.ua | |
} | |
}, function (err, res, data) { | |
cb(err, data); | |
}); | |
}, | |
// Gets channel info | |
channel: function (name, cb) { | |
request({ | |
method: 'get', | |
url: options.baseUrl + '/channels/'+name, | |
json: true, | |
jar: true, | |
headers: { | |
'User-Agent': options.ua | |
} | |
}, function (err, res, data) { | |
cb(err, data); | |
}); | |
}, | |
// Gets chat info | |
chat: function (id, cb) { | |
request({ | |
method: 'get', | |
url: options.baseUrl + '/chats/'+id, | |
json: true, | |
jar: true, | |
headers: { | |
'User-Agent': options.ua | |
} | |
}, function (err, res, data) { | |
cb(err, data); | |
}); | |
}, | |
// Creates the chat socket | |
// See ws.on('message') for other chat messages | |
chatSocketConnect: function (endpoint, authkey, cb) { | |
var ws = new WebSocket(endpoint, {headers: {'user-Agent': options.ua}}); | |
ws.on('open', function open() { | |
ws.send(JSON.stringify({type: "method", method: "auth", arguments: [channel.id, user.id, authkey], id: 1})); | |
}); | |
ws.once('message', function(data, flags) { | |
cb(ws); | |
}); | |
ws.on('message', function(data, flags) { | |
var msg = JSON.parse(data); | |
if (msg.type === 'event' && msg.event === 'ChatMessage' && msg.data.username !== user.username) { | |
console.log('[' + msg.data.username + '] ' + msg.data.msg); | |
} | |
}); | |
} | |
} | |
function connectToChat() { | |
beam.chat(channel.id, function (err, data) { | |
beam.chatSocketConnect(data.endpoints[0], data.authkey, function (socket) { | |
console.log("I'M A FIRING MAH LAZORZ"); | |
// Chuck norris joke generator | |
setInterval(function () { | |
request({ | |
method: 'get', | |
json: true, | |
url: 'http://api.icndb.com/jokes/random', | |
headers: { | |
'User-Agent': options.ua | |
} | |
}, function (err, res, data) { | |
socket.send(JSON.stringify({type: "method", method: "msg", arguments: [data.value.joke], id: 2})); | |
}); | |
}, 2000); | |
// End of joke generator | |
}); | |
}); | |
} | |
function getChannel(cb) { | |
rl.question("What channel would you like to connect to: ", function (name) { | |
beam.channel(name, function (err, data) { | |
cb(err, data); | |
}); | |
}); | |
} | |
function getLoginInfo(cb) { | |
rl.question("Username: ", function (username) { | |
user.username = username; | |
hidden("Password: ", function(password) { | |
user.password = password; | |
cb(username, password); | |
}); | |
}); | |
} | |
function hidden(query, callback) { | |
var stdin = process.openStdin(); | |
var onDataHandler = function(char) { | |
char = char + ""; | |
switch (char) { | |
case "\n": case "\r": case "\u0004": | |
// Remove this handler | |
stdin.removeListener("data",onDataHandler); | |
break;//stdin.pause(); break; | |
default: | |
process.stdout.write("\033[2K\033[200D" + query + Array(rl.line.length+1).join("*")); | |
break; | |
} | |
} | |
process.stdin.on("data", onDataHandler); | |
rl.question(query, function(value) { | |
rl.history = rl.history.slice(1); | |
callback(value); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 10 baseUrl: 'https://beam.pro/api/v1',