-
Notifications
You must be signed in to change notification settings - Fork 150
detectMention fails in multi-workspace OAuth mode, causing onNewMention to never fire when message.channels is subscribed #308
Description
Bug Description
When using multi-workspace OAuth (no defaultBotToken), detectMention() always returns false because adapter.botUserId is undefined. This is harmless when only app_mention events are subscribed (the Slack adapter sets isMention = true directly for app_mention events). However, when message.channels is also subscribed, the message event arrives before app_mention, wins the dedupe check, and routes to onNewMessage instead of onNewMention. The subsequent app_mention event is then silently dropped by deduplication.
The root cause is in detectMention() — it relies on adapter.botUserId which is only populated when defaultBotToken is configured (via auth.test in initialize()). In multi-workspace OAuth mode where tokens are per-workspace, _botUserId is never set from auth.test, and the per-context botUserId is not available during handleIncomingMessage.
Steps to Reproduce
- Create a
Chatinstance with multi-workspace OAuth (nodefaultBotToken) - Subscribe to both
message.channelsandapp_mentionin Slack Event Subscriptions - Register both
onNewMentionandonNewMessage(/.*/)handlers - @mention the bot in a public channel
- Observe that
onNewMentionhandler is never called;onNewMessagehandler fires instead
Expected Behavior
When a user @mentions the bot in a public channel, onNewMention should fire. The message event should be routed to onNewMention because the bot's user ID is present in the message text (<@BOT_USER_ID>).
The app_mention event being deduped is acceptable as long as the message event is correctly routed.
Actual Behavior
detectMention() returns false because adapter.botUserId is undefined in multi-workspace OAuth mode. The message event falls through to onNewMessage pattern matching (step 4 in the routing chain). The
app_mention event is then dropped by deduplication (same ts). The onNewMention handler never fires.
Code Sample
import {Chat} from 'chat';
import {createSlackAdapter} from '@chat-adapter/slack';
import {createRedisState} from '@chat-adapter/state-redis';
// Multi-workspace OAuth — no defaultBotToken
const bot = new Chat({
userName: 'mybot',
adapters: {
slack: createSlackAdapter({
signingSecret: process.env.SLACK_SIGNING_SECRET!,
clientId: process.env.SLACK_CLIENT_ID!,
clientSecret: process.env.SLACK_CLIENT_SECRET!,
encryptionKey: process.env.SLACK_ENCRYPTION_KEY!,
}),
},
state: createRedisState({url: process.env.REDIS_URL!}),
});
// This handler NEVER fires when message.channels is subscribed
bot.onNewMention(async (thread, message) => {
console.log('Mention received!'); // Never logged
await thread.post('Hello!');
});
// This handler catches everything, including mentions
bot.onNewMessage(/.*/, async (thread, message) => {
console.log('New message:', message.text); // Fires for mentions too
});Chat SDK Version
4.23.0
Node.js Version
No response
Platform Adapter
Slack
Operating System
None
Additional Context
No response