Skip to content

Instantly share code, notes, and snippets.

@cheeaun
Created July 27, 2016 04:12
Show Gist options
  • Save cheeaun/764894be3a23efbc7acb368ecba01ed7 to your computer and use it in GitHub Desktop.
Save cheeaun/764894be3a23efbc7acb368ecba01ed7 to your computer and use it in GitHub Desktop.
var blockspring = require('blockspring');
var request = require('request');
var moment = require('moment');
var cycleIndex = 0;
var cycle = function(list){
if (cycleIndex < list.length) cycleIndex++;
else cycleIndex = 1
return list[cycleIndex -1];
};
var webuildColors = ['#c11a18', '#e06149', '#228dB7', '#f1e9b4'];
var apiEndpoint = 'http://webuild.sg/api/v1/';
var webhook = function(team_domain, service_id, token, user_name, team_id, user_id, channel_id, timestamp, channel_name, text, trigger_word, raw_text, callback) {
var isToday = /today/i.test(text);
var url = apiEndpoint + (isToday ? ('check/' + moment().utcOffset(480).format('YYYY-MM-DD')) : 'events');
request.get({
url: url,
json: true
}, function(e, r, body){
var _events = body.events;
if (!isToday) _events = _events.slice(0, 5);
var events = _events.map(function(event){
var datetime = event.formatted_time.split(',');
var time = datetime.pop().trim();
var date = datetime.join(',');
return {
title: event.name,
title_link: event.url,
color: cycle(webuildColors),
mrkdwn_in: ['text'],
text: 'by *' + event.group_name + '* on *' + date + '*, ' + time + '\n' + event.location
};
});
var text = 'Oops, there are no events from We Build SG :sweat_smile:';
if (events.length){
if (isToday){
text = 'Events today from We Build SG https://webuild.sg/';
} else {
text = 'Upcoming events from We Build SG https://webuild.sg';
}
}
callback({
text: text, // send a text response (replies to channel if not blank)
attachments: events, // add attatchments: https://api.slack.com/docs/attachments
username: "We Build SG", // overwrite configured username (ex: MyCoolBot)
icon_url: "https://webuild.sg/public/logo.png", // overwrite configured icon (ex: https://mydomain.com/some/image.png
icon_emoji: "" // overwrite configured icon (ex: :smile:)
});
});
}
var block = function(request, response) {
var team_domain = request.params['team_domain'];
var service_id = request.params['service_id'];
var token = request.params['token'];
var user_name = request.params['user_name'];
var team_id = request.params['team_id'];
var user_id = request.params['user_id'];
var channel_id = request.params['channel_id'];
var timestamp = request.params['timestamp'];
var channel_name = request.params['channel_name'];
var raw_text = text = request.params['text'];
var trigger_word = request.params['trigger_word'];
// ignore all bot messages
if(user_id == 'USLACKBOT') return
// Strip out trigger word from text if given
if(trigger_word) {
text = text.substr(trigger_word.length).trim()
}
// Execute bot function
webhook(team_domain, service_id, token, user_name, team_id, user_id, channel_id, timestamp, channel_name, text, trigger_word, raw_text, function(output){
// set any keys that aren't blank
Object.keys(output).forEach(function(k) {
if(output[k]) {
response.addOutput(k, output[k]);
}
});
response.end();
});
}
blockspring.define(block);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment