Skip to content

Instantly share code, notes, and snippets.

@jmarkovic
Last active June 29, 2016 08:20
Show Gist options
  • Save jmarkovic/57d411a95b62f1c1799b6ad7276464e3 to your computer and use it in GitHub Desktop.
Save jmarkovic/57d411a95b62f1c1799b6ad7276464e3 to your computer and use it in GitHub Desktop.
Simple, and ineffective, bot for slack that tries to talk with you and find a github repo with provided query. Written using NodeJS and Botkit. Botkit is a required dependency.
var controller = require('botkit').slackbot();
var https = require("https");
var bot = controller.spawn({
debug: true,
token: "bot_token_goes_here"
});
bot.startRTM(function(err,bot,payload) {
if (err) {
throw new Error('Could not connect to Slack');
} else {
console.log("success");
}
});
controller.hears('wifi', ['ambient'], function(bot, message) {
console.log(message);
if (message.text.indexOf("sexy") > -1) {
bot.reply(message,
"https://assets.infinum.co/assets/people/selfies/125-ae0e3f14011f78dadf0461367b617175.jpg");
}
bot.reply(message, "SSID: NeMozeNamNikoNista\n Password: uberpassword");
});
controller.hears(['github repo', 'github', 'repo'], ['ambient'],
function(bot, message) {
bot.startConversation(message, askRepo);
});
var askRepo = function(response, convo) {
convo.ask("What repo would you like me to find?", function(response, convo) {
var text = response.text;
convo.say("I'm looking for `" + text + "`");
searchGithub(text, function(top_three) {
convo['top_three'] = top_three;
var count = top_three.length;
var question = "";
if (count == 0) {
question = "Unfortunatelly, I found nothing :confounded:";
convo.say(question);
} else {
question = "This is what I found. Which one would you like?";
console.log(top_three);
top_three.forEach(function(elem, index) {
question += "\n" + (index + 1) + ". " + elem.full_name;
});
convo.ask(question, exitStrategy);
}
convo.next();
});
});
}
var repoSelection = function(response, convo) {
var index = parseInt(response.text) - 1;
var top_three = convo['top_three'];
if (index >= 0 && index <= top_three.length) {
var selection = top_three[index];
convo.say("Okay, displaying `" + selection.full_name + "`");
convo.say("" + selection.html_url);
} else {
convo.ask("I can only understand numbers that I've displayed."
+ ":confounded: Could you repeat your entry?", exitStrategy);
}
convo.next();
}
var exitConvo = function(response, convo) {
convo.say("Okay, goodbye!");
convo.next();
}
var exitStrategy = [
{
pattern: "exit|done",
callback: exitConvo
},
{
default: true,
callback: repoSelection
}
]
function searchGithub(query, callback) {
const GITHUB_API = 'api.github.com';
const GITHUB_REPO_SEARCH = '/search/repositories?q=';
var headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
}
var call = {
host : GITHUB_API,
path : GITHUB_REPO_SEARCH + escape(query),
method: 'GET',
headers : headers
}
var top_three = [];
var request = https.get(call, function(response) {
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
var bodyChunks = [];
response.on('data', function(chunk) {
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
var response = JSON.parse(body);
if (response.total_count >= 3) {
for(var i =0; i<3;i++) {
top_three.push(response.items[i]);
}
}
callback(top_three);
})
});
request.on('error', function(e) {
console.log('HTTP error --> ' + e.message);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment