Search code examples
node.jsslackslack-api

Direct Messaging custom slack bot built on node.js


I have built a slack bot using the slack/bots apis in node.js: https://slack.dev/bolt-js/tutorial/getting-started

Currently, it is working fine when I type <bot> help in a channel I have set up for using webhooks. I am trying to run those same commands in a DM with the bot using the app.event('app_mention',...) method but it is not working. its like the message doesn't register in a DM with the bot for some reason but it works in a public channel. code snippet below:

app.event('app_mention', async ({ event, client}) => {
    console.log(event);
    const text = event.text;
    const parentMessageId = event.ts;
    const eventChannel = event.channel;
    if (text.includes("help")) {
        console.log(event);
        try {
            await client.chat.postMessage({
                channel: eventChannel,
                text: helpMessage,
                thread_ts: parentMessageId
            });
        } catch (err) {
            console.error(err);
        }

I should have permissions set up correctly as well. I basically have all the permissions that I can add for the bot


Solution

  • The documentation of the app_mention api specifically mentions that this event does not work with DMs.

    Messages sent to your app in direct message conversations are not dispatched via app_mention, whether the app is explicitly mentioned or otherwise. Subscribe to message.im events to receive messages directed to your bot user in direct message conversations.

    Check here : https://api.slack.com/events/app_mention