Created
May 27, 2016 16:06
-
-
Save ben/52df4ee4d17a6c590a23f0a734cbe20e to your computer and use it in GitHub Desktop.
Something like Hubot, implemented on top of Botkit
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
require('dotenv').config() | |
var Fs = require('fs') | |
var Path = require('path') | |
var Botkit = require('botkit') | |
if (!process.env.SLACK_TOKEN) { | |
console.error('ERROR: this requires "SLACK_TOKEN" to be a valid Slack API token.') | |
process.exit(-1) | |
} | |
var controller = Botkit.slackbot({ | |
debug: false | |
}) | |
controller.spawn({ | |
token: process.env.SLACK_TOKEN | |
}).startRTM() | |
controller.hears('ping', 'direct_message', function (bot, msg) { | |
bot.reply(msg, 'PONG') | |
}) | |
controller.hears(['help ?(.*)'], ['direct_message', 'direct_mention', 'mention'], function (bot,msg) { | |
bot.startTyping(msg) | |
var results = [] | |
var re = RegExp(msg.match[1], 'gi') | |
for (var i in helpEntries) { | |
he = helpEntries[i] | |
if (he.search(re) != -1) results.push(he) | |
} | |
bot.reply(msg, results.join('\n')) | |
}) | |
// Load scripts | |
var helpEntries = [] | |
var scriptDir = Path.resolve('.', 'scripts') | |
var scripts = Fs.readdirSync(scriptDir).sort() | |
for (var i in scripts) { | |
var file = scripts[i] | |
var ext = Path.extname(file) | |
var path = Path.join(scriptDir, Path.basename(file, ext)) | |
if (!require.extensions[ext]) continue | |
var script | |
try { | |
controller.log('Loading script:', file) | |
script = require(path) | |
// Call init function so script can set up listeners | |
if (typeof(script.init) === 'function') { | |
script.init(controller) | |
} else { | |
controller.log.error('expected init to be a function, instead was ' + typeof(script.init)) | |
} | |
// Put the help items into the help directory | |
if (script.help) { | |
helpEntries = helpEntries.concat(script.help) | |
} | |
} catch (e) { | |
controller.log.error("Couldn't load", file, '\n', e) | |
} | |
} |
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
// Put this in the "scripts" directory | |
var request = require('request') | |
var _ = require('underscore') | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min)) + min; | |
} | |
function fetchResults(bot, msg, n, cb) { | |
request({ | |
url: 'https://api.datamarket.azure.com/Bing/Search/Image', | |
qs: { | |
Query: "'cute pug'", | |
$skip: getRandomInt(0, 100), | |
$top: n, | |
$format: 'json' | |
}, | |
auth: { | |
user: process.env.BING_ACCOUNT_KEY || null, | |
pass: process.env.BING_ACCOUNT_KEY || null | |
} | |
}, function(err, res, body) { | |
if (err || (res && res.statusCode !== 200)) { | |
bot.reply(msg, "Whoops, got an error: " + err + ' ' + | |
res.statusCode + ' ' + res.statusMessage) | |
} else { | |
cb(JSON.parse(body).d.results) | |
} | |
}) | |
} | |
module.exports = { | |
init: function (controller) { | |
if (!process.env.BING_ACCOUNT_KEY) { | |
controller.log.warning('To enable pug commands, provide BING_ACCOUNT_KEY') | |
return | |
} | |
contexts = ['direct_message', 'direct_mention', 'mention'] | |
controller.hears('pug bomb ?(.*)?', contexts, function (bot,msg) { | |
bot.startTyping(msg) | |
fetchResults(bot, msg, msg.match[1] || 10, function (results) { | |
_.each(results, function (result) { | |
bot.reply(msg, result.MediaUrl) | |
}) | |
}) | |
}) | |
controller.hears('pug( me)?', contexts, function (bot,msg) { | |
bot.startTyping(msg) | |
fetchResults(bot, msg, 1, function (results) { | |
bot.reply(msg, results[0].MediaUrl) | |
}) | |
}) | |
}, | |
help: [ | |
'pug [me] - Get a pug', | |
'pug bomb [N] - Get N pugs (default 10)' | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx for this code snipet