Instantly share code, notes, and snippets.

Embed
What would you like to do?
secret santa generator and post to slack, disclaimer: rushed together, did what we needed...
var Slack = require('node-slack'),
webhookUri = '{WEBHOOKURI}';
var slack = new Slack(webhookUri);
var users = [
'sydlawrence',
'adverplanner',
'sophtly',
'robman',
'nrocy'
];
var recipients = [];
var price = Math.floor(Math.random() * 100) / 100;
while (price < 7) {
price += 1;
}
var sendMessage = function(user, recipient) {
slack.send({
channel: '@' + user,
username: 'WMAS Secret Santa',
icon_url: 'http://www.sellmymobile.com/blog/wp-content/uploads/2014/12/0325.png',
text: 'Ho ho ho!',
attachments: [
{
color: 'good',
fields: [
{
title: 'You need to get a present for ' + recipient,
value: 'The present should be around £' + price
}
]
}]
}, function(err, response) {
console.log(err);
console.log(response);
});
}
function shuffle(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
while (true) {
var valid = true;
var x = [];
for (var i in users) {
x.push(users[i]);
}
recipients = shuffle(x);
for (var i in recipients) {
if (recipients[i] === users[i]) {
valid = false;
break;
}
}
if (valid) {
break;
}
}
for (var i in users) {
sendMessage(users[i], recipients[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment