Writing a slack slash command in bolt / nodeJS api

Viewed 1168

I'm working on building a slash command for slack in Javascript/Bolt( https://slack.dev/bolt/concepts)

It works "sometimes" but not always:

  • [x] In a public channel
  • [ ] In a private channel
  • [ ] In a direct message

I'm using the bot token to authenticate but I'm pretty sure I'm running into some auth permission issues.

I'm trying to use the: chat.postEphemeral slack api to present an interactive display to my user.

In either the private or direct message situation my app is printing out a channel_not_found error which I assume is due to some permission error

[DEBUG]  WebClient:0 apiCall('chat.postEphemeral') start
[DEBUG]  WebClient:0 will perform http request
[DEBUG]  WebClient:0 http response received
[DEBUG]  bolt-app An API error occurred: channel_not_found

When things work correctly it looks like this:

[DEBUG]  WebClient:0 apiCall('chat.postEphemeral') start
[DEBUG]  WebClient:0 will perform http request
[DEBUG]  WebClient:0 http response received

My slash command code looks like this - basically a simple reply with hi to the command:

app.command("/wl", async ({
  command,
  ack
}) => {
  console.log(command)
  await ack()

  channel_id = command.channel_id
  user_id = command.user_id

  await app.client.chat.postEphemeral({
    token: BOT_TOKEN,
    channel: channel_id,
    user: user_id,
    text: "hi"
  });
});

I've added every single option in the Bot Scope of the OAUTH page - and nothing seems to have done what I'm looking for.

1) Is it possible to actually write a valid slash command in bolt that works ever 2) Is it possible to do this with just using the BOT token or do I need to use the user token 3) What else am I missing?

Thanks

2 Answers

I had the same problem. What helped me is getting the user_id from this command:

const result = await app.client.users.list({
    token: process.env.SLACK_BOT_TOKEN
});
console.log(result);

the result object will have an array of all the users - take the id from there. For allowing it make sure you have the users:read activated in the Oauth scope.

Eventually, this is what helped me sending DMs for users.

One of the cases where you get this error Error: An API error occurred: channel_not_found is when you try to send a message to the private channel where you are not invited.

Try to add/invite your bot to the private channel you want to send a message

Note: check if the relevant scopes are attached.

Thanks

Related