slack.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {
  2. BodyParams, Controller, Get, Inject, Post, Req, Res,
  3. } from '@tsed/common';
  4. import { parseSlashCommand } from '@growi/slack';
  5. import { Installation } from '~/entities/installation';
  6. import { Relation } from '~/entities/relation';
  7. import { Order } from '~/entities/order';
  8. import { InstallationRepository } from '~/repositories/installation';
  9. import { RelationRepository } from '~/repositories/relation';
  10. import { OrderRepository } from '~/repositories/order';
  11. import { InstallerService } from '~/services/InstallerService';
  12. import { RegisterService } from '~/services/RegisterService';
  13. @Controller('/slack')
  14. export class SlackCtrl {
  15. @Inject()
  16. installerService: InstallerService;
  17. @Inject()
  18. installationRepository: InstallationRepository;
  19. @Inject()
  20. relationRepository: RelationRepository;
  21. @Inject()
  22. orderRepository: OrderRepository;
  23. @Inject()
  24. registerService: RegisterService;
  25. growiCommandsMappings = {
  26. register: async(body:{[key:string]:string}):Promise<void> => this.registerService.execSlashCommand(body),
  27. };
  28. @Get('/testsave')
  29. testsave(): void {
  30. const installation = new Installation();
  31. installation.data = {
  32. team: undefined,
  33. enterprise: undefined,
  34. user: {
  35. id: '',
  36. token: undefined,
  37. scopes: undefined,
  38. },
  39. };
  40. // const installationRepository = getRepository(Installation);
  41. this.installationRepository.save(installation);
  42. }
  43. @Get('/install')
  44. async install(): Promise<string> {
  45. const url = await this.installerService.installer.generateInstallUrl({
  46. // Add the scopes your app needs
  47. scopes: [
  48. 'channels:history',
  49. 'commands',
  50. 'groups:history',
  51. 'im:history',
  52. 'mpim:history',
  53. 'chat:write',
  54. ],
  55. });
  56. return `<a href="${url}">`
  57. // eslint-disable-next-line max-len
  58. + '<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" />'
  59. + '</a>';
  60. }
  61. @Post('/events')
  62. async handleEvent(@BodyParams() body:{[key:string]:string}, @Res() res: Res): Promise<string> {
  63. // Send response immediately to avoid opelation_timeout error
  64. // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
  65. const parsedBody = parseSlashCommand(body);
  66. const executeGrowiCommand = this.growiCommandsMappings[parsedBody.growiCommandType];
  67. await executeGrowiCommand(body);
  68. res.send();
  69. const installation = await this.installationRepository.findByID('1');
  70. if (installation == null) {
  71. throw new Error('installation is reqiured');
  72. }
  73. // Find the latest order by installationId
  74. let order = await this.orderRepository.findOne({
  75. installation: installation.id,
  76. }, {
  77. order: {
  78. createdAt: 'DESC',
  79. },
  80. });
  81. if (order == null || order.isExpired()) {
  82. order = await this.orderRepository.save({ installation: installation.id });
  83. }
  84. console.log('body', body);
  85. console.log('order', order);
  86. return 'This action will be handled by bolt service.';
  87. }
  88. @Get('/oauth_redirect')
  89. async handleOauthRedirect(@Req() req: Req, @Res() res: Res): Promise<void> {
  90. // illegal state
  91. // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
  92. if (req.query.state === '') {
  93. throw new Error('illegal state');
  94. }
  95. return this.installerService.installer.handleCallback(req, res);
  96. // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
  97. // this.installer.handleCallback(req, res, {
  98. // success: (installation, metadata, req, res) => {},
  99. // failure: (error, installOptions, req, res) => {},
  100. // });
  101. }
  102. }