Skip to content

Commit

Permalink
updated eventloop, cache, functions for new api
Browse files Browse the repository at this point in the history
  • Loading branch information
epai committed May 3, 2016
1 parent 91f435d commit f7f6c5b
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 142 deletions.
86 changes: 86 additions & 0 deletions cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var Cache = function() {
this.api = undefined;
this.namesCache = {}; // id : name
this.threadsCache = {}; // id : {name : threadName, ids : participantIDs}
}

Cache.prototype.addNamesInfo = function(participantIDs, cb) {
var cache = this.namesCache;
var newIDs = [];
for (var i=0; i < participantIDs.length; i++) {
var id = participantIDs[i];
if (!cache[id])
newIDs.push(id);
}
if (newIDs.length > 0) {
this.api.getUserInfo(newIDs, function(err, ret) {
if (err) return console.error(err);
for (var id in ret)
if (ret.hasOwnProperty(id))
cache[id] = ret[id].name;
cb();
});
} else {
cb();
}
}

Cache.prototype.addThreadInfo = function(threadID, cb) {
var callback = function(err, ret) {
if (err) return console.error(err);
this.threadsCache[threadID] = {
'name':ret.name,
'ids':ret.participantIDs}
this.addNamesInfo(ret.participantIDs, cb);
}
this.api.getThreadInfo(threadID, callback.bind(this));
};

Cache.prototype.load = function(api, threadID, cb) {
// all code that relies on names being in the cache should be
// called in the callback (cb) of this function to ensure no
// cache misses.
if (this.api === undefined)
this.api = api;
if (!this.threadsCache[threadID]) {
this.addThreadInfo(threadID, cb);
} else {
cb();
}
}

Cache.prototype.getAllParticipantIDs = function(threadID) {
return this.threadsCache[threadID].ids;
}

Cache.prototype.senderFromID = function(senderID) {
return this.namesCache[senderID];
}

Cache.prototype.threadFromID = function(threadID) {
return this.threadsCache[threadID].name;
}

Cache.prototype.getIDFromName = function(name) {
name = name.toLowerCase();
var match = undefined;
var matchNum = 0;
for (var id in this.namesCache) {
if (this.namesCache.hasOwnProperty(id)) {
var cacheName = this.namesCache[id].toLowerCase();
if (cacheName === name)
return id;
var firstName = cacheName.split(" ")[0];
if (firstName === name) {
if (!match)
match = id;
else
return null; // match on first name must be ambiguous
}
}
}
return match;
}

var cache = new Cache();
module.exports.cache = cache;
51 changes: 29 additions & 22 deletions eventloop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@ var chatApp = require("facebook-chat-api");
var async = require("async");
var loginInfo = require("./login");
var functions = require("./functions");
var cache = require("./cache").cache;

console.log('Logging In')

chatApp({email: loginInfo.email, password: loginInfo.password}, function (err, api) {
if (err) {
console.log(err);
} else {
api.listen(function (err, message) {
if (err) {
if (err) {
console.log(err);
} else {
console.log("Starting Listening")
api.listen(function (err, message) {
console.log("message!")
if (err) {
console.error(err);
process.exit(-1);
}

cache.load(api, message.threadID, function() {
// console.log(cache.namesCache)
// console.log(cache.threadsCache)
for (var f in functions) {
var func = functions[f];
var callback = function(){};
if (message.body && message.body.match(func.matchPattern)) {
func.action(api, message, callback);
} else if (!message.body) {
console.log("sticker!");
}
};
});

console.error(err);
process.exit(-1);
}
else {
for (var f in functions) {
var func = functions[f];
var callback = function(){};
if (message.body.match(func.matchPattern)) {
func.action(api, message, callback);
}
else if (func['onMessage']) {
func.onMessage(api, message);
}
};
}
});
}
});
}
});
32 changes: 11 additions & 21 deletions functions/emoji.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
/*
* Module for emoji actions in the bot.
* Module for emoji actions in the bot.
*
* Currently very naive: the matchPattern just matches any occurrence of
* any of the emoji triggers (e.g. *shrug*), then the action checks for the
* presence of one of them. Note that it will only actually send a message for
* the first trigger it finds, not all of them.
*/

module.exports.matchPattern = /(\*shrug\*)|(\/stare)|(\/lenny)/g;
module.exports.matchPattern = /\/emoji/g;

module.exports.action = function(api, message, cb) {
if (message.body.indexOf("*shrug*") != -1) {
api.sendMessage("¯\\_(ツ)_/¯", message.threadID);
} else if (message.body.indexOf("/stare") == 0) {
var possibleName = message.body.split(" ")[1];
var isName = false;
if (possibleName) {
for (var i in message.participantNames) {
if (message.participantNames[i].split(" ")[0].toLowerCase() == possibleName) {
isName = true;
}
}
}

var msg = "ಠ_ಠ";
if (isName)
msg = "@" + possibleName + ": " + msg;
api.sendMessage(msg, message.threadID);
} else if (message.body.indexOf("/lenny") == 0) {
api.sendMessage("( ͡° ͜ʖ ͡° )", message.threadID);
if (message.threadID !== message.senderID) {
cb();
return;
}
var emojis = [
"¯\\_(ツ)_/¯",
"ಠ__ಠ",
"( ͡° ͜ʖ ͡° )"
];
api.sendMessage(emojis.join("\r\n"), message.threadID);
}
20 changes: 13 additions & 7 deletions functions/hmsspace.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
var key = require("../login")['hmsspacekey'];
var email = require("../login")['creatoremail'];
var request = require("request");
var cache = require("../cache").cache;

module.exports.matchPattern = /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
module.exports.matchPattern =
/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;

module.exports.action = function (api, message, cb) {
var target = "http://hms.space/api/add";
var allLinks = message.body.match(module.exports.matchPattern);
var target = "http://hms.space/api/add";
var allLinks = message.body.match(module.exports.matchPattern);
for (var i in allLinks) {
var toShorten = allLinks[i];
var senderName = cache.senderFromID(message.senderID);
var threadName = cache.threadFromID(message.threadID);
request.post({
url: target,
form: {
"apiKey": key,
"creator": message.senderName,
"creator": senderName,
"target": toShorten,
"chatID": message.threadID,
"chatName": message.threadName,
"chatName": threadName,
}
}, function(err, httpResponse, body){
if (err) {
Expand All @@ -26,13 +30,15 @@ module.exports.action = function (api, message, cb) {
var res = JSON.parse(body);
if (res["Success"]) {
var url = res["ResultURL"];
//api.sendMessage(url, message.threadID);
// DO NOT send messages; will get banned if so.
// api.sendMessage("logged: " + url, message.threadID);
return setImmediate(cb);
} else {
if (body.indexOf("redirect loops") != -1)
return setImmediate(cb);
console.error(res);
api.sendMessage("hms.space fucked up. Blame @jordon wing.", message.threadID);
api.sendMessage("hms.space fucked up. Blame @jordon wing.",
message.threadID);
}
}
});
Expand Down
29 changes: 29 additions & 0 deletions functions/linklisten.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var key = require("../login")['hmsspacekey'];
var email = require("../login")['creatoremail'];
var request = require("request");
var cache = require("../cache");

// var cache = new Cache();

module.exports.matchPattern = /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;

// First, checks if it isn't implemented yet.
if (!String.format) {
String.format = function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}

module.exports.action = function (api, message, cb) {
var links = message.body.match(module.exports.matchPattern);
for (var i=0; i < links.length; i++) {
var link = links[i];
console.log("found link! " + link);
}
}
Loading

0 comments on commit f7f6c5b

Please sign in to comment.