Parcourir la source

Merge pull request #3596 from weseek/feat/create-modal-after-typing-growi-register

Feat/create modal after typing growi register
Yuki Takei il y a 5 ans
Parent
commit
fcf0bd1676

+ 1 - 0
packages/slack/src/index.ts

@@ -10,3 +10,4 @@ export const supportedGrowiCommands: string[] = [
 export * from './interfaces/growi-command';
 export * from './models/errors';
 export * from './utils/slash-command-parser';
+export * from './utils/block-creater';

+ 31 - 0
packages/slack/src/utils/block-creater.ts

@@ -0,0 +1,31 @@
+import { SectionBlock, InputBlock } from '@slack/types';
+
+export const generateMarkdownSectionBlock = (blocks:string):SectionBlock => {
+  return {
+    type: 'section',
+    text: {
+      type: 'mrkdwn',
+      text: blocks,
+    },
+  };
+};
+
+export const generateInputSectionBlock = (blockId:string, labelText:string, actionId:string, isMultiline:boolean, placeholder:string):InputBlock => {
+  return {
+    type: 'input',
+    block_id: blockId,
+    label: {
+      type: 'plain_text',
+      text: labelText,
+    },
+    element: {
+      type: 'plain_text_input',
+      action_id: actionId,
+      multiline: isMultiline,
+      placeholder: {
+        type: 'plain_text',
+        text: placeholder,
+      },
+    },
+  };
+};

+ 10 - 5
packages/slackbot-proxy/src/controllers/slack.ts

@@ -1,7 +1,7 @@
 import {
   BodyParams, Controller, Get, Inject, Post, Req, Res,
 } from '@tsed/common';
-
+import { parseSlashCommand } from '@growi/slack';
 import { Installation } from '~/entities/installation';
 import { Relation } from '~/entities/relation';
 import { Order } from '~/entities/order';
@@ -10,7 +10,7 @@ import { InstallationRepository } from '~/repositories/installation';
 import { RelationRepository } from '~/repositories/relation';
 import { OrderRepository } from '~/repositories/order';
 import { InstallerService } from '~/services/InstallerService';
-import { ReceiveService } from '~/services/RecieveService';
+import { RegisterService } from '~/services/RegisterService';
 
 
 @Controller('/slack')
@@ -29,7 +29,11 @@ export class SlackCtrl {
   orderRepository: OrderRepository;
 
   @Inject()
-  receiveService: ReceiveService;
+  registerService: RegisterService;
+
+  growiCommandsMappings = {
+    register: async(body:{[key:string]:string}):Promise<void> => this.registerService.execSlashCommand(body),
+  };
 
   @Get('/testsave')
   testsave(): void {
@@ -75,8 +79,9 @@ export class SlackCtrl {
     // Send response immediately to avoid opelation_timeout error
     // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
 
-    const slackInput = this.receiveService.receiveContentsFromSlack(body);
-    console.log('Controller/events', slackInput);
+    const parsedBody = parseSlashCommand(body);
+    const executeGrowiCommand = this.growiCommandsMappings[parsedBody.growiCommandType];
+    await executeGrowiCommand(body);
     res.send();
 
     const installation = await this.installationRepository.findByID('1');

+ 3 - 0
packages/slackbot-proxy/src/interfaces/growi-commands-mappings.ts

@@ -0,0 +1,3 @@
+export interface GrowiCommandsMappings{
+  execSlashCommand(body:{[key:string]:string}):Promise<void>
+}

+ 37 - 0
packages/slackbot-proxy/src/services/RegisterService.ts

@@ -0,0 +1,37 @@
+import { Service } from '@tsed/di';
+import { WebClient, LogLevel } from '@slack/web-api';
+import { generateInputSectionBlock } from '@growi/slack';
+import { GrowiCommandsMappings } from '../interfaces/growi-commands-mappings';
+
+@Service()
+export class RegisterService implements GrowiCommandsMappings {
+
+  async execSlashCommand(body:{[key:string]:string}):Promise<void> {
+    // tmp use process.env
+    const client = new WebClient(process.env.SLACK_BOT_USER_OAUTH_TOKEN, { logLevel: LogLevel.DEBUG });
+    await client.views.open({
+      trigger_id: body.trigger_id,
+      view: {
+        type: 'modal',
+        title: {
+          type: 'plain_text',
+          text: 'Register Credentials',
+        },
+        submit: {
+          type: 'plain_text',
+          text: 'Submit',
+        },
+        close: {
+          type: 'plain_text',
+          text: 'Close',
+        },
+        blocks: [
+          generateInputSectionBlock('growiDomain', 'GROWI domain', 'contents_input', false, 'https://example.com'),
+          generateInputSectionBlock('growiAccessToken', 'GROWI ACCESS_TOKEN', 'contents_input', false, 'jBMZvpk.....'),
+          generateInputSectionBlock('proxyToken', 'PROXY ACCESS_TOKEM', 'contents_input', false, 'jBMZvpk.....'),
+        ],
+      },
+    });
+  }
+
+}