RegisterService.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Service } from '@tsed/di';
  2. import { WebClient, LogLevel } from '@slack/web-api';
  3. import { generateInputSectionBlock, GrowiCommand, generateMarkdownSectionBlock } from '@growi/slack';
  4. import { AuthorizeResult } from '@slack/oauth';
  5. import { GrowiCommandProcessor } from '~/interfaces/slack-to-growi/growi-command-processor';
  6. import { OrderRepository } from '~/repositories/order';
  7. import { Installation } from '~/entities/installation';
  8. const isProduction = process.env.NODE_ENV === 'production';
  9. @Service()
  10. export class RegisterService implements GrowiCommandProcessor {
  11. async process(growiCommand: GrowiCommand, authorizeResult: AuthorizeResult, body: {[key:string]:string}): Promise<void> {
  12. const { botToken } = authorizeResult;
  13. const client = new WebClient(botToken, { logLevel: isProduction ? LogLevel.DEBUG : LogLevel.INFO });
  14. await client.views.open({
  15. trigger_id: body.trigger_id,
  16. view: {
  17. type: 'modal',
  18. callback_id: 'register',
  19. title: {
  20. type: 'plain_text',
  21. text: 'Register Credentials',
  22. },
  23. submit: {
  24. type: 'plain_text',
  25. text: 'Submit',
  26. },
  27. close: {
  28. type: 'plain_text',
  29. text: 'Close',
  30. },
  31. private_metadata: JSON.stringify({ channel: body.channel_name }),
  32. blocks: [
  33. generateInputSectionBlock('growiUrl', 'GROWI domain', 'contents_input', false, 'https://example.com'),
  34. generateInputSectionBlock('tokenPtoG', 'Access Token Proxy to GROWI', 'contents_input', false, 'jBMZvpk.....'),
  35. generateInputSectionBlock('tokenGtoP', 'Access Token GROWI to Proxy', 'contents_input', false, 'sdg15av.....'),
  36. ],
  37. },
  38. });
  39. }
  40. async insertOrderRecord(
  41. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  42. orderRepository: OrderRepository, installation: Installation | undefined, payload: any,
  43. ): Promise<void> {
  44. const inputValues = payload.view.state.values;
  45. const growiUrl = inputValues.growiUrl.contents_input.value;
  46. const tokenPtoG = inputValues.tokenPtoG.contents_input.value;
  47. const tokenGtoP = inputValues.tokenGtoP.contents_input.value;
  48. orderRepository.save({
  49. installation, growiUrl, tokenPtoG, tokenGtoP,
  50. });
  51. }
  52. async notifyServerUriToSlack(
  53. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  54. authorizeResult:AuthorizeResult, payload: any,
  55. ): Promise<void> {
  56. const { botToken } = authorizeResult;
  57. const { channel } = JSON.parse(payload.view.private_metadata);
  58. const serverUri = process.env.SERVER_URI;
  59. const client = new WebClient(botToken, { logLevel: isProduction ? LogLevel.DEBUG : LogLevel.INFO });
  60. await client.chat.postEphemeral({
  61. channel,
  62. user: payload.user.id,
  63. // Recommended including 'text' to provide a fallback when using blocks
  64. // refer to https://api.slack.com/methods/chat.postEphemeral#text_usage
  65. text: 'Proxy URL',
  66. blocks: [
  67. generateMarkdownSectionBlock('Please enter and update the following Proxy URL to slack bot setting form in your GROWI'),
  68. generateMarkdownSectionBlock(`Proxy URL: ${serverUri}`),
  69. ],
  70. });
  71. return;
  72. }
  73. }