I’m working on a slack bot in JS, and using Slack’s “block kit” to format the messages. However, quite often I get a function that looks like this:
JavaScript
x
69
69
1
app.command('/workspace-request', async ({ command, ack, respond }) => {
2
await ack();
3
4
//posts message to user
5
await app.client.chat.postEphemeral({
6
channel: command.channel_id,
7
user: command.user_id,
8
text: "Your request has been sent"
9
})
10
11
const blocks = [
12
{
13
"type": "section",
14
"text": {
15
"type": "mrkdwn",
16
"text": "New Workspace Request:"
17
}
18
},
19
{
20
"type": "section",
21
"text": {
22
"type": "mrkdwn",
23
"text": `*User:* @${command.user_name}`
24
}
25
},
26
{
27
"type": "section",
28
"text": {
29
"type": "mrkdwn",
30
"text": `*Description:* ${command.text}`
31
}
32
},
33
{
34
"type": "actions",
35
"elements": [
36
{
37
"type": "button",
38
"text": {
39
"type": "plain_text",
40
"text": "Add to tasks"
41
},
42
"style": "primary",
43
"value": "RequestApprove"
44
},
45
{
46
"type": "button",
47
"text": {
48
"type": "plain_text",
49
"text": "Deny"
50
},
51
"style": "danger",
52
"value": "RequestDeny"
53
}
54
]
55
}
56
]
57
58
59
//posts message to workspace core
60
await app.client.chat.postMessage({
61
channel: "workspace-core",
62
link_names: true,
63
blocks: blocks
64
65
66
})
67
68
console.log(command)
69
});
Advertisement
Answer
Usual method is that you can keep the block kit as separate json file. And read block from that file.
Please find sample code below:
JavaScript
1
12
12
1
fs.readFile(filename, 'utf8', async (err, data) => {
2
if (err) throw err;
3
try {
4
const block = JSON.parse(data);
5
6
**// use block accordingly**
7
8
} catch (e) {
9
10
}
11
});
12