Last active
August 29, 2015 14:22
-
-
Save daltonclaybrook/6eb377c7c7e22f4e7002 to your computer and use it in GitHub Desktop.
Helper Bot
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
function handleGist(message, user, res) { | |
console.log('handling gist: ' + message); | |
var components = message.split(' '); | |
var gistID = null; | |
if (components.length >= 3) { | |
gistID = components[2]; | |
// gistID = '6eb377c7c7e22f4e7002'; | |
console.log('gistID: ' + gistID); | |
var url = 'https://api.github.com/gists/' + gistID; | |
var options = { | |
url: url, | |
headers: { | |
'Accept': 'application/vnd.github.v3+json', | |
'User-Agent': 'BR-Helper-Bot' | |
} | |
}; | |
request(options, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var parsed = JSON.parse(body); | |
var firstKey = Object.keys(parsed.files)[0]; | |
var firstFile = parsed.files[firstKey]; | |
console.log(JSON.stringify(firstFile)); | |
var message = '/code\n' + firstFile.content; | |
sendMessage(message, res); | |
} else { | |
console.log('error'); | |
res.send(); | |
} | |
}); | |
} else { | |
sendMessage('Here\'s what a request should look like:\n/example gist 6eb377c7c7e22f4e7002', res); | |
} | |
} |
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
router.post('/', function(req, res) { | |
console.log(req.body); | |
var fullMessage = req.body.item.message.message; | |
var user = req.body.item.message.from.mention_name; | |
var components = fullMessage.split(' '); | |
if ((components.length < 1) || (components[0] != '/bot')) { | |
console.log('invalid command'); | |
res.send(); | |
return; | |
} | |
var command = (components.length >= 2) ? components[1] : ''; | |
if (command == 'cat') { | |
handleCatPic(fullMessage, user, res); | |
} else if (command == 'test') { | |
handleTest(fullMessage, user, res); | |
} else if (command == 'gist') { | |
handleGist(fullMessage, user, res); | |
} else { | |
handleUnrecognized(fullMessage, user, res); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment