فهرست منبع

Send not allowed response to user when the command is forbidden in the channel

hakumizuki 4 سال پیش
والد
کامیت
7be00a22db
2فایلهای تغییر یافته به همراه22 افزوده شده و 2 حذف شده
  1. 21 1
      packages/slackbot-proxy/src/controllers/slack.ts
  2. 1 1
      src/server/routes/apiv3/slack-integration.js

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

@@ -7,7 +7,7 @@ import axios from 'axios';
 import { WebAPICallResult } from '@slack/web-api';
 
 import {
-  generateMarkdownSectionBlock, GrowiCommand, parseSlashCommand, postEphemeralErrors, verifySlackRequest,
+  generateMarkdownSectionBlock, GrowiCommand, parseSlashCommand, postEphemeralErrors, verifySlackRequest, generateWebClient,
 } from '@growi/slack';
 
 import { Relation } from '~/entities/relation';
@@ -173,7 +173,10 @@ export class SlackCtrl {
       }
     }));
 
+    let didAnyCommandRun = false;
+
     if (relationsForSingleUse.length > 0) {
+      didAnyCommandRun = true;
       body.growiUrisForSingleUse = relationsForSingleUse.map(v => v.growiUri);
       return this.selectGrowiService.process(growiCommand, authorizeResult, body);
     }
@@ -190,8 +193,25 @@ export class SlackCtrl {
      * forward to GROWI server
      */
     if (relationsForBroadcastUse.length > 0) {
+      didAnyCommandRun = true;
       this.sendCommand(growiCommand, relationsForBroadcastUse, body);
     }
+
+    if (!didAnyCommandRun) {
+      const botToken = relations[0].installation?.data.bot?.token;
+
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      const client = generateWebClient(botToken!);
+
+      return client.chat.postEphemeral({
+        text: 'Error occured.',
+        channel: body.channel_id,
+        user: body.user_id,
+        blocks: [
+          generateMarkdownSectionBlock(`It is not allowed to run *'${growiCommand.growiCommandType}'* command from this channel.`),
+        ],
+      });
+    }
   }
 
   @Post('/interactions')

+ 1 - 1
src/server/routes/apiv3/slack-integration.js

@@ -75,7 +75,7 @@ module.exports = (crowi) => {
 
     // validate
     if (!supportedCommands.includes(command)) {
-      return res.status(403).send(`You are not allowded to run '${command}' command to this GROWI.`);
+      return res.status(403).send(`You are not allowed to run '${command}' command to this GROWI.`);
     }
 
     next();