slackapi/bolt-js

appToken is not read in socketMode @3.18.0

Closed this issue · 4 comments

@slack/bolt version

3.18.0

Your App and Receiver Configuration

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  appToken: process.env.SLACK_APP_TOKEN,
  socketMode: true,
})

Node.js runtime version

Node.js v20.12.2

Steps to reproduce:

1.Run it with node filename

const { App } = require('@slack/bolt')


const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  appToken: process.env.SLACK_APP_TOKEN,
  socketMode: true,
})

// Listening to messages in channels or direct messages
app.message(async ({ message, say }) => {
  console.log(message)
  // Ensure the message is not from a bot to avoid infinite loops
  if (message.subtype && message.subtype === 'bot_message') {
    return
  }
  await say(`Echo: ${message.text}`)
})

// Listening for app mentions
app.event('app_mention', async ({ event, say }) => {
  await say(`Thanks for the mention, <@${event.user}>!`)
});

(async () => {
  // Start your app
  await app.start()
  console.log('⚡️ Bolt app is running!')
})()

Expected result:

The app works

Actual result:

throw new errors_1.AppInitializationError('You must provide an appToken when socketMode is set to true. To generate an appToken see: https://api.slack.com/apis/connections/socket#token');

I console.logged the env vars to make sure they are read. I also created a python app with exactly the same .env vars and it does work.

import os

from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

# Install the Slack app and get xoxb- token in advance
app = App(token=os.environ["SLACK_BOT_TOKEN"])


#Message handler for Slack
@app.message(".*")
def message_handler(message, say, logger):
   print(message)
   
   say('hi back')

if __name__ == "__main__":
   SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()

Hi @headzik, thanks for asking the question! I've checked if your code example works for me, and I didn't see any issues with the SLACK_APP_TOKEN resolution using bolt-js 3.18.0. Probably there is still something wrong with your settings when running your app. I hope you'll find the cause out soon.

Thanks for the quick response. What could it be then? Why would it be settings of my app if it works in Python with the same token? What node version do you have?

What node version do you have?

I ran the code with Node 20 but it should not matter for this.

Why would it be settings of my app if it works in Python with the same token?

This is not an issue related to the validity of token. The token is not loaded in your Node.js app. As you can see here, the error can be thrown only when the appToken parameter is missing. You might set SLACK_APP_TOKEN variable in your shell session, but the data might not be loaded within your Node.js app for some reason (e.g., dotenv loads different set of data).

Ok, it works. So it did not work when I used bun even though my vars were loaded, when I changed to node the values were not loaded. I guess I cannot use bun for it. I don't know enough to understand why the different behaviour.

Thanks for your time and sorry for wasting it :).