Skip to content

Instantly share code, notes, and snippets.

@digilord
Created February 11, 2014 19:37
Show Gist options
  • Save digilord/8942482 to your computer and use it in GitHub Desktop.
Save digilord/8942482 to your computer and use it in GitHub Desktop.
// Packages we need
var GitHubApi = require("github");
var sugar = require("sugar");
var _ = require("underscore")._;
// Start of variable assignments
var issues_by_numbers = {};
var project = "MyGitHubProject";
var github = new GitHubApi({
// required
version: "3.0.0",
// optional
debug: false,
protocol: "https",
timeout: 5000
});
github.authenticate({
type: "basic",
username: "YouKnow", // You do don't you?
password: "1234password4321" // No not really
});
// Module setup
module.id = "brains";
var Brains = function(bot, botName){
var self = this;
self.bot = function(){};
if(typeof bot !== 'object')
throw "Instance of bot is NOT an object";
if(typeof botName !== 'string')
throw "You need a botName!";
self.botName = botName;
self.bot = bot;
self.say = function(to, message) {
self.bot.say(to, message);
};
self.listen = function(nick, to, text, message){
open_issues(nick, to, text, message);
help(nick, to, text, message);
};
// It's important to know that I, the bot, didn't say something so I don't get
// into an endless loop.
var isMe = function(nick){
if(nick.toLowerCase().has(self.botName)){
return true;
} else {
return false;
}
}
var open_issues = function(nick, to, text, message){
if(isMe)
return;
if(text.toLowerCase().has("open issues")){
self.say(to, nick + ": I am sending you a PM with any open issues I find.")
// Using the credentials provided in the github.authenticate call
// fetch all the issues that this user has access to.
github.issues.getAll({}, function(error, data){
var count = 0;
_.each(data, function(issue){
if(issue.repository.name === project && issue.state === 'open'){
count++;
issues_by_numbers[issue.number] = issue;
};
});
if(count > 0){
bot.say(nick, "Fetching open issues...");
var issue_numbers = Object.keys(issues_by_numbers).join(", ");
var msg = "There are " + count + " open issues. The issues are numbered: " + issue_numbers + ".";
self.say(nick, msg);
msg = "To get more information about a specific issue please say 'more about issue X' where X is the issue number.";
self.say(nick, msg);
} else {
self.say(nick, "I didn't find any open issues.")
}
});
};
};
var help = function(nick, to, text, message){
if(isMe)
return;
if(text.toLowerCase().has("bot help")){
var msg = "Queries for " + to + "\n";
msg += "---------------\n";
msg += "open issues - Tells you about any open issues.\n";
msg += "more about issue X - Tells you about the issue where X is the issue number.\n"
self.say(nick, msg);
}
};
};
exports.Brains = Brains;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment