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

GW-6712 Implemented checkboxes message

hakumizuki 4 лет назад
Родитель
Сommit
735ccbdf3c
2 измененных файлов с 110 добавлено и 2 удалено
  1. 3 0
      src/server/routes/apiv3/slack-integration.js
  2. 107 2
      src/server/service/slackbot.js

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

@@ -114,6 +114,9 @@ module.exports = (crowi) => {
         case 'help':
           await crowi.slackBotService.helpCommand(client, body);
           break;
+        case 'togetter':
+          await crowi.slackBotService.togetterCommand(client, body, args);
+          break;
         default:
           await crowi.slackBotService.notCommand(client, body);
           break;

+ 107 - 2
src/server/service/slackbot.js

@@ -91,6 +91,17 @@ class SlackBotService extends S2sMessageHandlable {
     return;
   }
 
+  async togetterCommand(client, body, args) {
+    // args一旦無視で
+    // Checkbox Message を返す
+    client.chat.postEphemeral({
+      channel: body.channel_id,
+      user: body.user_id,
+      text: 'Select messages to use.',
+      blocks: this.togetterMessageBlocks(),
+    });
+  }
+
   getKeywords(args) {
     const keywordsArr = args.slice(1);
     const keywords = keywordsArr.join(' ');
@@ -339,12 +350,12 @@ class SlackBotService extends S2sMessageHandlable {
     }
   }
 
-  generateMarkdownSectionBlock(blocks) {
+  generateMarkdownSectionBlock(text) {
     return {
       type: 'section',
       text: {
         type: 'mrkdwn',
-        text: blocks,
+        text,
       },
     };
   }
@@ -375,6 +386,100 @@ class SlackBotService extends S2sMessageHandlable {
     };
   }
 
+  togetterMessageBlocks() {
+    return [
+      this.generateMarkdownSectionBlock('Select messages to use.'),
+      this.actionsBlock(this.buttonElement('Show more', 'show_more'), this.buttonElement('Check all', 'check_all')),
+      this.actionsBlock(this.togetterCheckboxesElement()),
+      this.inputBlock(this.togetterInputBlockElement('page_path'), 'Page path'),
+      this.actionsBlock(this.buttonElement('Cancel', 'cancel'), this.buttonElement('Create page', 'create_page')),
+    ];
+  }
+
+  actionsBlock(...elements) {
+    return {
+      type: 'actions',
+      elements: [
+        ...elements,
+      ],
+    };
+  }
+
+  inputBlock(element, labelText) {
+    return {
+      type: 'input',
+      element,
+      label: {
+        type: 'plain_text',
+        text: labelText,
+        emoji: true,
+      },
+    };
+  }
+
+  /**
+   * Button element
+   * https://api.slack.com/reference/block-kit/block-elements#button
+   */
+  buttonElement(text, actionId) {
+    return {
+      type: 'button',
+      text: {
+        type: 'plain_text',
+        text,
+      },
+      action_id: actionId,
+    };
+  }
+
+  togetterCheckboxesElement() {
+    return {
+      type: 'checkboxes',
+      options: this.togetterCheckboxesElementOptions(),
+      action_id: 'checkboxes_changed',
+    };
+  }
+
+  togetterCheckboxesElementOptions() {
+    // options を conversations.history の結果でインクリメント
+    const options = [];
+    // 仮置き
+    for (let i = 0; i < 10; i++) {
+      const option = this.checkboxesElementOption('*username*  12:00PM', 'sample slack messages ... :star:');
+      options.push(option);
+    }
+    return options;
+  }
+
+  /**
+   * Option object
+   * https://api.slack.com/reference/block-kit/composition-objects#option
+   */
+  checkboxesElementOption(text, description) {
+    return {
+      text: {
+        type: 'mrkdwn',
+        text,
+      },
+      description: {
+        type: 'mrkdwn',
+        text: description,
+      },
+      value: 'selected',
+    };
+  }
+
+  /**
+   * Plain-text input element
+   * https://api.slack.com/reference/block-kit/block-elements#input
+   */
+  togetterInputBlockElement(actionId) {
+    return {
+      type: 'plain_text_input',
+      action_id: actionId,
+    };
+  }
+
 }
 
 module.exports = SlackBotService;