RegisterService.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/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. // tmp use process.env
  14. const client = new WebClient(botToken, { logLevel: isProduction ? LogLevel.DEBUG : LogLevel.INFO });
  15. await client.views.open({
  16. trigger_id: body.trigger_id,
  17. view: {
  18. type: 'modal',
  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. blocks: [
  32. generateInputSectionBlock('growiDomain', 'GROWI domain', 'contents_input', false, 'https://example.com'),
  33. generateInputSectionBlock('growiAccessToken', 'GROWI ACCESS_TOKEN', 'contents_input', false, 'jBMZvpk.....'),
  34. generateInputSectionBlock('proxyToken', 'PROXY ACCESS_TOKEN', 'contents_input', false, 'jBMZvpk.....'),
  35. {
  36. block_id: 'channel_to_post_proxy_url',
  37. type: 'input',
  38. label: {
  39. type: 'plain_text',
  40. text: 'Select a channel to post the proxy URL on',
  41. },
  42. element: {
  43. action_id: 'submit_growi_url_and_access_tokens',
  44. type: 'conversations_select',
  45. response_url_enabled: true,
  46. default_to_current_conversation: true,
  47. },
  48. },
  49. ],
  50. },
  51. });
  52. }
  53. async upsertOrderRecord(
  54. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  55. orderRepository: OrderRepository, installation: Installation | undefined, payload: any,
  56. ): Promise<void> {
  57. const inputValues = payload.view.state.values;
  58. const inputGrowiUrl = inputValues.growiDomain.contents_input.value;
  59. const inputGrowiAccessToken = inputValues.growiAccessToken.contents_input.value;
  60. const inputProxyAccessToken = inputValues.proxyToken.contents_input.value;
  61. const order = await orderRepository.findOne({ installation: installation?.id, growiUrl: inputGrowiUrl });
  62. if (order != null) {
  63. orderRepository.update(
  64. { installation: installation?.id, growiUrl: inputGrowiUrl },
  65. { growiAccessToken: inputGrowiAccessToken, proxyAccessToken: inputProxyAccessToken },
  66. );
  67. }
  68. else {
  69. orderRepository.save({
  70. installation: installation?.id, growiUrl: inputGrowiUrl, growiAccessToken: inputGrowiAccessToken, proxyAccessToken: inputProxyAccessToken,
  71. });
  72. }
  73. }
  74. async notifyServerUriToSlack(
  75. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  76. authorizeResult:AuthorizeResult, payload: any,
  77. ): Promise<void> {
  78. const { botToken } = authorizeResult;
  79. const serverUri = process.env.SERVER_URI;
  80. const client = new WebClient(botToken, { logLevel: isProduction ? LogLevel.DEBUG : LogLevel.INFO });
  81. await client.chat.postEphemeral({
  82. channel: payload.response_urls[0].channel_id,
  83. user: payload.user.id,
  84. // Recommended including 'text' to provide a fallback when using blocks
  85. // refer to https://api.slack.com/methods/chat.postEphemeral#text_usage
  86. text: 'Proxy URL',
  87. blocks: [
  88. generateMarkdownSectionBlock('Please enter and update the following Proxy URL to slack bot setting form in your GROWI'),
  89. generateMarkdownSectionBlock(`Proxy URL: ${serverUri}`),
  90. ],
  91. });
  92. return;
  93. }
  94. }