Slack Team Notifications

Show notifications for Slack teams and channels with option to mark as read. See https://github.com/benjifs/bitbar-slack-team-notifications for configuration instructions.

Image preview of Slack Team Notifications plugin.

slack-team-notifications.1m.js

Edit
Open on GitHub
#!/usr/bin/env /usr/local/bin/node
/* jshint esversion: 8 */
/* jshint asi: true */

// <xbar.title>Slack Team Notifications</xbar.title>
// <xbar.version>v1.1.1</xbar.version>
// <xbar.author>Benji Encalada Mora</xbar.author>
// <xbar.author.github>benjifs</xbar.author.github>
// <xbar.image>https://i.imgur.com/ORbsRBx.jpg</xbar.image>
// <xbar.desc>Show notifications for Slack teams and channels with option to mark as read. See https://github.com/benjifs/bitbar-slack-team-notifications for configuration instructions.</xbar.desc>
// <xbar.dependencies>node.js superagent</xbar.dependencies>

const request = require('superagent');
const tokens = require('./.tokens.js');

// CONFIG
// BITBAR_SLACK_ICON defines which Slack Icon is shown
// 0 -> Original Slack Icon
// 1 -> White Slack Icon
// 2 -> Black Slack Icon
const BITBAR_SLACK_ICON = process.env.BitBarDarkMode ? 1 : 2;
// If MENTIONS_ONLY is true, the count only includes mentions and DMs.
// If MENTIONS_ONLY is false, the count includes all unread messages.
const MENTIONS_ONLY = false;
// MAX_LENGTH of channel name or user name
const MAX_LENGTH = 18;
// Show `No new notifications` message
const SHOW_ZERO_NOTIFICATIONS = true;

// Is Slack.app installed?
let SLACK_INSTALLED = true;
const { exec } = require('child_process');
exec('ls /Applications | grep Slack | wc -l', (err, stdout, stderr) => {
	if (!err && stdout == 0) {
		SLACK_INSTALLED = false;
	}
});

const DEBUG = process.argv.indexOf('--debug') > 0;
const SCRIPT = process.argv[1];

// Slack App Credentials
const SLACK_OAUTH_AUTHORIZE = 'https://slack.com/oauth/authorize?scope=client&client_id=';
const SLACK_CLIENT_ID = '11708641376.684689498789';

// Slack API
const SLACK_API = 'https://slack.com/api/';
const SLACK_CONVERSATIONS = 'conversations';
const SLACK_CHANNELS = 'channels';
const SLACK_GROUPS = 'groups';
const SLACK_IM = 'im';
const SLACK_TEAM = 'team';
const SLACK_USERS = 'users';
const SLACK_INFO = '.info';
const SLACK_LIST = '.list';
const SLACK_MARK = '.mark';
const SLACK_HISTORY = '.history';
const SLACK_AUTH_TEST = 'auth.test';

// ICONS {
// Original Slack icon (unused)
const SLACK_ICON = 'image=iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6dGlmZj0iaHR0cDovL25zLmFkb2JlLmNvbS90aWZmLzEuMC8iPgogICAgICAgICA8dGlmZjpPcmllbnRhdGlvbj4xPC90aWZmOk9yaWVudGF0aW9uPgogICAgICA8L3JkZjpEZXNjcmlwdGlvbj4KICAgPC9yZGY6UkRGPgo8L3g6eG1wbWV0YT4KTMInWQAACatJREFUWAmlWGtsVMcVPjP33t21vbt+Ydog8ZSSNjXhISgRkFLTH3kBoSQxceBPo/5o+6tNq/KjvNaISBVSkfqvitTSRIUATtQmMa/8CKaNjSCkBIOVhKaBEBEpNn7trr3rvXdm+p25u8bYsFjqkWZ27syZc86cty3ofmCMICEMo63oHNxNJLZimcS4opXefWFNXWfKGJlq2yxoc5ta2L59lZRyD84XYqTJiEPd6/aksCZKpSSGtut7TOIe+7e3iwJ9v3PojciM6pZgOEekFMnKOOncCOmC3/ThmtozfOGRY9vXSMc5IyujpEcLRI4gN1lJfn/6SPfTe1vIEB6HuQzIMmfUbIzD2lnRNfyEW5Vs8W+lf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ABJRU5ErkJggg=='
// }

let unread_count = 0;
const slack_output = {};
const errors = [];
let call_log = {};

debug('Debugging');

if (process.argv.indexOf('--mark') > 0) {
	console.log('Mark as read');

	let token;
	for (let i = 3; i < process.argv.length; i++) {
		if (process.argv[i].indexOf('--token=') === 0) {
			token = process.argv[i].split('=')[1];
		}
	}
	if (!token) {
		console.log('Error: Missing token');
		return;
	}

	for (let i = 3; i < process.argv.length; i++) {
		let args = process.argv[i].split('=');
		if (args.length != 2) {
			continue;
		}
		if ([SLACK_CHANNELS, SLACK_GROUPS, SLACK_IM].indexOf(args[0]) < 0) {
			continue;
		}

		let channels = args[1].split(',');
		for (let j in channels) {
			console.log('/' + args[0] + SLACK_MARK + ' (' + channels[j] + ')');
			slack_request(SLACK_CONVERSATIONS + SLACK_MARK, token, {
				'channel': channels[j],
				'ts': Math.floor(Date.now() / 1000) + '.000000'
			})
				.then((body) => {
					// console.log('  Success: ' + args[0] + ':' + channels[j]);
				});
		}
	}
	return;
}

function debug(message) {
	return DEBUG && console.log(message);
}

function slack_request(URL, token, query) {
	debug('  /' + URL + (query && query.channel ? ' (' + query.channel + ')' : ''));
	// The following is to keep track of how many calls are being made
	// for each token to each method. Should help debug the rate limits
	if (!call_log[token]) {
		call_log[token] = {};
	}
	if (!call_log[token][URL]) {
		call_log[token][URL] = 0;
	}
	call_log[token][URL]++;
	return request
		.get(SLACK_API + URL)
		.query(query)
		.auth(token, { type: 'bearer' })
		.then((res) => {
			debug(res.body);
			if (res && res.body && res.body.ok === true) {
				return Promise.resolve(res.body);
			}
			return Promise.reject(res.body.error);
		})
		.catch((err) => {
			debug('ERROR: ' + err);
			debug('  ' + URL);
			debug('  ' + JSON.stringify(query));
			errors.push(URL + ': ' + err + ' | color=red');
		});
}

function output() {
	unread_count = unread_count > 10 ? '10+' : unread_count > 0 ? unread_count : '';
	const slack_icon = BITBAR_SLACK_ICON == 2 ? SLACK_ICON_B : BITBAR_SLACK_ICON == 1 ? SLACK_ICON_W : SLACK_ICON;
	if (errors.length > 0) {
		console.log('! |color=red ' + slack_icon);
	} else {
		console.log(unread_count + ' | ' + slack_icon);
	}

	if (Object.keys(slack_output).length) {
		for (let i in slack_output) {
			let team = slack_output[i];

			if (SHOW_ZERO_NOTIFICATIONS || team.notifications.count > 0) {
				console.log('---');
				console.log(team.name + ' | size=12');
			}
			if (team.notifications.count > 0) {
				if (team.notifications.channels.length > 0) {
					console.log('Channels | size=12');
					for (let j in team.notifications.channels) {
						console.log(team.notifications.channels[j]);
					}
				}
				if (team.notifications.dms.length > 0) {
					console.log('Direct messages | size=12');
					for (let j in team.notifications.dms) {
						console.log(team.notifications.dms[j]);
					}
				}
				console.log('Mark all as read ' +
					'|bash=' + SCRIPT +
					' param1=--mark' +
					' param2=--token=' + team.token +
					(team.params[SLACK_IM] ? ' param3=' + SLACK_IM + '=' + team.params[SLACK_IM].join() : '') +
					(team.params[SLACK_GROUPS] ? ' param4=' + SLACK_GROUPS + '=' + team.params[SLACK_GROUPS].join() : '') +
					(team.params[SLACK_CHANNELS] ? ' param5=' + SLACK_CHANNELS + '=' + team.params[SLACK_CHANNELS].join() : '') +
					' refresh=true' +
					' terminal=false');
			} else if (SHOW_ZERO_NOTIFICATIONS) {
				console.log('No new notifications');
			}
		}
	}
	if (errors.length > 0) {
		console.log('---');
		console.log('Errors');
		for (let i in errors) {
			console.log('--' + errors[i]);
		}
	}
	console.log('---');
	console.log('Add New Workspace');
	console.log('--Generate OAuth Token | href=' + SLACK_OAUTH_AUTHORIZE + SLACK_CLIENT_ID);
	debug(call_log);
}

function format_channel_name(channel) {
	let name = '';
	if (channel.is_im) {
		name = '@' + channel.name;
	} else if (channel.name.indexOf('mpdm-') === 0) {
		let users = channel.name.split('-');
		users = users.filter((user, i) => {
			// mpdm string is of this format: `mpdm-xxxxx--yyyyy--zzzzz-n`
			// where xxxxx, yyyyy, and zzzzz are usernames
			// Remove empty strings, first, and last elements of the array
			return user !== '' && i !== 0 && i !== users.length - 1;
		});
		// Using │ since bitbar can not display the standard | character
		name = '@' + users.join('│@');
	} else {
		name = '#' + channel.name;
	}
	if (name.length > MAX_LENGTH) {
		name = name.substring(0, MAX_LENGTH - 1) + '…';
	}
	return name;
}

function channel_output(channel) {
	unread_count += channel.count;

	let output_str = format_channel_name(channel);
	output_str += ' '.repeat(MAX_LENGTH + 2 - output_str.length);
	output_str += (channel.count > 10 ? '10+' : channel.count);

	let key = channel.is_im ? SLACK_IM : channel.is_channel ? SLACK_CHANNELS : SLACK_GROUPS;
	let href;
	if (SLACK_INSTALLED) {
		href = 'slack://channel?team=' + channel.team + '&id=' + channel.id;
	} else {
		href = 'https://app.slack.com/client/' + channel.team + '/' + channel.id;
	}

	output_str += '|font=Menlo size=13 href=' + href;
	let alt_str = 'Mark as read ' +
		'|alternate=true' +
		' font=Menlo size=13' +
		' bash=' + SCRIPT +
		' param1=--mark' +
		' param2=--token=' + channel.token +
		' param3=' + key + '=' + channel.id +
		' refresh=true' +
		' terminal=false';

	if (output_str[0] == '@') {
		slack_output[channel.token].notifications.dms.push(output_str);
		slack_output[channel.token].notifications.dms.push(alt_str);
	} else {
		slack_output[channel.token].notifications.channels.push(output_str);
		slack_output[channel.token].notifications.channels.push(alt_str);
	}
	slack_output[channel.token].notifications.count++;

	if (!slack_output[channel.token].params[key]) {
		slack_output[channel.token].params[key] = [];
	}
	slack_output[channel.token].params[key].push(channel.id);
}

async function run() {
	if (typeof tokens === 'undefined' || !tokens || !tokens.length) {
		errors.push('Missing Slack Legacy Token | color=red href=https://api.slack.com/custom-integrations/legacy-tokens');
		errors.push('Generate OAuth Token | color=red href=' + SLACK_OAUTH_AUTHORIZE + SLACK_CLIENT_ID);
		return output();
	}

	for (let i in tokens) {
		await get_team_notifications(tokens[i]);
	}
	output();
}

function get_team_notifications(token) {
	return get_team_info(token)
		.then((team) => {
			if (team) {
				slack_output[token] = {
					'id': team.id,
					'name': team.name,
					'token': token,
					'notifications': {
						'channels': [],
						'groups': [],
						'dms': [],
						'count': 0
					},
					'params': {},
					'errors': []
				};
				return get_auth_info(token);
			} else {
				throw 'Invalid token';
			}
		})
		.then((user_id) => {
			slack_output[token].user_id = user_id;
			return get_team_conversations(token);
		})
		.then((channels) => {
			return check_channels_unread(channels, token);
		})
		.then((channels) => {
			for (let i in channels) {
				if (channels[i]) {
					channel_output(channels[i]);
				}
			}
		})
		.catch((errors) => {
			debug(errors);
		})
}

function get_team_info(token) {
	debug('Fetching team info for ' + token);
	return slack_request(SLACK_TEAM + SLACK_INFO, token)
		.then((body) => {
			if (body && body.team) {
				return Promise.resolve(body.team);
			}
		});
}

function get_auth_info(token) {
	debug('Fetch auth info for ' + token);
	return slack_request(SLACK_AUTH_TEST, token)
		.then((body) => {
			if (body && body.user_id) {
				return Promise.resolve(body.user_id);
			}
		});
}

function get_team_conversations(token) {
	debug('Fetching conversations for ' + token);
	return slack_request(SLACK_CONVERSATIONS + SLACK_LIST, token, {
		'exclude_archived': true,
		'limit': 200,
		'types': 'public_channel,private_channel,mpim,im'
	})
		.then((body) => {
			if (body && body.channels) {
				return Promise.resolve(body.channels);
			}
		});
}

async function check_channels_unread(channels, token) {
	let req = [];
	for (let i in channels) {
		if (channels[i]) {
			req.push(is_channel_unread(channels[i], token));
		}
	}
	return await Promise.all(req);
}

function is_channel_unread(channel, token) {
	return get_unread_count(channel, token)
		.then((unread_count) => {
			if (channel && unread_count > 0) {
				if (channel.is_im) {
					return get_user(channel.user, token)
						.then((user) => {
							if (user) {
								return Promise.resolve({
									'id': channel.id,
									'name': user.name,
									'count': unread_count,
									'team': user.team_id,
									'is_im': true,
									'token': token
								});
							}
						});
				} else if (channel.is_member || channel.is_group) {
					let team = channel.shared_team_ids && channel.shared_team_ids.length > 0 ? channel.shared_team_ids[0] : '';
					return Promise.resolve({
						'id': channel.id,
						'name': channel.name,
						'count': unread_count,
						'team': team,
						'is_channel': channel.is_member && channel.is_channel,
						'is_group': channel.is_group || (channel.is_channel && channel.is_private),
						'token': token
					});
				}
			}
		});
}

function get_unread_count(channel, token) {
	// unread_count_display is a count of messages that the calling user has
	// yet to read that matter to them (this means it excludes things like
	// join/leave messages)
	// unread_count_display does not show up consistently.
	// In the case unread_count_display is not present, check conversations.history
	if (channel && !('unread_count_display' in channel)) {
		return check_conversation_history(channel, token)
			.then((unread_count) => {
				return Promise.resolve(unread_count);
			})
	} else {
		return Promise.resolve(channel.unread_count_display);
	}
}

function check_conversation_history(channel, token) {
	debug('Fetch history for ' + channel.id);
	return slack_request(SLACK_CONVERSATIONS + SLACK_HISTORY, token, {
		'channel': channel.id,
		'oldest': channel.last_read != '0000000000.000000' ? channel.last_read : 0,
		'unreads': true
	})
		.then((body) => {
			if (body && body.unread_count_display > 0) {
				const user_id = slack_output[token].user_id;
				if (channel.is_im || channel.is_mpim) {
					return Promise.resolve(body.unread_count_display);
				}
				return Promise.resolve(count_mentions(body, user_id));
			}
			return Promise.resolve(0);
		});
}

function count_mentions(body, user_id) {
	if (MENTIONS_ONLY && body.messages && body.messages.length > 0) {
		let count = 0;
		for (let i in body.messages) {
			if (i >= body.unread_count_display) {
				break;
			}
			if (body.messages[i].text.indexOf(user_id) >= 0) {
				count++;
			}
		}
		return count;
	}
	return body.unread_count_display;
}

function get_user(user, token) {
	debug('Fetch user info for ' + user);
	return slack_request(SLACK_USERS + SLACK_INFO, token, {
		'user': user
	})
		.then((body) => {
			if (body && body.user) {
				return Promise.resolve(body.user);
			}
		});
}

run();