Skip to content

Latest commit

ย 

History

History

split-bot

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 

์ •์‚ฐ๋ด‡ ๋งŒ๋“ค๊ธฐ

์†Œ์Šค์ฝ”๋“œ

require('dotenv').config()
const { App } = require('@slack/bolt');

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  socketMode: true,
  appToken: process.env.SLACK_APP_TOKEN,
  port: process.env.PORT || 3000
});

app.command('/์ •์‚ฐ', async ({ ack, body, client, logger }) => {
  await ack();

  try {
    const result = await client.views.open({
      trigger_id: body.trigger_id,
      view: {
        "type": "modal",
        "callback_id": "split_view",
        "title": {
          "type": "plain_text",
          "text": "์ •์‚ฐ๋„์šฐ๋ฏธ",
          "emoji": true
        },
        "submit": {
          "type": "plain_text",
          "text": "Submit",
          "emoji": true
        },
        "close": {
          "type": "plain_text",
          "text": "Cancel",
          "emoji": true
        },
        "blocks": [
          {
            "type": "input",
            "block_id": "description",
            "element": {
              "type": "plain_text_input",
              "action_id": "plain_text_input-action",
              "placeholder": {
                "type": "plain_text",
                "text": "๋ฌด์—‡์— ๋Œ€ํ•œ ์ •์‚ฐ์ธ๊ฐ€์š”?"
              },
            },
            "label": {
              "type": "plain_text",
              "text": "ํ•ญ๋ชฉ",
              "emoji": true
            }
          },
          {
            "type": "input",
            "block_id": "total",
            "element": {
              "type": "plain_text_input",
              "action_id": "plain_text_input-action",
              "placeholder": {
                "type": "plain_text",
                "text": "์ด ๊ธˆ์•ก์€ ์–ผ๋งˆ์ธ๊ฐ€์š”?"
              },
            },
            "label": {
              "type": "plain_text",
              "text": "์ด ๊ธˆ์•ก",
              "emoji": true
            }
          },
          {
            "type": "input",
            "block_id": "account_info",
            "element": {
              "type": "plain_text_input",
              "action_id": "plain_text_input-action",
              "placeholder": {
                "type": "plain_text",
                "text": "์€ํ–‰๋ช…๊ณผ ๊ณ„์ขŒ๋ฒˆํ˜ธ"
              },
            },
            "label": {
              "type": "plain_text",
              "text": "์€ํ–‰๋ช…๊ณผ ๊ณ„์ขŒ๋ฒˆํ˜ธ",
              "emoji": true
            }
          },
          {
            "type": "input",
            "block_id": "subjects",
            "element": {
              "type": "multi_users_select",
              "placeholder": {
                "type": "plain_text",
                "text": "๋Œ€์ƒ์„ ์„ ํƒํ•ด์ฃผ์„ธ์š” (๋ณธ์ธ์€ ํฌํ•จํ•˜์ง€ ๋ง์•„์ฃผ์„ธ์š”)",
                "emoji": true
              },
              "action_id": "multi_users_select-action"
            },
            "label": {
              "type": "plain_text",
              "text": "๋Œ€์ƒ (๋ณธ์ธ ๋ฏธํฌํ•จ ์ „์›์„ ์„ ํƒํ•ด์ฃผ์„ธ์š”)",
              "emoji": true
            }
          }
        ]
      }
    })
  }
  catch (error) {
    logger.error(error);
  }
});

app.view('split_view', async ({ ack, body, view, client, logger }) => {
  await ack();

  const user = body.user.id;
  const total = view.state.values.total["plain_text_input-action"].value
  const subjects = view.state.values.subjects["multi_users_select-action"].selected_users
  
  const amountDue = total / (subjects.length + 1)
  console.log(total, amountDue)

  try {
    Promise.all(subjects.map(sub => {
      return client.chat.postMessage({
        channel: sub,
        "attachments": [
          {
            "color": "#f2c744",
            "blocks": [
              {
                "type": "header",
                "text": {
                  "type": "plain_text",
                  "text": "๐Ÿค‘ ์ƒˆ๋กœ์šด ์ •์‚ฐ ์š”์ฒญ",
                  "emoji": true
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*ํ•ญ๋ชฉ*: ์ ์‹ฌ ๋ฒ„ํ…์Šค"
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": `*๋Œ€์ƒ*: <@${user}>`
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*๊ณ„์ขŒ*: ์นด๋ฑ… 3333-06-12345"
                }
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": `*๊ธˆ์•ก*: ${amountDue.toLocaleString()}์›`
                }
              }
            ]
          }
        ]
      })
    }))
  }
  catch (error) {
    logger.error(error);
  }
});

(async () => {
  await app.start();

  console.log('Bolt app is running');
})();