Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using authorize with user events and a user token. #680

Closed
5 of 9 tasks
mcharawi opened this issue Nov 5, 2020 · 4 comments
Closed
5 of 9 tasks

Using authorize with user events and a user token. #680

mcharawi opened this issue Nov 5, 2020 · 4 comments
Labels
question M-T: User needs support to use the project

Comments

@mcharawi
Copy link

mcharawi commented Nov 5, 2020

Description

Hello everyone. I'm building a slack app that will be installed on multiple workspaces outside of my organization, and I am trying to implement OAuth using bolt and the provided slack OAuth node module. However, I'm running into issues when I try to subscribe to events on behalf of the user installing the app. Specifically, when I subscribe to the message.im event on behalf of the user, i get [DEBUG] bolt-app Conversation context failed loading for ID: D01B25CPPFD, error: Conversation not found using the following server setup:

import { LogLevel, App } from '@slack/bolt';
import { InstallProvider } from '@slack/oauth';
import levelup from 'levelup';
import leveldown from 'leveldown';

const database = levelup(leveldown('./installationsdb'));

const installer = new InstallProvider({
    clientId: process.env.SLACK_CLIENT_ID,
    clientSecret: process.env.SLACK_CLIENT_SECRET,
    authVersion: 'v2',
    stateSecret: "abc-123",
    installationStore: {
        storeInstallation: async(installation) => {
            return await database.put(installation.team.id, JSON.stringify(installation));
        },
        fetchInstallation: async(InstallQuery) => {
            const res = await database.get(InstallQuery.teamId);
            return JSON.parse(res.toString());
        },
    },
});

const authorizeFn = async(authObject) => {
    const install = await installer.installationStore.fetchInstallation({
        teamId: authObject.teamId
    })
    	return {
    	  userToken: install.user.token,
    	  botId: install.bot.id,
    	  botUserId: install.bot.userId
    	};
}
const app = new App({
    signingSecret: process.env.SLACK_SIGNING_SECRET,
    logLevel: LogLevel.DEBUG,
    authorize: authorizeFn,
});

app.event('message.im', async ({ event, client }) => {
	console.log("Message Received");
  });

app.receiver.router.get('/slack/install', async(req, res, next) => {
    try {
        const url = await installer.generateInstallUrl({
            userScopes: ["im:history", "users:write", "channels:history", "channels:read", "reactions:read", "files:write", "groups:history", "im:read", "groups:write", "calls:write", "calls:read", "reminders:read", "pins:write", "users.profile:write", "users:read", "usergroups:write", "users.profile:read", "remote_files:read", "stars:write", "team:read", "remote_files:share", "stars:read", "search:read", "reminders:write", "usergroups:read", "pins:read", "mpim:write", "mpim:read", "reactions:write", "links:write", "channels:write", "chat:write", "dnd:read", "dnd:write", "emoji:read", "files:read", "groups:read", "identify", "users:read.email", "mpim:history", "links:read", "im:write"],
            scopes: ["app_mentions:read", "calls:read", "calls:write", "channels:history", "channels:join", "channels:manage", "channels:read", "chat:write", "chat:write.customize", "chat:write.public", "commands", "dnd:read", "emoji:read", "files:read", "files:write", "groups:history", "groups:read", "groups:write", "im:history", "im:read", "im:write", "incoming-webhook", "links:read", "links:write", "mpim:history", "mpim:read", "mpim:write", "pins:read", "pins:write", "reactions:read", "reactions:write", "reminders:read", "reminders:write", "remote_files:read", "remote_files:share", "remote_files:write", "team:read", "usergroups:read", "usergroups:write", "users.profile:read", "users:read", "users:read.email", "users:write", "workflow.steps:execute"],
        })
        res.send(`<a href=${url}><img alt=""Add to Slack"" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/[email protected] 2x" /></a>`);
    } catch (error) {
        console.log(error)
    }
});

app.receiver.router.get('/slack/oauth_redirect', async(req, res) => {
    await installer.handleCallback(req, res);
});

(async() => {
    await app.start(process.env.PORT || 3000);
})();

If anyone has any pointers on how to get this to work, it would be much appreciated. Thanks!

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

  • bug
  • enhancement (feature request)
  • question
  • documentation 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.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version: 2.4.1

node version: 14.15.0

OS version(s): Ubuntu 20

Steps to reproduce:

  1. Start bolt app using the above code block.
  2. Install the app using the /slack/install/ OAuth flow.
  3. Subscribe to the message.im event on behalf of the user who installed the app.
  4. Send a message in a direct message channel.

Expected result:

The callback attached to the message.im event handler to be called.

Actual result:

Error that the conversation cannot be found.

Attachments:

None.

@gitwave gitwave bot added untriaged question M-T: User needs support to use the project and removed untriaged labels Nov 5, 2020
@stevengill
Copy link
Member

Hey @mcharawi!

Firstly, you can ignore [DEBUG] bolt-app Conversation context failed loading for ID: D01B25CPPFD, error: Conversation not found. It isn't related to the OAuth code you are trying to implement.

Next, instead of using @slack/oauth package directly, you can access it via bolt-js. See https://slack.dev/bolt-js/concepts#authenticating-oauth to see it in action. When you are using the OAuth module (on its own or via bolt-js), you don't need to define authorize as the oauth module comes with its own authorize method.

@mcharawi
Copy link
Author

mcharawi commented Nov 6, 2020

Thanks @stevengill. But then why isn't the logging happening when the message.im event is received?

@mcharawi
Copy link
Author

mcharawi commented Nov 6, 2020

I took another look at the API documentation and I'm actually supposed to be listening for the message event and filtering on subtype. Thank you for your help!

@stevengill
Copy link
Member

Glad to hear you figured it out! Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project
Projects
None yet
Development

No branches or pull requests

2 participants