nest-slack-bolt icon indicating copy to clipboard operation
nest-slack-bolt copied to clipboard

How to apply the Event Subscription URL.

Open inkweon7269 opened this issue 9 months ago • 1 comments

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);
  }
}
스크린샷 2024-05-12 오후 2 40 31 스크린샷 2024-05-12 오후 2 51 33

Or how can the ExpressReceiver be used in this module to apply it to the Slack Events API?

inkweon7269 avatar May 12 '24 05:05 inkweon7269

Screenshot 2024-05-17 at 00 23 49

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. Screenshot 2024-05-16 at 23 57 48

bamada avatar May 16 '24 22:05 bamada