Skip to content

Commit 2dfe988

Browse files
Slack: move botToken to AuthHub when needed (#458)
* Slack: move `botToken` to AuthHub when needed * update * Slack (patch) move AuthHub code * deps * tests * remove engine dep change * Update package.json Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update test/slack/lib.test.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix rabbit! * 🐇 test * call AuthHub instead of engine * update tests * - register `/auth-hub/send-message` only in AuthHub - add `x-appmixer-version-slack` when calling authhub - tests * add version endpoint for AuthHub and comment out unused code * remove unused dependency 'form-data' from package-lock.json * update axios to version 1.8.4 and clean up package.json * update * update * deps --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent a2a68ab commit 2dfe988

File tree

12 files changed

+898
-3
lines changed

12 files changed

+898
-3
lines changed

package-lock.json

Lines changed: 190 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
"test-unit-coverage": "nyc --reporter=lcov --reporter=text-summary mocha --recursive --exit"
66
},
77
"devDependencies": {
8+
"@slack/web-api": "^7.3.1",
89
"axios": "^0.22.0",
910
"content-type": "^1.0.4",
1011
"eslint": "^8.43.0",
1112
"form-data": "4.0.0",
13+
"html-entities": "^1.2.0",
1214
"ip-address": "^10.0.1",
1315
"jmespath": "0.16.0",
1416
"json-pointer": "0.6.2",

src/appmixer/slack/bundle.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "appmixer.slack",
3-
"version": "4.1.3",
3+
"version": "4.1.4",
44
"engine": ">=6.0.0",
55
"changelog": {
66
"1.0.1": [
@@ -37,6 +37,9 @@
3737
"(breaking change) Added payload authentication for triggers. Will require setting `signingSecret` in the connector configuration.",
3838
"Added options to SendChannelMessage and SendPrivateChannelMessage to send messages as a bot user.",
3939
"Fixed and issue when NewChannelMessageRT trigger did not register messages containing some special UTF characters."
40+
],
41+
"4.1.4": [
42+
"Improve `botToken` handling in SendChannelMessage and SendPrivateChannelMessage components when using the default configuration."
4043
]
4144
}
4245
}

src/appmixer/slack/lib.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
const pathModule = require('path');
55
const Entities = require('html-entities').AllHtmlEntities;
66
const { WebClient } = require('@slack/web-api');
7+
// TODO: Uncomment when https://github.com/clientIO/appmixer-core/issues/2889 is fixed
8+
// const slackConnectorVersion = require('./bundle.json').version;
79

810
module.exports = {
911

@@ -24,16 +26,40 @@ module.exports = {
2426
if (asBot === true) {
2527
// Make sure the bot token is used.
2628
token = context.config?.botToken;
27-
if (!token) {
29+
if (!token && !context.config?.usesAuthHub) {
2830
throw new context.CancelError('Bot token is required for sending messages as bot. Please provide it in the connector configuration.');
2931
}
3032

3133
({ iconUrl, username } = context.messages.message.content);
3234
}
3335

3436
let entities = new Entities();
37+
38+
if (context.config?.usesAuthHub && asBot) {
39+
// Send into AuthHub route
40+
const authHubUrl = process.env.AUTH_HUB_URL + '/plugins/appmixer/slack/auth-hub/send-message';
41+
const { data } = await context.httpRequest({
42+
url: authHubUrl,
43+
method: 'POST',
44+
headers: {
45+
Authorization: `Bearer ${process.env.AUTH_HUB_TOKEN}`
46+
// 'x-appmixer-version-slack': slackConnectorVersion
47+
},
48+
data: {
49+
iconUrl,
50+
username,
51+
channelId,
52+
text: entities.decode(message)
53+
}
54+
});
55+
56+
return data;
57+
}
58+
3559
const web = new WebClient(token);
3660

61+
// Auth token or Bot token is set. AuthHub is not used.
62+
// Directly send as bot.
3763
const response = await web.chat.postMessage({
3864
icon_url: iconUrl,
3965
username,

src/appmixer/slack/list/SendPrivateChannelMessage/SendPrivateChannelMessage.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ module.exports = {
1515
return context.sendJson(message, 'newMessage');
1616
}
1717
};
18-

0 commit comments

Comments
 (0)