bolt-js icon indicating copy to clipboard operation
bolt-js copied to clipboard

Can I use both userToken and botToken when instancing SlackApp?

Open chsmendes opened this issue 1 year ago • 5 comments

@slack/bolt version

3.13.2

Your App and Receiver Configuration

import { Environment } from '@/infra/environment'
import { Injectable } from '@nestjs/common'
import { App as SlackApp, LogLevel } from '@slack/bolt'

@Injectable()
export class SlackFactory {
  private readonly _app: SlackApp

  constructor(private readonly _environment: Environment) {
    this._app = new SlackApp({
      signingSecret: this._environment.slackSigningSecret,
      token: this._environment.slackUserToken,
      appToken: this._environment.slackSocketToken,
      socketMode: true,
      logLevel: LogLevel.ERROR as LogLevel,
      processBeforeResponse: true
    })
    this._app.start()
  }

  getApp(): SlackApp {
    return this._app
  }
}

Node.js runtime version

18

I'm using this app to handle user commands in Slack.

Currently, my app is configured to use the slackUserToken. However, I’ve encountered an issue:

I want to use the chat.postEphemeral method to send an ephemeral message to a specific user in a channel. The problem is that the slackUserToken cannot have the chat:write.public scope, which is required to send ephemeral messages to channels where the bot is not a member. As a result, I receive a not_in_channel error unless I invite the bot to the channel beforehand.

As a workaround, to avoid having to invite the bot user to channel, I updated my SlackFactory to use the slackBotToken instead. This allows me to apply the chat:write.public scope and use the chat.postEphemeral method without issues, now worked fine.

However, my app also uses other methods, such as admin.conversations.convertToPrivate, which only work with the slackUserToken.

What would be the best way to handle this situation? Do you have any suggestions for managing both tokens effectively?

chsmendes avatar May 13 '25 21:05 chsmendes

Hi, @chsmendes! Thanks for submitting this question.

You can actually pass in tokens as overrides when you call certain API methods. You can store the user token as an environment variable and initialize your app with the bot token. Then, you can manually pass it in when you call the API under the token property. (You'll need to make sure you have the proper scopes added to this user token!)

More info can be found in this answer here.

hello-ashleyintech avatar May 13 '25 22:05 hello-ashleyintech

Hi @hello-ashleyintech , thanks for support. I've tried pass token manually as mentioned in a method which convert channel to private:

 public async handle({ channelsIds }: { channelsIds: string[] }): Promise<string[]> {
    const channelsIdsError: string[] = []

    for (const channelId of channelsIds) {
      try {
        await this._slackFactory.client.admin.conversations.convertToPrivate({
          channel_id: channelId,
          token: this._environment.slackUserToken
        })
      } catch (error) {
        channelsIdsError.push(`<#${channelId}>`)
        this._logger.error({
          message: `${messages.convertChannelError} - ${channelId}`,
          error
        })
      }
    }

    return channelsIdsError
  }

But when initializing the app I used botToken, and passing manually like above not worked, it keep using botToken and doest no overrides, keeping the error. Any idea to solve this? Thanks

chsmendes avatar May 14 '25 14:05 chsmendes

👋 I just confirmed using a user token will convert a public channel to private, but if the provided token is undefined this method will fallback to the configured bot token.

@chsmendes Is it possible to check that this._environment.slackUserToken is set to a token prefixed with xoxp?

I will also suggest updating to the latest version of @slack/bolt - this is now 4.4.0 - since a bug fix might've landed since 3.13.2 that we cannot offer support for. 🤖

zimeg avatar May 22 '25 02:05 zimeg

👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.

github-actions[bot] avatar Jun 23 '25 00:06 github-actions[bot]

As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.

github-actions[bot] avatar Jul 07 '25 00:07 github-actions[bot]