slack.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. BodyParams, Controller, Get, Inject, Post, Req, Res,
  3. } from '@tsed/common';
  4. import { Installation } from '~/entities/installation';
  5. import { Order } from '~/entities/order';
  6. import { InstallationRepository } from '~/repositories/installation';
  7. import { OrderRepository } from '~/repositories/order';
  8. import { InstallerService } from '~/services/InstallerService';
  9. @Controller('/slack')
  10. export class SlackCtrl {
  11. @Inject()
  12. installerService: InstallerService;
  13. @Inject()
  14. installationRepository: InstallationRepository;
  15. @Inject()
  16. orderRepository: OrderRepository;
  17. @Get('/testsave')
  18. testsave(): void {
  19. const installation = new Installation();
  20. installation.data = {
  21. team: undefined,
  22. enterprise: undefined,
  23. user: {
  24. id: '',
  25. token: undefined,
  26. scopes: undefined,
  27. },
  28. };
  29. // const installationRepository = getRepository(Installation);
  30. this.installationRepository.save(installation);
  31. }
  32. @Get('/install')
  33. async install(): Promise<string> {
  34. const url = await this.installerService.installer.generateInstallUrl({
  35. // Add the scopes your app needs
  36. scopes: [
  37. 'channels:history',
  38. 'commands',
  39. 'groups:history',
  40. 'im:history',
  41. 'mpim:history',
  42. 'chat:write',
  43. ],
  44. });
  45. return `<a href="${url}">`
  46. // eslint-disable-next-line max-len
  47. + '<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/add_to_slack@2x.png 2x" />'
  48. + '</a>';
  49. }
  50. @Post('/events')
  51. async handleEvent(@BodyParams() body: any, @Res() res: Res): Promise<string> {
  52. // Send response immediately to avoid opelation_timeout error
  53. // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
  54. res.send();
  55. console.log('body', body);
  56. const order = new Order(body.team_id);
  57. await this.orderRepository.save(order);
  58. return 'This action will be handled by bolt service.';
  59. }
  60. @Get('/oauth_redirect')
  61. async handleOauthRedirect(@Req() req: Req, @Res() res: Res): Promise<void> {
  62. // illegal state
  63. // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
  64. if (req.query.state === '') {
  65. throw new Error('illegal state');
  66. }
  67. return this.installerService.installer.handleCallback(req, res);
  68. // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
  69. // this.installer.handleCallback(req, res, {
  70. // success: (installation, metadata, req, res) => {},
  71. // failure: (error, installOptions, req, res) => {},
  72. // });
  73. }
  74. }