Skip to content

detectMention fails in multi-workspace OAuth mode, causing onNewMention to never fire when message.channels is subscribed #308

@mlenczewski-tidio

Description

@mlenczewski-tidio

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

  1. Create a Chat instance with multi-workspace OAuth (no defaultBotToken)
  2. Subscribe to both message.channels and app_mention in Slack Event Subscriptions
  3. Register both onNewMention and onNewMessage(/.*/) handlers
  4. @mention the bot in a public channel
  5. Observe that onNewMention handler is never called; onNewMessage handler 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions