slack.ts 3.1 KB

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