Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add '/dt' slash command #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use('/slack/receive', slash({
handler: SlackTime,
handler: SlackTime.getSlackMessageWithChannelTimezones,
token: process.env.SLACK_SLASH_TOKEN
}));

app.use('/slack/receive_dt', slash({
handler: SlackTime.getSlackMessageWithLocalTime,
token: process.env.SLACK_SLASH_TOKEN
}));

app.use('/slack', auth({
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
Expand Down
65 changes: 62 additions & 3 deletions lib/slack_time.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ var logger = require('../lib/logger');
var NodeCache = require('node-cache');
var unknownRecipientMessageCache = new NodeCache();

module.exports = SlackTime = function (args) {
module.exports = SlackTime = {};

SlackTime.getSlackMessageWithChannelTimezones = function (args) {
var token = args.apiToken;
var originalMessage = args.message;
var currentUserId = args.currentUserId;
Expand Down Expand Up @@ -64,7 +66,25 @@ module.exports = SlackTime = function (args) {
var message = addTimezonesToSlackMessage(originalMessage, userInfo, zones);
return postToSlack(channelId, userInfo, message, token);
});
}
};

SlackTime.getSlackMessageWithLocalTime = function (args) {
var token = args.apiToken;
var originalMessage = args.message;
var currentUserId = args.currentUserId;
var teamId = args.teamId;
var channelId = args.channelId;

return getUserList(token)
.then(parseUserList)
.then(function (userList) {
var userInfo = currentUserInfo(userList.members, currentUserId);

var message = addLocalTimeToSlackMessage(originalMessage, userInfo);

return postToSlack(channelId, userInfo, message, token);
});
};

SlackTime.requiredOAuthScopes = "users:read,chat:write:bot,channels:read,commands";

Expand Down Expand Up @@ -121,7 +141,7 @@ function updateChannelAndProcessOriginalMessage(args) {
.then(function () {
var originalMessage = unknownRecipientMessageCache.get(teamId + "|" + channelId);
args.message = originalMessage;
return SlackTime(args);
return SlackTime.getSlackMessageWithChannelTimezones(args);
});
}

Expand Down Expand Up @@ -268,6 +288,23 @@ function formatChannelLocalTimes(startMoment, endMoment, userInfo, zones) {
return times.join(", ");
}

function toSlackFormattedDate(unix, timeOnly) {
timeOnly = timeOnly || false;

return '<!date^' + unix + '^' + (timeOnly ? '{time}':'{date_short_pretty} {time}') + '|Unix:' + unix + '>';
}

function formatUserLocalTimes(startMoment, endMoment) {
var dateStartString = formatSlackDate(startMoment);
var startUnix = startMoment.unix();

var includeEndDate = endMoment && formatSlackDate(endMoment) !== dateStartString;

var formatted = toSlackFormattedDate(startUnix) + (endMoment ? ' to ' + toSlackFormattedDate(endMoment.unix(), !includeEndDate) : '');

return formatted;
}

function chronoResultToUtcMoment(result, userInfo) {
if (!result) {
return;
Expand All @@ -281,6 +318,14 @@ function chronoResultToUtcMoment(result, userInfo) {
return dateMoment;
}

function chronoResultToMoment(result) {
if (!result) {
return;
}

return result.moment();
}

function addTimezonesToSlackMessage(message, userInfo, zones) {
var ref = moment().add(-moment().utcOffset() + userInfo.timezoneOffset, 'minutes');
var results = chrono.parse(message, ref);
Expand All @@ -295,6 +340,20 @@ function addTimezonesToSlackMessage(message, userInfo, zones) {
return message;
}

function addLocalTimeToSlackMessage(message, userInfo) {

var ref = moment().add(-moment().utcOffset() + userInfo.timezoneOffset, 'minutes');
var results = chrono.parse(message, ref);

results.forEach(function(result) {
var userLocalTime = formatUserLocalTimes(chronoResultToMoment(result.start), chronoResultToMoment(result.end));
message = message.replace(result.text, result.text + ' (' + userLocalTime + ' _at your local time_)');
});

return message;
}


function sortObjectToArray(obj) {
out = [];
Object
Expand Down