slack.ts 3.5 KB

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