From fc76e01fc6ab49f5e7d7106945713cb96a4dffcb Mon Sep 17 00:00:00 2001 From: Foteini Giannaropoulou Date: Mon, 22 Jun 2020 01:36:02 +0300 Subject: [PATCH] Add '/dt' slash command --- app.js | 8 +++++- lib/slack_time.js | 65 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 5e282b8..883d423 100644 --- a/app.js +++ b/app.js @@ -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, diff --git a/lib/slack_time.js b/lib/slack_time.js index 7fef9e8..4659764 100644 --- a/lib/slack_time.js +++ b/lib/slack_time.js @@ -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; @@ -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"; @@ -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); }); } @@ -268,6 +288,23 @@ function formatChannelLocalTimes(startMoment, endMoment, userInfo, zones) { return times.join(", "); } +function toSlackFormattedDate(unix, timeOnly) { + timeOnly = timeOnly || false; + + return ''; +} + +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; @@ -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); @@ -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