-
Notifications
You must be signed in to change notification settings - Fork 388
/
app.js
188 lines (172 loc) · 5.1 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const { App, LogLevel, SocketModeReceiver } = require('@slack/bolt');
const clientOptions = {
// enable this for dev instance
// slackApiUrl: 'https://dev.slack.com/api/'
};
// const socketModeReceiver = new SocketModeReceiver({
// appToken: process.env.APP_TOKEN,
// installerOptions: {
// clientOptions,
// // use the following when running against a dev instance and using OAuth
// // authorizationUrl: 'https://dev.slack.com/oauth/v2/authorize',
// },
// // enable the following if you want to use OAuth
// // clientId: process.env.CLIENT_ID,
// // clientSecret: process.env.CLIENT_SECRET,
// // stateSecret: 'my-state-secret',
// // scopes: ['channels:read', 'chat:write', 'app_mentions:read', 'channels:manage', 'commands'],
// logLevel: LogLevel.DEBUG,
// });
const app = new App({
// receiver: socketModeReceiver,
token: process.env.SLACK_BOT_TOKEN, //disable this if enabling OAuth in socketModeReceiver
// logLevel: LogLevel.DEBUG,
clientOptions,
appToken: process.env.SLACK_APP_TOKEN,
socketMode: true,
logLevel: LogLevel.DEBUG,
});
(async () => {
await app.start();
console.log('⚡️ Bolt app started');
})();
// Publish a App Home
app.event('app_home_opened', async ({ event, client }) => {
await client.views.publish({
user_id: event.user,
view: {
"type": "home",
"blocks": [
{
"type": "section",
"block_id": "section678",
"text": {
"type": "mrkdwn",
"text": "App Home Published"
},
}
]
},
});
});
// Message Shortcut example
app.shortcut('launch_msg_shortcut', async ({ shortcut, body, ack, context, client }) => {
await ack();
console.log(shortcut);
});
// Global Shortcut example
// setup global shortcut in App config with `launch_shortcut` as callback id
// add `commands` scope
app.shortcut('launch_shortcut', async ({ shortcut, body, ack, context, client }) => {
try {
// Acknowledge shortcut request
await ack();
// Call the views.open method using one of the built-in WebClients
const result = await client.views.open({
trigger_id: shortcut.trigger_id,
view: {
type: "modal",
title: {
type: "plain_text",
text: "My App"
},
close: {
type: "plain_text",
text: "Close"
},
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "About the simplest modal you could conceive of :smile:\n\nMaybe <https://api.slack.com/reference/block-kit/interactive-components|*make the modal interactive*> or <https://api.slack.com/surfaces/modals/using#modifying|*learn more advanced modal use cases*>."
}
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: "Psssst this modal was designed using <https://api.slack.com/tools/block-kit-builder|*Block Kit Builder*>"
}
]
}
]
}
});
} catch (error) {
console.error(error);
}
});
// subscribe to 'app_mention' event in your App config
// need app_mentions:read and chat:write scopes
app.event('app_mention', async ({ event, context, client, say }) => {
try {
await say({
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Thanks for the mention <@${event.user}>! Click my fancy button`
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Button",
"emoji": true
},
"value": "click_me_123",
"action_id": "first_button"
}
}
]
});
} catch (error) {
console.error(error);
}
});
// subscribe to `message.channels` event in your App Config
// need channels:read scope
app.message('hello', async ({ message, say }) => {
// say() sends a message to the channel where the event was triggered
// no need to directly use 'chat.postMessage', no need to include token
await say({
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Thanks for the mention <@${message.user}>! Click my fancy button`
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Button",
"emoji": true
},
"value": "click_me_123",
"action_id": "first_button"
}
}
]
});
});
// Listen and respond to button click
app.action('first_button', async ({ action, ack, say, context }) => {
console.log('button clicked');
console.log(action);
// acknowledge the request right away
await ack();
await say('Thanks for clicking the fancy button');
});
// Listen to slash command
// need to add commands permission
// create slash command in App Config
app.command('/socketslash', async ({ command, ack, say }) => {
// Acknowledge command request
await ack();
await say(`${command.text}`);
});