question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`fetchInstallation` never called and bot doesn't react to messages?

See original GitHub issue

Description

I am trying to set up a Slack Bolt app using the OAuth mechanism as described in https://slack.dev/bolt-js/concepts#authenticating-oauth but it seems like the authentication never finishes.

My implementation looks like this

export class SlackService {
  slackApp: App;

  constructor() {
    this.slackApp = new App({
      signingSecret: process.env.SLACK_SIGNING_SECRET,
      clientId: process.env.SLACK_CLIENT_ID,
      clientSecret: process.env.SLACK_CLIENT_SECRET,
      stateSecret: process.env.SLACK_STATE_SECRET,
      scopes: [
        'chat:write',
        'channels:read',
        'channels:history',
        'im:read',
        'im:write',
        'incoming-webhook',
      ],
      logLevel: LogLevel.DEBUG,
      installationStore: {
        storeInstallation: async (installation) => {
          console.log('store installation');
          return await database.set(installation.team.id, installation);
        },
        fetchInstallation: async (InstallQuery) => {
           console.log('fetch Installation');
           return await database.get(InstallQuery.teamId);
        },
      },
    });

    this.bootstrap();
  }

  async bootstrap() {
    await this.slackApp.start(process.env.SLACK_APP_PORT);
    this.slackApp.message(':wave:', async ({ message, say }) => {
      console.log('got message');
      await say(`Hello, <@${message.user}>`);
    });
    this.slackApp.client.chat.postMessage({...});
    console.log('⚡️ Slack app is running!');
  }
}

The part with storing the installation works, so store installation shows up in the console once I finish the installation. Also the data that I get in store installation seems valid, I tested the webhook url and it works fine when I just use it in my HTTP requests.

But what’s weird is that fetchInstallation never gets called, and my app never reacts to the :wave: message. The line this.slackApp.client.chat.postMessage({...}); throws an error:

(node:67243) UnhandledPromiseRejectionWarning: Error: An API error occurred: not_authed

Is there something I forgot to add? Unfortunately I can’t find any more information in the docs.

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I’ve read and understood the Contributing guidelines and have done my best effort to follow them.
  • I’ve read and agree to the Code of Conduct.
  • I’ve searched for any related issues and avoided creating a duplicate issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
seratchcommented, Feb 19, 2021

@Evilweed This may be a bit confusing but the slackApp.client does not hold a token (even when you pass a bot token in App constructor). Thus, you need to pass token as an argument in the chat.postMessage method call this way:

await this.slackApp.client.chat.postMessage({
  token: process.env.SLACK_BOT_TOKEN,
  channel: "#random", // using a channel ID is better
  text: "Hi there!"
})

In the case of @zwenza’s app, installationStore does not work outside App’s event listeners. If the app needs to post a message when bootstrapping, picking one bot token up to post the message would be the way to go.

I think this comment answered both questions. Let me close this issue now. If you have any further questions, please feel free to write in here or create a new question type issue.

2reactions
TheJimFactorcommented, Dec 31, 2021

Just because it wasn’t immediatly obvious where fetchInstallation could be called from I wrote an example script that uses postMessage and fetchInstallation

app.command('/testcommand', async({ command, ack, respond }) =>{
  const installation = await app.receiver.installer.installationStore.fetchInstallation({teamId:command.team_id})
  await app.client.chat.postMessage({
    token:installation.bot.token,
    channel:'channelidhere',
    text:`<@${command.user_id}> used the /testcommand with text: ${command.text}!`
  })

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

discord.js bot doesn't react to message at all - Stack Overflow
I've been looking for it for a while on old posts couldn't find anything new. Is this code outdated or is there like...
Read more >
Reactions - discord.js Guide
Listening for reactions on old messages​​ Messages sent before your bot started are uncached unless you fetch them first. By default, the library ......
Read more >
Enabling interactions with bots - Slack API
Installing the bot to a workspace. A bot user is added to a workspace by installing the app that the bot is associated...
Read more >
Bolt for Python - Slack Platform Developer Tools
This allows your app to take action when something happens in a workspace where it's installed, like a user reacting to a message...
Read more >
Troubleshoot Your Bot - Salesforce Help
A great way to get started is to learn how to use the Event Logs to troubleshoot. ... Bot features, input from Chat...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found