Skip to content

Commit 2f2719a

Browse files
committed
pass through as_user unless false and a bot token is available
This allows us to support both existing and new Slack connections created with Slack's v2 OAuth flow, with or without the deprecated perspectival chat:write:user and chat:write:bot scopes: | Slack auth | Token Types | Scopes | as_user Values | |------------|-------------|-----------------------|-----------------| | v1 | user | chat:write:bot, :user | true, false | | v2 | user, bot | chat:write:bot, :user | true, false | | v2 | user, bot | chat:write | true | In v2, when the chat:write scope is requested, it replaces chat:write:bot and chat:write:user. Without the chat:write:bot scope, as_user cannot be false; user tokens always post as the user, and bot tokens always post as the bot. In v2, with or without the chat:write:bot scope, we can use the bot token if as_user is false since it will have permission to post as itself. And we MAY pass through as_user if it's true since Slack allows it even when it's superfluous. However, we MUST pass through as_user if it's true AND the user token still has the chat:write:bot scope since otherwise the message will post as the bot user instead of the user. See: https://docs.slack.dev/reference/methods/chat.postMessage/#legacy_as_user
1 parent e68de1f commit 2f2719a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

components/slack/slack.app.mjs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ export default {
295295
page: page + 1,
296296
count: constants.LIMIT,
297297
throwRateLimitError: true,
298-
as_bot: true,
299298
});
300299
return files?.map(({
301300
id: value, name: label,
@@ -490,7 +489,8 @@ export default {
490489
return this.$auth.oauth_uid;
491490
},
492491
getToken(opts = {}) {
493-
return (opts.as_bot && this.$auth.bot_token)
492+
// Use bot token if asBot is true and available, otherwise use user token.
493+
return (opts.asBot && this.$auth.bot_token)
494494
? this.$auth.bot_token
495495
: this.$auth.oauth_access_token;
496496
},
@@ -530,14 +530,23 @@ export default {
530530
});
531531
},
532532
async makeRequest({
533-
method = "", throwRateLimitError = false, as_user, as_bot, ...args
533+
method = "", throwRateLimitError = false, asBot = false, as_user, ...args
534534
} = {}) {
535-
as_bot = as_user === false || as_bot;
535+
const botTokenAvailable = Boolean(this.$auth.bot_token);
536+
// Passing as_user as false with a v2 user token lacking the deprecated
537+
// `chat:write:bot` scope, results in an error. So if as_user is false and
538+
// there's a bot token available, we should use the bot token and omit
539+
// as_user. Otherwise, use the user token and pass as_user through.
540+
if (as_user === false && botTokenAvailable) {
541+
asBot = true;
542+
} else {
543+
args.as_user = as_user;
544+
}
536545

537546
const props = method.split(".");
538547
const sdk = props.reduce((reduction, prop) =>
539548
reduction[prop], this.sdk({
540-
as_bot,
549+
asBot,
541550
}));
542551

543552
let response;
@@ -547,7 +556,7 @@ export default {
547556
if ([
548557
"not_in_channel",
549558
"channel_not_found",
550-
].includes(error?.data?.error) && as_bot) {
559+
].includes(error?.data?.error) && asBot) {
551560
// If method starts with chat, include the part about "As User"
552561
// Otherwise, just say "Ensure the bot is a member of the channel"
553562
if (method.startsWith("chat.")) {
@@ -673,7 +682,7 @@ export default {
673682
const {
674683
bot_id, user_id,
675684
} = await this.authTest({
676-
as_bot: true,
685+
asBot: true,
677686
});
678687
if (!bot_id) {
679688
throw new Error("Could not get bot ID. Make sure the Slack app has a bot user.");
@@ -932,7 +941,9 @@ export default {
932941
args.count ||= constants.LIMIT;
933942
return this.makeRequest({
934943
method: "files.list",
935-
as_bot: true,
944+
// Use bot token, if available, since the required `files:read` scope
945+
// is only requested for bot tokens in the Pipedream app.
946+
asBot: true,
936947
...args,
937948
});
938949
},
@@ -946,7 +957,9 @@ export default {
946957
getFileInfo(args = {}) {
947958
return this.makeRequest({
948959
method: "files.info",
949-
as_bot: true,
960+
// Use bot token, if available, since the required `files:read` scope
961+
// is only requested for bot tokens in the Pipedream app.
962+
asBot: true,
950963
...args,
951964
});
952965
},

0 commit comments

Comments
 (0)