slack.ts 3.3 KB

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