bolt-js: An unhandled error occurred while Bolt processed an event

We are facing the following issue with the Slack app when we are trying to add the app to another workspace (different from the one where it was created). Once the authorization is given, we are able to read all the credentials including team id, access token, etc. However, when we try to use the same credentials to access the workspace for any activity,

we are getting the following error:

2023-03-20T18:07:34: [ERROR] An unhandled error occurred while Bolt processed an event
2023-03-20T18:07:34: [DEBUG] Error details: TypeError: Cannot read property 'teamId' of undefined, storedResponse: undefined

I think getting this response from Slack bolt,

In the first line, I’m printing the teamId and same also passing it to the bolt lib(authorizer),

0|slack-app | 2023-03-20T18:10:58: TQXMVxxxx
0|slack-app | 2023-03-20T18:10:58: [ERROR] An unhandled error occurred while Bolt processed an event
0|slack-app | 2023-03-20T18:10:58: [DEBUG] Error details: TypeError: Cannot read property 'teamId' of undefined, storedResponse: undefined

Code:

const authorizeFn = async ({ teamId, enterpriseId }) => {
    console.log(teamId);
    // Fetch team info from database
    db.mongoose
        .connect(process.env.DB_CONNECTION_STRING, {
            useNewUrlParser: true,
            useUnifiedTopology: true
        })
        .then(() => {
            console.log("Connected to the database!");
        })
        .catch(err => {
            console.log("Cannot connect to the database!", err);
            process.exit();
        });
    Slackbot.find({ "access_details.team.id": teamId })
        .then(data => {
            if (!data)
                console.log("Not found slack user with team " + teamId);
            else {
                console.log(data[0].access_details);
                return {
                    // You could also set userToken instead
                    botToken: data[0].access_details.access_token,
                    botId: data[0].access_details.app_id,
                    botUserId: data[0].access_details.bot_user_id
                };
            }
        })
        .catch(err => {
            console.log("Error retrieving slack with team=" + teamId);
        });
}

// Initializes your app with your bot token and app token
const app = new App({
    appToken: process.env.SLACK_APP_TOKEN,
    signingSecret: process.env.SLACK_SIGNING_SECRET,
    logLevel: LogLevel.DEBUG,
    customRoutes: customRoutes.customRoutes,
    processBeforeResponse: true,
    stateSecret: 'my-state-secret',
    scopes: [process.env.SLACK_SCOPES],
    authorize: authorizeFn
});

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Added the socket mode also, @filmaj

const app = new App({
    appToken: process.env.SLACK_APP_TOKEN,
    signingSecret: process.env.SLACK_SIGNING_SECRET,
    logLevel: LogLevel.DEBUG,
    customRoutes: customRoutes.customRoutes,
    scopes: [process.env.SLACK_SCOPES],
    authorize: authorizeFn,
    developerMode: true,
    socketMode: false
});
0|slack-app  | 2023-03-21T21:48:31: [DEBUG]  bolt-app {"token":"4sND1EQy5IGQaJGjvIguLynn","team_id":"TQXMxxxxx","context_team_id":"TQXMxxxxx","context_enterprise_id":null,"api_app_id":"A04FLDxxxxx","event":{"client_msg_id":"xxxxx-1a99-45e8-a7ef-94ecce2033ae","type":"message","text":"hifives","user":"U01Vxxxxxxx","ts":"1679415510.997069","blocks":[{"type":"rich_text","block_id":"gz3OT","elements":[{"type":"rich_text_section","elements":[{"type":"text","text":"hifives"}]}]}],"team":"TQXMxxxxx","channel":"D04U2xxxxxx","event_ts":"1679415510.997069","channel_type":"im"},"type":"event_callback","event_id":"Ev04VC8Z2K2M","event_time":1679415510,"authorizations":[{"enterprise_id":null,"team_id":"TQXMxxxxx","user_id":"U04U2xxxxxx","is_bot":true,"is_enterprise_install":false}],"is_ext_shared_channel":false,"event_context":"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUUVhNVlNIUTgiLCJhaWQiOiJBMDRGTEQyVTkwVCIsImNpZCI6IkQwNFUyVFBHWTlaIn0"}
0|slack-app  | 2023-03-21T21:48:31: TQXMxxxxx
0|slack-app  | 2023-03-21T21:48:31: Connected to the database!
0|slack-app  | 2023-03-21T21:48:32: {
0|slack-app  | 2023-03-21T21:48:32:   ok: true,
0|slack-app  | 2023-03-21T21:48:32:   app_id: 'A04xxxxx',
0|slack-app  | 2023-03-21T21:48:32:   authed_user: { id: 'U01V2xxxxx' },
0|slack-app  | 2023-03-21T21:48:32:   scope: 'team:read,app_mentions:read,channels:history,chat:write,commands,im:history,im:read,im:write,users.profile:read,users:read,users:read.email',
0|slack-app  | 2023-03-21T21:48:32:   token_type: 'bot',
0|slack-app  | 2023-03-21T21:48:32:   access_token: 'xoxb-xxxxxxx-xxxxxx-xxxxxxxx',
0|slack-app  | 2023-03-21T21:48:32:   bot_user_id: 'U04U2xxxxxx',
0|slack-app  | 2023-03-21T21:48:32:   team: { id: 'TQXMxxxxx', name: 'xxxxxx' },
0|slack-app  | 2023-03-21T21:48:32:   enterprise: null,
0|slack-app  | 2023-03-21T21:48:32:   is_enterprise_install: false
0|slack-app  | 2023-03-21T21:48:32: }
0|slack-app  | 2023-03-21T21:48:32: [ERROR]   An unhandled error occurred while Bolt processed an event
0|slack-app  | 2023-03-21T21:48:32: [DEBUG]   Error details: TypeError: Cannot read property 'teamId' of undefined, storedResponse: undefined