slack.ts 2.7 KB

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