Files
botpress--botpress/integrations/slack/src/webhook-events/handlers/oauth-callback.ts
T
2026-07-13 13:34:48 +08:00

23 lines
997 B
TypeScript

import * as sdk from '@botpress/sdk'
import { SlackClient } from 'src/slack-api'
import { getAppCredentials } from 'src/slack-api/slack-manifest-client'
import * as bp from '.botpress'
export const isOAuthCallback = (req: sdk.Request): req is sdk.Request & { path: '/oauth' } => req.path === '/oauth'
export const handleOAuthCallback = async ({ req, client, ctx, logger }: bp.HandlerProps) => {
logger.forBot().debug('OAuth callback handled in webhook handler, redirecting to end step')
const query = new URLSearchParams(req.query)
const code = query.get('code')
if (!code || typeof code !== 'string') {
throw new Error('Handler received an empty code')
}
const slackClient = await SlackClient.createFromAuthorizationCode({ client, ctx, logger, authorizationCode: code })
const appCreds = await getAppCredentials(client, ctx)
const identifier = appCreds.clientId ? slackClient.getBotUserId() : slackClient.getTeamId()
await client.configureIntegration({ identifier })
}