Skip to content

Instantly share code, notes, and snippets.

@mrm8488
Last active August 28, 2015 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrm8488/c25dc83b3bcac1982c38 to your computer and use it in GitHub Desktop.
Save mrm8488/c25dc83b3bcac1982c38 to your computer and use it in GitHub Desktop.
module['exports'] = function registerIO(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');
var mongoose = require('mongoose');
mongoose.connect('mongodb://moelia:basel2015@ds035663.mongolab.com:35663/employees_activity');
var activitySchema = mongoose.Schema({
user: String,
activity: {type: String, enum: ['entrada', 'salida']},
date: { type: Date, default: Date.now }
});
var Activity = mongoose.model('Activity', activitySchema);
// 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 === 'HeqP9qOUV8e2MezhD0zAxxa0' && (params.text.toLowerCase() === 'entrada' || params.text.toLowerCase() === 'salida' )) {
var d = new Date();
d.setUTCHours(d.getUTCHours() + 2);
var newActivity = new Activity({user: params.user_name, activity: params.text.toLowerCase(), date: d});
newActivity.save(function (err){});
var icon = (params.text.toLowerCase() === 'entrada') ? ':arrow_left:' : ':arrow_right:';
// Set up the options for the HTTP request.
var options = {
// Use the Webhook URL from the Slack Incoming Webhooks integration.
uri: 'https://hooks.slack.com/services/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',
method: 'POST',
// Slack expects a JSON payload with a "text" property.
json: {
'text': '@' + params.user_name + ' has just registered the event ' + '*' + params.text + '*' + ' at Moelia ' + d.toString().split('GMT')[0] + ' ' +icon ,
// Request that Slack parse URLs, usernames, emoji, etc. in the
// incoming text.
'parse': 'full'
}
};
// 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(hook.req.headers['x-forwarded-for']);
hook.res.end('Activity registered successfully! :+1:');
});
} else {
// If the token didn't match, send a response anyway for debugging.
hook.res.end('Error: token or command text does not match. Command format: /activity <entrada | salida>');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment