-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-lab-3.js
249 lines (238 loc) · 5.82 KB
/
app-lab-3.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const { App } = require('@slack/bolt')
// Initializes your app with your bot token and signing secret
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
(async () => {
// Start your app
await app.start(process.env.PORT || 3000)
console.log('⚡️Hello World.. Bolt app is running!')
})()
// The echo command simply echoes on command
app.command('/survey-gen', async ({ command, ack, payload, context, respond }) => {
// Acknowledge command request
ack()
if (command.text === '') {
respond({ text: 'Oops, you didn\'t provide a sub-command to your Slash command.' })
} else {
// Open a modal powered by Block Kit
app.client.views.open({
token: context.botToken,
trigger_id: payload.trigger_id,
view: {
// let's save response_url so we can call it in a few moments in the view_submission action handler
private_metadata: command.response_url,
type: 'modal',
callback_id: 'survey',
title: {
type: 'plain_text',
text: `Survey ${command.text}`,
emoji: true
},
submit: {
type: 'plain_text',
text: 'Submit',
emoji: true
},
close: {
type: 'plain_text',
text: 'Cancel',
emoji: true
},
blocks: [
{
type: 'section',
text: {
type: 'plain_text',
text: ':wave: Hello!\n\nWe\'d love to hear from you how we can make this place the best place you’ve ever worked.',
emoji: true
}
},
{
type: 'divider'
},
{
block_id: 'enjoy_working',
type: 'input',
label: {
type: 'plain_text',
text: 'You enjoy working here at Acme & Co',
emoji: true
},
element: {
action_id: 'enjoy_working_action',
type: 'static_select',
placeholder: {
type: 'plain_text',
text: 'Select an item',
emoji: true
},
options: [
{
text: {
type: 'plain_text',
text: 'Strongly Agree',
emoji: true
},
value: 'strongly-agree'
},
{
text: {
type: 'plain_text',
text: 'Agree',
emoji: true
},
value: 'agree'
},
{
text: {
type: 'plain_text',
text: 'Neither agree nor disagree',
emoji: true
},
value: 'neither-agree-nor-disagree'
},
{
text: {
type: 'plain_text',
text: 'Disagree',
emoji: true
},
value: 'disagree'
},
{
text: {
type: 'plain_text',
text: 'Strongly disagree',
emoji: true
},
value: 'strongly-disgree'
}
]
}
},
{
block_id: 'lunch',
type: 'input',
label: {
type: 'plain_text',
text: 'What do you want for our team weekly lunch?',
emoji: true
},
element: {
action_id: 'lunch_action',
type: 'multi_static_select',
placeholder: {
type: 'plain_text',
text: 'Select your favorites',
emoji: true
},
options: [
{
text: {
type: 'plain_text',
text: ':pizza: Pizza',
emoji: true
},
value: 'pizza'
},
{
text: {
type: 'plain_text',
text: ':fried_shrimp: Thai food',
emoji: true
},
value: 'thai-food'
},
{
text: {
type: 'plain_text',
text: ':desert_island: Hawaiian',
emoji: true
},
value: 'hawaiian'
},
{
text: {
type: 'plain_text',
text: ':meat_on_bone: Texas BBQ',
emoji: true
},
value: 'texas-bbq'
},
{
text: {
type: 'plain_text',
text: ':hamburger: Burger',
emoji: true
},
value: 'burger'
},
{
text: {
type: 'plain_text',
text: ':taco: Tacos',
emoji: true
},
value: 'tacos'
},
{
text: {
type: 'plain_text',
text: ':green_salad: Salad',
emoji: true
},
value: 'salad'
},
{
text: {
type: 'plain_text',
text: ':stew: Indian',
emoji: true
},
value: 'indian'
}
]
}
},
{
block_id: 'anything_else',
type: 'input',
label: {
type: 'plain_text',
text: 'Anything else you want to tell us?',
emoji: true
},
element: {
action_id: 'anything_else_action',
type: 'plain_text_input',
multiline: true
},
optional: false
}
]
}
})
}
})
app.view('survey', async ({ ack, body, view, context }) => {
// Acknowledge the view_submission event
ack()
// Process input
const userResponsesRaw = view.state.values
const userResponses = {
enjoy_working: userResponsesRaw.enjoy_working.enjoy_working_action.selected_option.value,
lunch: userResponsesRaw.lunch.lunch_action.selected_options.map((c) => { return c.text.text }),
anything_else: userResponsesRaw.anything_else.anything_else_action.value
}
console.log('=== User responses ===\n', userResponses)
// Send a message back to the conversation
const message = `:wave: Thanks for filling out our survey, <@${body.user.id}>!\n:ear: We hear you loud and clear, you're interested in ${userResponses.lunch.join(' / ')} for lunch, OK!`
// view.metadata has the value of our response_url webhook so that we can respond in the conversation the slash command was initiated in
fetch(view.private_metadata, {
method: "post",
body: JSON.stringify({ text: message }),
headers: { "Content-Type": "application/json" },
})
})