RegisterService.ts 4.1 KB

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