Explorar o código

implement intaractive route

itizawa %!s(int64=5) %!d(string=hai) anos
pai
achega
f3f5d0895a
Modificáronse 1 ficheiros con 29 adicións e 0 borrados
  1. 29 0
      src/server/routes/apiv3/slack-bot.js

+ 29 - 0
src/server/routes/apiv3/slack-bot.js

@@ -1,11 +1,15 @@
 
 const express = require('express');
+const { createMessageAdapter } = require('@slack/interactive-messages');
 
 const router = express.Router();
 
 module.exports = (crowi) => {
   this.app = crowi.express;
 
+  const signingSecret = crowi.configManager.getConfig('crowi', 'slackbot:signingSecret');
+  const slackInteractions = createMessageAdapter(signingSecret);
+
   // Check if the access token is correct
   // function verificationAccessToken(req, res, next) {
   //   const slackBotAccessToken = req.body.slack_bot_access_token || null;
@@ -18,6 +22,12 @@ module.exports = (crowi) => {
   //   return next();
   // }
 
+  slackInteractions.viewSubmission('view_submission', (payload) => {
+    // Log the input elements from the view submission.
+    console.log(payload.view.state);
+  });
+
+
   function verificationRequestUrl(req, res, next) {
     // for verification request URL on Event Subscriptions
     if (req.body.type === 'url_verification') {
@@ -52,6 +62,25 @@ module.exports = (crowi) => {
 
   });
 
+  router.post('/interactive', verificationRequestUrl, async(req, res) => {
+
+    // Send response immediately to avoid opelation_timeout error
+    // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
+    res.send();
+    console.log(req.body);
+    const payload = JSON.parse(req.body.payload);
+    const { type } = payload;
+
+    switch (type) {
+      case 'view_submission':
+        await crowi.boltService.createPageInGrowi(payload);
+        break;
+      default:
+        break;
+    }
+
+  });
+
 
   return router;
 };