-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
184 lines (160 loc) · 4.83 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
const { App, ExpressReceiver } = require('@slack/bolt');
const database = require("./database");
const sentCardTemplate = require("./templates/sent_card_template");
const chooseImageTemplate = require("./templates/choose_image_template");
const confirmImageTemplate = require("./templates/confirm-image-template");
const newCardTemplate = require("./templates/new_card_template");
const homeTemplate = require('./templates/home_template');
let homeView;
const receiver = new ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
endpoints: '/slack/events',
});
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
receiver,
});
openModal = async({ body, client }) => {
try {
homeView = await client.views.open({
trigger_id: body.trigger_id,
view: newCardTemplate(),
});
}
catch (error) {
console.error(error);
}
database().addInitialCard(body.user_id || body.user?.id);
}
// COMMANDS
app.command('/carte-de-saint-valentin', async ({ ack, body, client }) => {
await ack();
openModal({ body, client });
});
// VIEWS
app.view({ callback_id: 'new_card_modal', type: 'view_submission' }, async ({ ack, body, client }) => {
try {
await database().getLastUpdatedCardFromUser(body.user.id).then( async (data) => {
newCardFromBD = data[0];
if (newCardFromBD.receiver_id && newCardFromBD.image && newCardFromBD.text) {
await ack();
await client.users.profile.get({ user: newCardFromBD.sender_id }).then( async (currentUser) => {
newCardFromBD.sender = currentUser.profile.real_name_normalized;
await client.users.profile.get({ user: newCardFromBD.receiver_id }).then( async (currentUser) => {
newCardFromBD.receiver = currentUser.profile.real_name_normalized;
await client.chat.postMessage({
channel: newCardFromBD.receiver_id,
blocks: sentCardTemplate(newCardFromBD.receiver, newCardFromBD.sender, newCardFromBD.image, newCardFromBD.text),
});
}, (err) => {
console.log(err)
});
}, (err) => {
console.log(err)
});
} else {
try {
await ack({
response_action: 'errors',
errors: {
'block_image': 'Vous devez remplir tous les champs'
}
});
await client.views.update({
view_id: homeView.view.id,
trigger_id: body.trigger_id,
view: newCardTemplate(true),
});
} catch(error) {
console.log(error);
}
}
});
} catch (error) {
console.error(error);
}
});
app.view({ callback_id: 'confirm_image_modal', type: 'view_closed' }, async ({ ack, body, view, client }) => {
await ack();
try {
database().getLastUpdatedCardFromUser(body.user.id).then(async(data) => {
newCardFromBD = data[0];
const result = await client.views.update({
view_id: homeView.view.id,
trigger_id: body.trigger_id,
view: newCardTemplate(false, newCardFromBD.image),
});
});
} catch(error) {
console.log(error);
}
});
// ACTIONS
app.action('users_select-action', async ({ ack, body, client }) => {
await ack();
try {
const senderId = body.user?.id;
const selectedUserId = body['actions'][0]['selected_user'];
database().updateRecipient(senderId, selectedUserId);
}
catch (error) {
console.error(error);
}
});
app.action('plain_text_input-action', async ({ ack, body }) => {
await ack();
try {
database().updateMessage(body.user?.id, body['actions'][0]['value']);
}
catch (error) {
console.error(error);
}
});
app.action('choose_image-action', async ({ ack, body, client }) => {
await ack();
try {
await client.views.push({
trigger_id: body.trigger_id,
view: chooseImageTemplate(),
});
}
catch (error) {
console.error(error);
}
});
app.action('carte-action', async ({ ack, body, client }) => {
await ack();
try {
database().updateImage(body.user?.id, body.actions[0].value).then( async () => {
const result = await client.views.update({
view_id: body.view.id,
trigger_id: body.trigger_id,
hash: body.view.hash,
view: confirmImageTemplate(body.actions[0].value),
});
});
} catch (error) {
console.log(error);
}
});
app.action('open_modal-action', async ({ ack, body, client }) => {
await ack();
openModal({ body, client });
});
// EVENTS
app.event('app_home_opened', async ({ event, client }) => {
try {
const result = await client.views.publish({
user_id: event.user,
view: homeTemplate(event.user)
});
}
catch (error) {
console.error(error);
}
});
(async () => {
await app.start(process.env.PORT || 3000);
// stats(database, receiver.router, app.client);
console.log('⚡️ Bolt app is running!');
})();