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

Slack Event API Support v2 #89

Merged
merged 58 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
c5094d1
add support for send msgs as bot user
ewingrj Jan 12, 2018
2139a7c
got basic slack event bridging
ewingrj Jan 13, 2018
5797761
update readme
ewingrj Jan 13, 2018
86353f2
better file support & fix room registration bug
ewingrj Jan 13, 2018
a241862
fix bug when failed to enablePublicSharing
ewingrj Jan 14, 2018
d050846
small slack event handler refactor
ewingrj Jan 15, 2018
9c692b3
a bit more cleanup
ewingrj Jan 16, 2018
64b1f8d
be consistent w/ returns
ewingrj Jan 16, 2018
beb4aac
fix bug when inviting bot with a user on another HS
ewingrj Jan 18, 2018
6be1c78
fix some typos in if statements
ewingrj Jan 24, 2018
8e43fe2
Merge remote-tracking branch 'perissology/master' into hs/perissology…
Half-Shot Sep 24, 2018
8b808cf
Slack should be capitalised
Half-Shot Sep 24, 2018
cff4c09
Use const for imports and constants
Half-Shot Sep 24, 2018
191d857
Don't stick a delay in because the homeserver is racy
Half-Shot Sep 24, 2018
74a794d
Can't imagine only webhooks are allowed to be !ok
Half-Shot Sep 24, 2018
83be714
team_domain is no more, attempt to extract it via an API call.
Half-Shot Sep 24, 2018
bdfc5b2
Missing (
Half-Shot Sep 24, 2018
1392c31
Add extra scope required
Half-Shot Sep 24, 2018
c5e474a
new
Half-Shot Sep 24, 2018
ad0180e
Import rp
Half-Shot Sep 24, 2018
edb4ee6
Return a promise
Half-Shot Sep 24, 2018
9e9f5a2
main -> this
Half-Shot Sep 24, 2018
bc54e4e
Handle the promise
Half-Shot Sep 24, 2018
79632cb
Return the promise
Half-Shot Sep 24, 2018
a61575b
Log when trying to update a users profile
Half-Shot Sep 24, 2018
091233a
Log if failed to get user profile
Half-Shot Sep 24, 2018
c479979
Add additional scope dependencies
Half-Shot Sep 24, 2018
b17c1d0
Fix corrext substitution of < > and & in formatted bodies
Cadair Sep 26, 2018
f4f4db2
Fix file bridging with the new slack files
Cadair Sep 26, 2018
8b7b695
Strip out language specifier on the code tags
Cadair Sep 26, 2018
11a2999
Support all media types of attachments.
Half-Shot Sep 27, 2018
45b2608
Add size to all file types
Half-Shot Sep 27, 2018
b0fe94d
Document size
Half-Shot Sep 27, 2018
44e3b7d
Let's try using request promise
Half-Shot Sep 27, 2018
1ffd7b0
Try setting the header directly
Half-Shot Sep 27, 2018
0fbd4d7
Oops
Half-Shot Sep 27, 2018
c5ca926
Thumbnails
Half-Shot Sep 27, 2018
1ae5daa
indenting
Half-Shot Sep 27, 2018
c567927
We want to fetch all content
Half-Shot Sep 27, 2018
5ec13ef
Make some regexs constants
Half-Shot Sep 27, 2018
3f70865
Tidy readme up
Half-Shot Sep 27, 2018
24d94dd
Responses to feedback
Half-Shot Sep 27, 2018
8e856c3
indentation
Half-Shot Sep 27, 2018
1a36dc8
Replace string regardless with ID if it fails.
Half-Shot Sep 27, 2018
5be295e
Merge branch 'develop' into hs/perissology/slack-events
Half-Shot Sep 27, 2018
1f95220
Move logic to correct function
Half-Shot Sep 27, 2018
f3534e8
Fix typos
Half-Shot Sep 27, 2018
b9142df
Properly replace channel
Half-Shot Sep 27, 2018
e0b1a57
Reinstate getUserId
Half-Shot Sep 27, 2018
0d6a25a
Get the team for users
Half-Shot Sep 27, 2018
197e50c
More team rubbish
Half-Shot Sep 27, 2018
73814f9
Supply channel to getTeamDomainForMessage
Half-Shot Sep 27, 2018
e17eb34
dupe text field to markdownText
Half-Shot Sep 27, 2018
ec88b12
Count iterations
Half-Shot Sep 27, 2018
2cd84cd
Slack markdown is just wrong
Half-Shot Sep 27, 2018
d50bdf1
Merge remote-tracking branch 'origin/develop' into hs/perissology/sla…
Half-Shot Oct 5, 2018
d1a5cbf
Use Logging for Slack Events
Half-Shot Oct 5, 2018
87c6dad
Touchups
Half-Shot Oct 5, 2018
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
86 changes: 83 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ The bridge itself should now be running.

To actually use it, you will need to configure some linked channels.


Provisioning
------------

Expand All @@ -100,6 +99,87 @@ first the individual Matrix room and Slack channel need to be created, and then
a command needs to be issued in the administration console room to add the link
to the bridge's database.

There are 2 ways to bridge a room. The recommended way uses the newer Slack events api
and bot users. This allows you to link as many channels as you would like with only
1 Slack integration. The legacy way uses incoming/outgoing webhooks, and requires
2 Slack integrations per channel to be bridged.

### Recommended - Events API

1. Add a custom app to your Slack team/workspace by visiting https://api.slack.com/apps
and clicking on `Create New App`.

2. Name the app & select the team/workspace this app will belong to.

3. Click on `bot users` and add a new bot user. We will use this account to bridge the
the rooms.

4. Click on `Event Subscriptions` and enable them. At this point, the bridge needs to be
started as Slack will do some verification of the request rul. The request url should be
`https://$HOST:$SLACK_PORT"`. Then add the following events and save:

Bot User Events:

- team_domain_change
- message.channels
- chat:write:bot
- message.groups (if you want to bridge private channels)
- users:read
- team.info

5. Skip this step if you do not want to bridge files.
Click on `OAuth & Permissions` and add the following scopes:

- files:write:user

Note: any media uploaded to matrix is currently accessible by anyone who knows the url.
turt2live marked this conversation as resolved.
Show resolved Hide resolved
In order to make Slack files visible to matrix users, this bridge will make Slack files
visible to anyone with the url (including files in private channels). This is different
then the current behavior in Slack, which only allows authenticated access to media
posted in private channels. See [MSC701](https://github.com/matrix-org/matrix-doc/issues/701)
for details.

6. Click on `Install App` and `Install App to Workspace`. Note the access tokens show.
You will need the `Bot User OAuth Access Token` and if you want to bridge files, the
`OAuth Access Token` whenever you link a room.

7. For each channel you would like to bridge, perform the following steps:

1. Create a Matrix room in the usual manner for your client. Take a note of its
Matrix room ID - it will look something like `!aBcDeF:example.com`.

2. invite the bot user to the Slack channel you would like to bridge.

```
/invite @bot-user-name
```

You will also need to determine the "channel ID" that Slack uses to identify
the channel, which can be found in the url `https://XXX.slack.com/messages/<channel id>/`.

2. Issue a ``link`` command in the administration control room with these
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're going to duplicate list item numbers, can we use 1. please?

collected values as arguments:

with file bridging:

```
link --channel_id CHANNELID --room !the-matrix:room.id --slack_bot_token xoxb-xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx --slack_user_token xoxp-xxxxxxxx-xxxxxxxxx-xxxxxxxx-xxxxxxxx
```
without file bridging:

```
link --channel_id CHANNELID --room !the-matrix:room.id --slack_bot_token xoxb-xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx
```

These arguments can be shortened to single-letter forms:

```
link -I CHANNELID -R !the-matrix:room.id -t xoxb-xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx
```


### Legacy - Webhooks

1. Create a Matrix room in the usual manner for your client. Take a note of its
Matrix room ID - it will look something like `!aBcDeF:example.com`.

Expand All @@ -123,13 +203,13 @@ to the bridge's database.
collected values as arguments:

```
link --channel CHANNELID --room !the-matrix:room.id --token THETOKEN --webhook_uri http://the.webhook/uri
link --channel_id CHANNELID --room !the-matrix:room.id --webhook_url https://hooks.slack.com/services/ABC/DEF/123
```

These arguments can be shortened to single-letter forms:

```
link -c CHANNELID -r !the-matrix:room.id -t THETOKEN -u http://the.webhook/uri
link -I CHANNELID -R !the-matrix:room.id -u https://hooks.slack.com/services/ABC/DEF/123
```

See also https://github.com/matrix-org/matrix-appservice-bridge/blob/master/HOWTO.md for the general theory of all this :)
Expand Down
15 changes: 13 additions & 2 deletions lib/AdminCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,31 @@ adminCommands.link = new AdminCommand({
required: true,
},
webhook_url: {
description: "Slack webhook URL",
description: "Slack webhook URL. Used with slack outgoing hooks integration",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/slack/Slack/

aliases: ['u'],
},
slack_bot_token: {
description: "Slack bot user token. Used with slack bot user & Events api",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/slack/Slack/

aliases: ['t'],
},
slack_user_token: {
description: "Slack user token. Used to bridge files",
}
},
func: function(main, opts, args, respond) {
return main.actionLink({
matrix_room_id: opts.room,
slack_channel_name: opts.channel,
slack_channel_id: opts.channel_id,
slack_webhook_uri: opts.webhook_url,
slack_bot_token: opts.slack_bot_token,
slack_user_token: opts.slack_user_token,
}).then(
(room) => {
respond("Room is now " + room.getStatus());
respond("Inbound URL is " + main.getInboundUrlForRoom(room));
if (room.getSlackWebhookUri()) {
respond("Inbound URL is " + main.getInboundUrlForRoom(room));
}
},
(e) => { respond("Cannot link - " + e ) }
);
Expand Down
207 changes: 207 additions & 0 deletions lib/BaseSlackHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
"use strict";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... do we have to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently we do everywhere else 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will hold back the tears


const rp = require('request-promise');
const Promise = require('bluebird');
const promiseWhile = require("./promiseWhile");
const getSlackFileUrl = require("./substitutions").getSlackFileUrl;

const CHANNEL_ID_REGEX = /<#(\w+)\|?\w*?>/g;
const CHANNEL_ID_REGEX_FIRST = /<#(\w+)\|?\w*?>/;

// (if this is an emote msg, the format is <@ID|nick>, but in normal msgs it's just <@ID>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment references 'this' which makes no sense up here

const USER_ID_REGEX = /<@(\w+)\|?\w*?>/g;
const USER_ID_REGEX_FIRST = /<@(\w+)\|?\w*?>/;

/**
* @constructor
* @param {Main} main the toplevel bridge instance through which to
* communicate with matrix.
*/
function BaseSlackHandler(main) {
this._main = main;
}

BaseSlackHandler.prototype.replaceChannelIdsWithNames = function(message, token) {
var main = this._main;

// match all channelIds
var testForName = message.text.match(CHANNEL_ID_REGEX);
var iteration = 0;
var matches = 0;
if (testForName && testForName.length) {
matches = testForName.length;
}
return promiseWhile(function () {
// Do this until there are no more channel ID matches
return iteration < matches;
}, function () {
// foreach channelId, pull out the ID
// (if this is an emote msg, the format is <#ID|name>, but in normal msgs it's just <#ID>
var id = testForName[iteration].match(CHANNEL_ID_REGEX_FIRST)[1];
var channelsInfoApiParams = {
uri: 'https://slack.com/api/channels.info',
qs: {
token: token,
channel: id
},
json: true
};
main.incRemoteCallCounter("channels.info");
return rp(channelsInfoApiParams).then((response) => {
let name = id;
if (response && response.channel && response.channel.name) {
console.log("channels.info: " + id + " mapped to " + response.channel.name);
name = response.channel.name;
}
else {
console.log("channels.info returned no result for " + id);
}
message.text = message.text.replace(CHANNEL_ID_REGEX_FIRST, "#" + name);
iteration++;
}).catch((err) => {
console.log("Caught error " + err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also helpful error messages would be awesome :)

});
}).then(() => {
// Notice we can chain it because it's a Promise,
// this will run after completion of the promiseWhile Promise!
return message;
});
};

BaseSlackHandler.prototype.replaceUserIdsWithNames = function(message, token) {
var main = this._main;

// match all userIds
var testForName = message.text.match(USER_ID_REGEX);
var iteration = 0;
var matches = 0;
if (testForName && testForName.length) {
matches = testForName.length;
}
return promiseWhile(() => {
// Condition for stopping
return iteration < matches;
}, function () {
// foreach userId, pull out the ID
// (if this is an emote msg, the format is <@ID|nick>, but in normal msgs it's just <@ID>
turt2live marked this conversation as resolved.
Show resolved Hide resolved
var id = testForName[iteration].match(USER_ID_REGEX_FIRST)[1];
var usersInfoApiParams = {
uri: 'https://slack.com/api/users.info',
qs: {
token: token,
user: id
},
json: true
};
main.incRemoteCallCounter("users.info");
let response;
iteration++;
return rp(usersInfoApiParams).then((res) => {
// Technically the function only requires the team_id, so
// pass in the response to get user instead.
// Though obviously don't if the response was wrong.
response = res;
return main.getTeamDomainForMessage(
{
team_id: (res && res.user ? res.user : message).team_id,
channel: message.channel,
}
);
}).then((team_domain) => {
const user_id = main.getUserId(id, team_domain);
if (response && response.user && response.user.name) {
console.log("users.info: " + id + " mapped to " + response.user.name);
const pill = `<https://matrix.to/#/${user_id}|${response.user.name}>`;
message.text = message.text.replace(USER_ID_REGEX_FIRST, response.user.name);
message.markdownText = message.markdownText.replace(
USER_ID_REGEX_FIRST,
pill
);
return;
}
console.log(`users.info returned no result for ${id} Response:`, response);
// Fallback to checking the user store.
var store = this.getUserStore();
return store.select({id: user_id});
}).then((result) => {
if (result === undefined) {
return;
}
let name = user_id;
console.log(`${user_id} did ${result.length > 0 ? "not" : ""} an entry`);
if (result.length) {
// It's possible not to have a displayname set.
name = result[0].display_name || result[0].id;
}
message.text = message.text.replace(USER_ID_REGEX_FIRST, name);
message.markdownText = message.markdownText.replace(
USER_ID_REGEX_FIRST,
`<https://matrix.to/#/${user_id}|${name}>`
);
}).catch((err) => {
console.log("Caught error " + err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes the catch to swallow and trap the error, treating the chain as successful - is that intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, if we fail to get one user ID, we don't want to stop there and not get the others.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also get a more descriptive error message on this?

});
}).then(() => {
// Notice we can chain it because it's a Promise,
// this will run after completion of the promiseWhile Promise!
return message;
});
};


/**
* Enables public sharing on the given file object. then fetches its content.
*
* @param {Object} file A slack 'message.file' data object
* @param {string} token A slack API token that has 'files:write:user' scope
* @return {Promise<Object>} A Promise of the updated slack file data object
*/
BaseSlackHandler.prototype.enablePublicSharing = function (file, token) {
if (file.public_url_shared) return Promise.resolve(file);

this._main.incRemoteCallCounter("files.sharedPublicURL");
return rp({
method: 'POST',
form: {
file: file.id,
token: token,
},
uri: "https://slack.com/api/files.sharedPublicURL",
json: true
}).then((response) => {
if (!response || !response.file || !response.file.permalink_public) {
console.log("Could not find sharedPublichURL: " + JSON.stringify(response));
return undefined;
}

return response.file;
});
}

/**
* Fetchs the file at a given url.
*
* @param {Object} file A slack 'message.file' data object
* @return {Promise<string>} A Promise of file contents
*/
BaseSlackHandler.prototype.fetchFileContent = function (file, token) {
if (!file) return Promise.resolve();

const url = getSlackFileUrl(file) || file.permalink_public;
if (!url) {
return Promise,reject("File doesn't have any URLs we can use.");
}

return rp({
uri: url,
resolveWithFullResponse: true,
encoding: null
}).then((response) => {
var content = response.body;
console.log("Successfully fetched file " + file.id +
" content (" + content.length + " bytes)");
return content;
});
};

module.exports = BaseSlackHandler;
Loading