Skip to content

Instantly share code, notes, and snippets.

@iamnewton

iamnewton/rubi.js

Forked from kmoe/highfive.js
Last active Aug 29, 2015
Embed
What would you like to do?
module.exports = function rubi( hook ) {
// hook.io has a range of node modules available - see
// https://hook.io/modules.
// We use request (https://www.npmjs.com/package/request) for an easy way to
// make the HTTP request.
var request = require( 'request' );
// The parameters passed in via the slash command POST request.
var params = hook.params;
// Check that the hook has been triggered from our slash command by
// matching the token against the one in our environment variables.
if( params.token === hook.env.rubi_token ) {
// @cnewton for writing this code
var regex = /(@\w+)(.*)/g
var user = params.text.replace( regex, '<$1>' );
var message = params.text.replace( regex, '$2');
// Set up the options for the HTTP request.
var options = {
// Use the Webhook URL from the Slack Incoming Webhooks integration.
uri: hook.env.rubi_url,
method: 'POST',
// Slack expects a JSON payload with a "text" property. Links should be
// enclosed in '<', '>' for correct formatting in Slack.
json: {
'text': '<@' + params.user_name + '> sent a Rubi to ' + user + message
}
};
// Make the POST request to the Slack incoming webhook.
request( options, function( error, response, body ) {
// Pass error back to client if request endpoint can't be reached.
if ( error ) {
hook.res.end( error.message );
}
// Finally, send the response. This will be displayed to the user after
// they submit the slash command.
hook.res.end('Success! Go to <#rubi> to see it :smile:');
});
} else {
// If the token didn't match, send a response anyway for debugging.
hook.res.end( 'Incorrect token.' );
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.