Browse Source

Merge pull request #3606 from weseek/feat/impl-notify-slack-ws-put-test-button

Feat/impl notify slack ws put test button
Sizma yosimaz 5 years ago
parent
commit
e2265c94a6
1 changed files with 50 additions and 0 deletions
  1. 50 0
      src/server/routes/apiv3/slack-integration.js

+ 50 - 0
src/server/routes/apiv3/slack-integration.js

@@ -4,6 +4,7 @@ const logger = loggerFactory('growi:routes:apiv3:notification-setting');
 const express = require('express');
 const express = require('express');
 const { body } = require('express-validator');
 const { body } = require('express-validator');
 const crypto = require('crypto');
 const crypto = require('crypto');
+const { WebClient, LogLevel } = require('@slack/web-api');
 const ErrorV3 = require('../../models/vo/error-apiv3');
 const ErrorV3 = require('../../models/vo/error-apiv3');
 
 
 const router = express.Router();
 const router = express.Router();
@@ -55,6 +56,9 @@ module.exports = (crowi) => {
       body('currentBotType')
       body('currentBotType')
         .isIn(['official-bot', 'custom-bot-without-proxy', 'custom-bot-with-proxy']),
         .isIn(['official-bot', 'custom-bot-without-proxy', 'custom-bot-with-proxy']),
     ],
     ],
+    NotificationTestToSlackWorkSpace: [
+      body('channel').isString(),
+    ],
   };
   };
 
 
   async function updateSlackBotSettings(params) {
   async function updateSlackBotSettings(params) {
@@ -268,6 +272,52 @@ module.exports = (crowi) => {
     }
     }
   });
   });
 
 
+  /**
+   * @swagger
+   *
+   *    /slack-integration/test-notification-to-slack-work-space:
+   *      post:
+   *        tags: [SlackTestToWorkSpace]
+   *        operationId: postSlackMessageToSlackWorkSpace
+   *        summary: test to send message to slack work space
+   *        description: post message to slack work space
+   *        responses:
+   *          200:
+   *            description: Succeeded to send a message to slack work space
+   */
+  router.post('/notification-test-to-slack-work-space',
+    loginRequiredStrictly, adminRequired, csrf, validator.NotificationTestToSlackWorkSpace, apiV3FormValidator, async(req, res) => {
+      const isConnectedToSlack = crowi.slackBotService.isConnectedToSlack;
+      const { channel } = req.body;
+
+      if (isConnectedToSlack === false) {
+        const msg = 'Bot User OAuth Token is not setup.';
+        logger.error('Error', msg);
+        return res.apiv3Err(new ErrorV3(msg, 'not-setup-slack-bot-token', 400));
+      }
+
+      const slackBotToken = crowi.configManager.getConfig('crowi', 'slackbot:token');
+      this.client = new WebClient(slackBotToken, { logLevel: LogLevel.DEBUG });
+      logger.debug('SlackBot: setup is done');
+
+      try {
+        this.client.chat.postMessage({
+          channel: `#${channel}`,
+          text: 'Your test was successful!',
+        });
+        logger.info(`SlackTest: send success massage to slack work space at #${channel}.`);
+        logger.info(`If you do not receive a message, you may not have invited the bot to the #${channel} channel.`);
+        // eslint-disable-next-line max-len
+        const message = `Successfully send message to Slack work space. See #general channel. If you do not receive a message, you may not have invited the bot to the #${channel} channel.`;
+        return res.apiv3({ message });
+      }
+      catch (error) {
+        const msg = 'Error occured in testing to notify slack work space';
+        logger.error('Error', error);
+        return res.apiv3Err(new ErrorV3(msg, 'notification-test-slack-work-space-failed'), 500);
+      }
+    });
+
   /**
   /**
    * @swagger
    * @swagger
    *
    *