generated from slack-samples/bolt-ts-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
79 lines (71 loc) · 2.5 KB
/
app.ts
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
76
77
78
79
import { App, type BlockAction, LogLevel } from '@slack/bolt';
import { config } from '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_function', async ({ client, inputs, fail, logger }) => {
try {
const { user_id } = inputs;
await client.chat.postMessage({
channel: user_id as string,
text: 'Click the button to signal the function has completed',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Click the button to signal the function has completed',
},
accessory: {
type: 'button',
text: {
type: 'plain_text',
text: 'Complete function',
},
action_id: 'sample_button',
},
},
],
});
} catch (error) {
logger.error(error);
fail({ error: `Failed to handle a function request: ${error}` });
}
});
/** Sample Action Listener */
app.action<BlockAction>('sample_button', async ({ body, client, complete, fail, logger }) => {
const { channel, message, user } = body;
try {
// Functions 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
// biome-ignore lint/style/noNonNullAssertion: we know this button comes from a function, so `fail` is available.
await complete!({ outputs: { user_id: user.id } });
await client.chat.update({
// biome-ignore lint/style/noNonNullAssertion: we know this button was posted to a channel, so `channel` is available.
channel: channel!.id,
// biome-ignore lint/style/noNonNullAssertion: we know this button was posted to a channel, so `message` is available.
ts: message!.ts,
text: 'Function completed successfully!',
});
} catch (error) {
logger.error(error);
// biome-ignore lint/style/noNonNullAssertion: we know this button comes from a function, so `fail` is available.
fail!({ error: `Failed to handle a function 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);
}
})();