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

Merge branch 'feat/growi-bot' into feat/GW-5646-show-workspace-name-for-growi-app

Shun Miyazawa 5 лет назад
Родитель
Сommit
4684b0674c
1 измененных файлов с 34 добавлено и 14 удалено
  1. 34 14
      packages/slackbot-proxy/src/controllers/slack.ts

+ 34 - 14
packages/slackbot-proxy/src/controllers/slack.ts

@@ -3,8 +3,6 @@ import {
 } from '@tsed/common';
 import { parseSlashCommand } from '@growi/slack';
 import { Installation } from '~/entities/installation';
-import { Relation } from '~/entities/relation';
-import { Order } from '~/entities/order';
 
 import { InstallationRepository } from '~/repositories/installation';
 import { RelationRepository } from '~/repositories/relation';
@@ -79,8 +77,16 @@ 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
 
+    if (body.text == null) {
+      return 'No text.';
+    }
+
     const parsedBody = parseSlashCommand(body);
     const executeGrowiCommand = this.growiCommandsMappings[parsedBody.growiCommandType];
+
+    if (executeGrowiCommand == null) {
+      return 'No executeGrowiCommand';
+    }
     await executeGrowiCommand(body);
     res.send();
 
@@ -102,27 +108,41 @@ export class SlackCtrl {
       order = await this.orderRepository.save({ installation: installation.id });
     }
 
-    console.log('body', body);
-    console.log('order', order);
-
     return 'This action will be handled by bolt service.';
   }
 
   @Get('/oauth_redirect')
   async handleOauthRedirect(@Req() req: Req, @Res() res: Res): Promise<void> {
+
     // illegal state
     // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
-    if (req.query.state === '') {
-      throw new Error('illegal state');
+    if (req.query.state !== 'init') {
+      res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8' });
+      res.end('<html>'
+      + '<head><meta name="viewport" content="width=device-width,initial-scale=1"></head>'
+      + '<body style="text-align:center; padding-top:20%;">'
+      + '<h1>Illegal state, try it again.</h1>'
+      + '<a href="/slack/install">'
+      + 'go to install page'
+      + '</a>'
+      + '</body></html>');
     }
 
-    return this.installerService.installer.handleCallback(req, res);
-
-    // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
-    // this.installer.handleCallback(req, res, {
-    //   success: (installation, metadata, req, res) => {},
-    //   failure: (error, installOptions, req, res) => {},
-    // });
+    this.installerService.installer.handleCallback(req, res, {
+      // success: (installation, metadata, req, res) => {},
+      failure: (error, installOptions, req, res) => {
+        res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8' });
+        res.end('<html>'
+        + '<head><meta name="viewport" content="width=device-width,initial-scale=1"></head>'
+        + '<body style="text-align:center; padding-top:20%;">'
+        + '<h1>GROWI Bot installation failed</h1>'
+        + '<p>Please contact administrators of your workspace</p>'
+        + 'Reference: <a href="https://slack.com/help/articles/222386767-Manage-app-installation-settings-for-your-workspace">'
+        + 'Manage app installation settings for your workspace'
+        + '</a>'
+        + '</body></html>');
+      },
+    });
   }
 
 }