slack.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 teamId = body.team_id;
  57. // TODO move to service
  58. const order = await this.orderRepository.findOrCreateByTeamId(teamId);
  59. console.log('order', order);
  60. return 'This action will be handled by bolt service.';
  61. }
  62. @Get('/oauth_redirect')
  63. async handleOauthRedirect(@Req() req: Req, @Res() res: Res): Promise<void> {
  64. // illegal state
  65. // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
  66. if (req.query.state === '') {
  67. throw new Error('illegal state');
  68. }
  69. return this.installerService.installer.handleCallback(req, res);
  70. // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
  71. // this.installer.handleCallback(req, res, {
  72. // success: (installation, metadata, req, res) => {},
  73. // failure: (error, installOptions, req, res) => {},
  74. // });
  75. }
  76. }