yusuketk 5 years ago
parent
commit
6025922910
2 changed files with 42 additions and 50 deletions
  1. 2 8
      src/server/routes/apiv3/slack-bot.js
  2. 40 42
      src/server/service/bolt.js

+ 2 - 8
src/server/routes/apiv3/slack-bot.js

@@ -10,18 +10,12 @@ module.exports = (crowi) => {
 
   router.post('/', async(req, res) => {
     try {
-      const response = await boltService.receiver.requestHandler.bind(boltService.receiver)(req, res);
-      if (response !== null) {
-        res.send(response);
-      }
+      const response = await boltService.receiver.requestHandler.bind(boltService.receiver)(req, res) || null;
+      res.send(response);
     }
     catch (err) {
-      console.log('catch');
-      console.log(err);
       return res.apiv3Err(new ErrorV3(`Error:Slack-Bot:${err}`), 500);
     }
-    // for verification request URL on Event Subscriptions
-    res.send(req.body);
   });
 
   return router;

+ 40 - 42
src/server/service/bolt.js

@@ -8,14 +8,14 @@ class BoltReciever {
 
   async requestHandler(req, res) {
     if (this.bolt === undefined) {
-      throw new Error('Slack bot is not setup');
+      throw new Error('Slack Bot service is not setup');
     }
 
     let ackCalled = false;
 
     // for verification request URL on Event Subscriptions
-    if (req.body.challenge && req.body.type) {
-      return res.send(req.body);
+    if (req.body.type === 'url_verification') {
+      return req.body;
     }
 
     const payload = req.body.payload;
@@ -38,10 +38,11 @@ class BoltReciever {
         ackCalled = true;
 
         if (response instanceof Error) {
-          throw new Error();
+          const message = response.message || 'Error occurred';
+          throw new Error(message);
         }
         else if (!response) {
-          return;
+          return null;
         }
         else {
           return response;
@@ -71,7 +72,7 @@ class BoltService {
     this.client = client;
 
     if (token != null || signingSecret != null) {
-      logger.debug('TwitterStrategy: setup is done');
+      logger.debug('SlackBot: setup is done');
       this.bolt = new App({
         token,
         signingSecret,
@@ -97,7 +98,7 @@ class BoltService {
       await say(`${command.text}`);
     });
 
-    this.bolt.command('/growi', async({
+    this.bolt.command('/hoge', async({
       command, client, body, ack,
     }) => {
       await ack();
@@ -106,11 +107,11 @@ class BoltService {
 
       switch (firstArg) {
         case 'search':
-          this.searchResults(command, args);
+          await this.searchResults(command, args);
           break;
 
         case 'create':
-          this.createModal(command, client, body);
+          await this.createModal(command, client, body);
           break;
 
         default:
@@ -121,7 +122,7 @@ class BoltService {
 
     this.bolt.view('createPage', async({ ack, view }) => {
       await ack();
-      return this.createPageInGrowi(view);
+      await this.createPageInGrowi(view);
     });
 
     this.bolt.action('button_click', async({ body, ack, say }) => {
@@ -132,27 +133,26 @@ class BoltService {
   }
 
   notCommand(command) {
-    logger.error('Input first arguments');
-    return this.client.chat.postEphemeral({
+    logger.error('Invalid first argument');
+    this.client.chat.postEphemeral({
       channel: command.channel_id,
       user: command.user_id,
       blocks: [
         this.generateMarkdownSectionBlock('*No command.*\n Hint\n `/growi [command] [keyword]`'),
       ],
     });
-
+    throw new Error('/growi command: Invalid first argument');
   }
 
   async searchResults(command, args) {
     const firstKeyword = args[1];
     if (firstKeyword == null) {
-      return this.client.chat.postEphemeral({
+      this.client.chat.postEphemeral({
         channel: command.channel_id,
         user: command.user_id,
-        blocks: [
-          this.generateMarkdownSectionBlock('*Input keywords.*\n Hint\n `/growi search [keyword]`'),
-        ],
+        blocks: [this.generateMarkdownSectionBlock('*Input keywords.*\n Hint\n `/growi search [keyword]`')],
       });
+      throw new Error('/growi command:search: Invalid keyword');
     }
 
     // remove leading 'search'.
@@ -164,13 +164,13 @@ class BoltService {
 
     // no search results
     if (results.data.length === 0) {
-      return this.client.chat.postEphemeral({
+      logger.info(`No page found with "${keywords}"`);
+      this.client.chat.postEphemeral({
         channel: command.channel_id,
         user: command.user_id,
-        blocks: [
-          this.generateMarkdownSectionBlock('*No page that match your keywords.*'),
-        ],
+        blocks: [this.generateMarkdownSectionBlock('*No page that match your keywords.*')],
       });
+      return;
     }
 
     const resultPaths = results.data.map((data) => {
@@ -210,20 +210,26 @@ class BoltService {
           this.generateMarkdownSectionBlock('*Failed to search.*\n Hint\n `/growi search [keyword]`'),
         ],
       });
+      throw new Error('/growi command:search: Failed to search');
     }
   }
 
   async createModal(command, client, body) {
     const User = this.crowi.model('User');
+    const slackUser = await User.findUserByUsername('slackUser');
 
-    try {
-      const slackUser = await User.findUserByUsername('slackUser');
-
-      // if "slackUser" is null, don't show create Modal
-      if (slackUser == null) {
-        throw new Error('userNull');
-      }
+    // if "slackUser" is null, don't show create Modal
+    if (slackUser == null) {
+      logger.error('Failed to create a page because slackUser is not found.');
+      this.client.chat.postEphemeral({
+        channel: command.channel_id,
+        user: command.user_id,
+        blocks: [this.generateMarkdownSectionBlock('*slackUser does not exist.*')],
+      });
+      throw new Error('/growi command:create: slackUser is not found');
+    }
 
+    try {
       await client.views.open({
         trigger_id: body.trigger_id,
 
@@ -250,18 +256,8 @@ class BoltService {
         },
       });
     }
-    catch (e) {
-      if (e instanceof Error) {
-        return this.client.chat.postEphemeral({
-          channel: command.channel_id,
-          user: command.user_id,
-          blocks: [
-            this.generateMarkdownSectionBlock('*slackUser does not exist.*'),
-          ],
-        });
-      }
-
-      logger.error('Failed to create page.');
+    catch (err) {
+      logger.error('Failed to create a page.');
       await this.client.chat.postEphemeral({
         channel: command.channel_id,
         user: command.user_id,
@@ -269,6 +265,7 @@ class BoltService {
           this.generateMarkdownSectionBlock('*Failed to create new page.*\n Hint\n `/growi create`'),
         ],
       });
+      throw err;
     }
   }
 
@@ -290,10 +287,11 @@ class BoltService {
       path = pathUtils.normalizePath(path);
 
       const user = slackUser._id;
-      return Page.create(path, body, user, {});
+      await Page.create(path, body, user, {});
     }
-    catch {
+    catch (err) {
       logger.error('Failed to create page in GROWI.');
+      throw err;
     }
   }