Skip to content

Instantly share code, notes, and snippets.

@sydlawrence
Last active November 24, 2015 14:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sydlawrence/97f0205d4ab5df5af306 to your computer and use it in GitHub Desktop.
Save sydlawrence/97f0205d4ab5df5af306 to your computer and use it in GitHub Desktop.
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