Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.swp
*.swo
node_modules/
npm-debug.log
error_log.txt
12 changes: 8 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ function formatMessageMentions(text) {
}

// find special words
return formattedText.replace(
/<!channel>/g,
'{yellow-fg}@channel{/yellow-fg}');
return formattedText
.replace(
/<!channel>/g,
'{yellow-fg}@channel{/yellow-fg}')
.replace(
/<!everyone>/g,
'{yellow-fg}@everyone{/yellow-fg}');
}

function handleNewMessage(message) {
Expand Down Expand Up @@ -188,7 +192,7 @@ slack.getChannels((error, response, data) => {
}

const channelData = JSON.parse(data);
channels = channelData.channels.filter(channel => !channel.is_archived);
channels = channelData.channels.filter(channel => !channel.is_archived && !channel.is_mpim);

components.channelList.setItems(
channels.map(slackChannel => slackChannel.name)
Expand Down
53 changes: 36 additions & 17 deletions slackClient.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const fs = require('fs');
const request = require('request');
const WebSocket = require('ws');

const TOKEN = process.env.SLACK_TOKEN;

if (TOKEN === undefined) {
console.log( // eslint-disable-line no-console
'Error: SLACK_TOKEN undefined. Please add SLACK_TOKEN to the environment variables.'
);
process.exit(1);
const throwError = 'Error: SLACK_TOKEN undefined. Please add SLACK_TOKEN to the environment variables.';
throw new Error(throwError);
}

// makes a request to slack. Adds token to query
Expand All @@ -20,20 +17,19 @@ function slackRequest(endpoint, query, callback) {
qs,
}, (error, response, data) => {
if (error) {
fs.writeFileSync('error_log.txt', error);
process.exit(1);
throw new Error(error);
}

if (response.statusCode !== 200) {
fs.writeFileSync('error_log.txt', `Response Error: ${response.statusCode}`);
process.exit(1);
const logError = `Response Error: ${response.statusCode}`;
throw new Error(logError);
}

const parsedData = JSON.parse(data);
if (!parsedData.ok) {
// can't see console.logs with blessed
fs.writeFileSync('error_log.txt', `Error: ${parsedData.error}`);
process.exit(1);
// name_taken is expected if trying to channels.join on a group
if (!parsedData.ok && !(endpoint === 'channels.join' && parsedData.error === 'name_taken')) {
const logError = `Error: ${parsedData.error}`;
throw new Error(logError);
}

if (callback) {
Expand All @@ -53,7 +49,12 @@ module.exports = {
getChannels(callback) {
slackRequest('channels.list', {}, (error, response, data) => {
if (callback) {
callback(error, response, data);
const parsedData = JSON.parse(data);
slackRequest('groups.list', {}, (groupError, groupResponse, groupData) => {
const groupsData = JSON.parse(groupData);
parsedData.channels = parsedData.channels.concat(groupsData.groups);
callback(error, response, JSON.stringify(parsedData));
});
}
});
},
Expand All @@ -62,12 +63,28 @@ module.exports = {
name,
}, (error, response, data) => {
if (callback) {
callback(error, response, data);
const parsedData = JSON.parse(data);
if (!parsedData.ok && parsedData.error === 'name_taken') {
slackRequest('groups.list', {}, (groupError, groupResponse, groupData) => {
const groupList = JSON.parse(groupData);
groupList.groups.forEach((group) => {
if (group.name === name) {
callback(error, response, JSON.stringify({
channel: group,
}));
}
});
});
} else {
callback(error, response, data);
}
}
});
},
getChannelHistory(id, callback) {
slackRequest('channels.history', {
const endpoint = id.startsWith('G') ? 'groups.history' : 'channels.history';

slackRequest(endpoint, {
channel: id,
}, (error, response, data) => {
if (callback) {
Expand All @@ -76,7 +93,9 @@ module.exports = {
});
},
markChannel(id, timestamp, callback) {
slackRequest('channels.mark', {
const endpoint = id.startsWith('G') ? 'groups.mark' : 'channels.mark';

slackRequest(endpoint, {
channel: id,
ts: timestamp,
}, (error, response, data) => {
Expand Down