RegisterService.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. orderRepository: OrderRepository, installation: Installation | undefined,
  42. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  43. authorizeResult:AuthorizeResult, payload: any,
  44. ): Promise<any> {
  45. const inputValues = payload.view.state.values;
  46. const growiUrl = inputValues.growiUrl.contents_input.value;
  47. const tokenPtoG = inputValues.tokenPtoG.contents_input.value;
  48. const tokenGtoP = inputValues.tokenGtoP.contents_input.value;
  49. const { botToken } = authorizeResult;
  50. const { channel } = JSON.parse(payload.view.private_metadata);
  51. const client = new WebClient(botToken, { logLevel: isProduction ? LogLevel.DEBUG : LogLevel.INFO });
  52. const isUrl = (url: string) => {
  53. return url.match(/^(https?:\/\/)/);
  54. };
  55. if (isUrl(growiUrl) == null) {
  56. return {
  57. response_action: 'errors',
  58. errors: {
  59. growiUrl: 'Please enter a valid URL',
  60. },
  61. };
  62. await client.chat.postEphemeral({
  63. channel,
  64. user: payload.user.id,
  65. // Recommended including 'text' to provide a fallback when using blocks
  66. // refer to https://api.slack.com/methods/chat.postEphemeral#text_usage
  67. text: 'Invalid URL',
  68. blocks: [
  69. generateMarkdownSectionBlock('Please enter a valid URL'),
  70. ],
  71. });
  72. return { errors: 'Please enter a valid URL' };
  73. }
  74. orderRepository.save({
  75. installation, growiUrl, tokenPtoG, tokenGtoP,
  76. });
  77. }
  78. async notifyServerUriToSlack(
  79. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  80. authorizeResult:AuthorizeResult, payload: any,
  81. ): Promise<void> {
  82. const { botToken } = authorizeResult;
  83. const { channel } = JSON.parse(payload.view.private_metadata);
  84. const serverUri = process.env.SERVER_URI;
  85. const client = new WebClient(botToken, { logLevel: isProduction ? LogLevel.DEBUG : LogLevel.INFO });
  86. await client.chat.postEphemeral({
  87. channel,
  88. user: payload.user.id,
  89. // Recommended including 'text' to provide a fallback when using blocks
  90. // refer to https://api.slack.com/methods/chat.postEphemeral#text_usage
  91. text: 'Proxy URL',
  92. blocks: [
  93. generateMarkdownSectionBlock('Please enter and update the following Proxy URL to slack bot setting form in your GROWI'),
  94. generateMarkdownSectionBlock(`Proxy URL: ${serverUri}`),
  95. ],
  96. });
  97. return;
  98. }
  99. }