Last active
August 28, 2015 09:54
-
-
Save mrm8488/c25dc83b3bcac1982c38 to your computer and use it in GitHub Desktop.
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
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