slack.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. handleEvent(@BodyParams() body:{[key:string]:string}, @Res() res: Res): 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 modulePath = `../services/${method}Service`;
  54. const targetModule = require(modulePath);
  55. console.log('hoge', targetModule);
  56. targetModule();
  57. // const slackInput = this.receiveService.receiveContentsFromSlack(body);
  58. // console.log('Controller/events', slackInput);
  59. res.send();
  60. console.log('body', body);
  61. return 'This action will be handled by bolt service.';
  62. }
  63. @Get('/oauth_redirect')
  64. async handleOauthRedirect(@Req() req: Req, @Res() res: Res): Promise<void> {
  65. // illegal state
  66. // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
  67. if (req.query.state === '') {
  68. throw new Error('illegal state');
  69. }
  70. return this.installerService.installer.handleCallback(req, res);
  71. // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
  72. // this.installer.handleCallback(req, res, {
  73. // success: (installation, metadata, req, res) => {},
  74. // failure: (error, installOptions, req, res) => {},
  75. // });
  76. }
  77. }