Просмотр исходного кода

Merge pull request #3585 from weseek/feat/create-endpoint-growi-register

Feat/create endpoint growi register
Yuki Takei 5 лет назад
Родитель
Сommit
af4221deb5

+ 31 - 3
packages/slack/src/utils/slash-command-parser.test.ts

@@ -12,7 +12,7 @@ describe('parse SlashCommand', () => {
   describe('without growiCommandType', () => {
     test('throws InvalidGrowiCommandError', () => {
       // setup
-      const slashCommandText = '/growi';
+      const slashCommandText = '';
       const slashCommand = new SlashCommandMock(slashCommandText);
 
       // when/then
@@ -24,7 +24,7 @@ describe('parse SlashCommand', () => {
 
   test('returns a GrowiCommand instance with empty growiCommandArgs', () => {
     // setup
-    const slashCommandText = '/growi search';
+    const slashCommandText = 'search';
     const slashCommand = new SlashCommandMock(slashCommandText);
 
     // when
@@ -36,9 +36,37 @@ describe('parse SlashCommand', () => {
     expect(result.growiCommandArgs).toStrictEqual([]);
   });
 
+  test('returns a GrowiCommand instance with space growiCommandType', () => {
+    // setup
+    const slashCommandText = '   search   ';
+    const slashCommand = new SlashCommandMock(slashCommandText);
+
+    // when
+    const result = parse(slashCommand);
+
+    // then
+    expect(result.text).toBe(slashCommandText);
+    expect(result.growiCommandType).toBe('search');
+    expect(result.growiCommandArgs).toStrictEqual([]);
+  });
+
+  test('returns a GrowiCommand instance with space growiCommandArgs', () => {
+    // setup
+    const slashCommandText = '   search hoge   ';
+    const slashCommand = new SlashCommandMock(slashCommandText);
+
+    // when
+    const result = parse(slashCommand);
+
+    // then
+    expect(result.text).toBe(slashCommandText);
+    expect(result.growiCommandType).toBe('search');
+    expect(result.growiCommandArgs).toStrictEqual(['hoge']);
+  });
+
   test('returns a GrowiCommand instance', () => {
     // setup
-    const slashCommandText = '/growi search keyword1 keyword2';
+    const slashCommandText = 'search keyword1 keyword2';
     const slashCommand = new SlashCommandMock(slashCommandText);
 
     // when

+ 8 - 9
packages/slack/src/utils/slash-command-parser.ts

@@ -1,18 +1,17 @@
-import { SlashCommand } from '@slack/bolt';
+import { GrowiCommand } from '../interfaces/growi-command';
+import { InvalidGrowiCommandError } from '../models/errors';
 
-import { GrowiCommand } from '~/interfaces/growi-command';
-import { InvalidGrowiCommandError } from '~/models/errors';
+export const parse = (slashCommand:{[key:string]:string}): GrowiCommand => {
+  const trimmedText = slashCommand.text.trim();
+  const splitted = trimmedText.split(' ');
 
-export const parse = (slashCommand: SlashCommand): GrowiCommand => {
-  const splitted = slashCommand.text.split(' ');
-
-  if (splitted.length < 2) {
+  if (splitted[0] === '') {
     throw new InvalidGrowiCommandError('The SlashCommand.text does not specify GrowiCommand type');
   }
 
   return {
     text: slashCommand.text,
-    growiCommandType: splitted[1],
-    growiCommandArgs: splitted.slice(2),
+    growiCommandType: splitted[0],
+    growiCommandArgs: splitted.slice(1),
   };
 };

+ 2 - 0
packages/slackbot-proxy/package.json

@@ -17,7 +17,9 @@
     "test:lint:fix": "eslint src --ext .ts --fix"
   },
   "dependencies": {
+    "@growi/slack": "0.9.0-RC",
     "@slack/oauth": "^2.0.1",
+    "@slack/web-api": "^6.1.0",
     "@tsed/common": "^6.34.3",
     "@tsed/di": "^6.34.3",
     "@tsed/platform-express": "^6.34.3",

+ 8 - 1
packages/slackbot-proxy/src/controllers/slack.ts

@@ -5,6 +5,7 @@ import {
 import { Installation } from '~/entities/installation';
 import { InstallationRepository } from '~/repositories/installation';
 import { InstallerService } from '~/services/InstallerService';
+import { ReceiveService } from '~/services/RecieveService';
 
 
 @Controller('/slack')
@@ -16,6 +17,9 @@ export class SlackCtrl {
   @Inject()
   installationRepository: InstallationRepository;
 
+  @Inject()
+  receiveService: ReceiveService;
+
   @Get('/testsave')
   testsave(): void {
     const installation = new Installation();
@@ -56,9 +60,12 @@ export class SlackCtrl {
   }
 
   @Post('/events')
-  handleEvent(@BodyParams() body: any, @Res() res: Res): string {
+  handleEvent(@BodyParams() body:{[key:string]:string}, @Res() res: Res): string {
     // 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);
     res.send();
 
     console.log('body', body);

+ 16 - 0
packages/slackbot-proxy/src/services/RecieveService.ts

@@ -0,0 +1,16 @@
+import { Service } from '@tsed/di';
+import { parse } from '@growi/slack/src/utils/slash-command-parser';
+
+@Service()
+export class ReceiveService {
+
+  receiveContentsFromSlack(body:{[key:string]:string}) : string {
+    const parseBody = parse(body);
+    if (parseBody.growiCommandType === 'register') {
+      console.log('register action occured');
+      return 'register action occurd';
+    }
+    return 'return receiveContentsFromSlack';
+  }
+
+}