slack.ts 2.6 KB

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