generated from slack-samples/bolt-js-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
75 lines (67 loc) · 1.96 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { App, LogLevel } = require('@slack/bolt');
const { config } = require('dotenv');
config();
/** Initialization */
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN,
logLevel: LogLevel.DEBUG,
});
/** Sample Function Listener */
app.function('sample_step', async ({ client, inputs, logger, fail }) => {
try {
const { user_id } = inputs;
await client.chat.postMessage({
channel: user_id,
text: 'Click the button to signal the step has completed',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Click the button to signal the step has completed',
},
accessory: {
type: 'button',
text: {
type: 'plain_text',
text: 'Complete step',
},
action_id: 'sample_button',
},
},
],
});
} catch (error) {
logger.error(error);
fail({ error: `Failed to complete the step: ${error}` });
}
});
/** Sample Action Listener */
app.action('sample_button', async ({ body, client, logger, complete, fail }) => {
const { channel, message, user } = body;
try {
// Steps should be marked as successfully completed using `complete` or
// as having failed using `fail`, else they'll remain in an 'In progress' state.
// Learn more at https://api.slack.com/automation/interactive-messages
await complete({ outputs: { user_id: user.id } });
await client.chat.update({
channel: channel.id,
ts: message.ts,
text: 'Step completed successfully!',
});
} catch (error) {
logger.error(error);
fail({ error: `Failed to handle a step request: ${error}` });
}
});
/** Start the Bolt App */
(async () => {
try {
await app.start();
app.logger.info('⚡️ Bolt app is running!');
} catch (error) {
app.logger.error('Failed to start the app', error);
}
})();