nest-slack-bolt
nest-slack-bolt copied to clipboard
How to apply the Event Subscription URL.
To apply Event Subscriptions, you need to implement slack/events, but I need example code on how to set this up in the package.
import { App, Receiver, ReceiverEvent } from '@slack/bolt';
import { Injectable } from '@nestjs/common';
import { Request, Response } from 'express';
@Injectable()
export class AppReceiver implements Receiver {
private boltApp: App;
public init(boltApp: App): void {
this.boltApp = boltApp; // Slack Bolt 앱 인스턴스 저장
}
public async start(): Promise<void> {}
public async stop(): Promise<void> {}
public async handleRequest(req: Request, res: Response): Promise<void> {
const { body } = req;
const event: ReceiverEvent = {
body,
ack: async (response) => {
if (response) {
res.status(200).json(response);
} else {
res.status(200).send();
}
},
};
try {
await this.boltApp.processEvent(event);
} catch (error) {
console.error('Error processing Slack event:', error);
res.status(500).send('Internal Server Error');
}
}
}
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SlackModule } from 'nestjs-slack-bolt';
import { AppReceiver } from './app.receiver';
@Module({
imports: [
SlackModule.forRoot({
token: 'xxxxx',
appToken: 'xxxxx',
clientId: 'xxxxxx',
clientSecret: 'xxxxxx',
signingSecret: 'xxxxxx',
socketMode: false,
receiver: new AppReceiver(),
}),
],
controllers: [AppController],
providers: [AppService, AppReceiver],
})
export class AppModule {}
import { Controller, Post, Req, Res } from '@nestjs/common';
import { AppService } from './app.service';
import { AppReceiver } from './app.receiver';
import { Request, Response } from 'express';
@Controller('slack')
export class AppController {
constructor(
private readonly appService: AppService,
private readonly appReceiver: AppReceiver,
) {}
@Post('events')
async handleEvents(@Req() req: Request, @Res() res: Response): Promise<void> {
await this.appReceiver.handleRequest(req, res);
}
}
Or how can the ExpressReceiver be used in this module to apply it to the Slack Events API?
In your implementation, you don't take into account the challenge
part that is not managed by this module.
doc: https://api.slack.com/events/url_verification
Moreover, you can also use @Event annotation to subscribe to a specific event without the challenge
. In this case, you need to add the events you want to subscribe to your SlackApp.