-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
314 lines (279 loc) · 9.55 KB
/
index.js
File metadata and controls
314 lines (279 loc) · 9.55 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
require('dotenv').config();
const { App } = require('@slack/bolt');
const { handleHireMessage, handleConfirmHire, handleRejectHire, handleSubmitHireInfo } = require('./slack');
const DeelAPI = require('./deel');
// Initialize clients
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
// Store pending hires until we get their personal info
const pendingHires = new Map();
// Handle hire messages
app.message(/^\/hire/, async ({ message, client }) => {
await handleHireMessage(message, client);
});
// Handle hire confirmation from manager
app.action('confirm_hire', async ({ body, ack, client }) => {
await ack();
try {
const hireData = JSON.parse(body.actions[0].value);
console.log('Processing hire confirmation for:', hireData);
// First, update the original message to show confirmation
await client.chat.update({
channel: body.channel.id,
ts: body.message.ts,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `✅ Hire confirmed!\n\n*Role:* ${hireData.role}\n*Salary:* ${hireData.salary}\n*Equity:* ${hireData.equity}\n*Start Date:* ${hireData.startDate}`
}
}
],
text: "Hire confirmed"
});
// Extract username from slack handle
const userId = hireData.slackHandle.match(/<@([^>]+)>/)[1];
console.log('Sending DM to user:', userId);
// Open DM channel
const conversationResponse = await client.conversations.open({
users: userId
});
if (!conversationResponse.ok) {
throw new Error(`Failed to open DM channel: ${conversationResponse.error}`);
}
// Send the welcome form
await client.chat.postMessage({
channel: conversationResponse.channel.id,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Hey ${hireData.slackHandle}! Welcome to the team! 🎉\n\nI'm excited to let you know that ${hireData.hiringManager} has confirmed your hire:\n• Role: ${hireData.role}\n• Start Date: ${hireData.startDate}\n\nTo complete your onboarding, please fill out the following information:`
}
},
{
type: "input",
block_id: "full_name",
label: {
type: "plain_text",
text: "Full Legal Name",
emoji: true
},
element: {
type: "plain_text_input",
action_id: "full_name_input",
placeholder: {
type: "plain_text",
text: "Enter your full legal name"
}
}
},
{
type: "input",
block_id: "address",
label: {
type: "plain_text",
text: "Address",
emoji: true
},
element: {
type: "plain_text_input",
action_id: "address_input",
multiline: true,
placeholder: {
type: "plain_text",
text: "Enter your full address"
}
}
},
{
type: "input",
block_id: "personal_email",
label: {
type: "plain_text",
text: "Personal Email",
emoji: true
},
element: {
type: "plain_text_input",
action_id: "personal_email_input",
placeholder: {
type: "plain_text",
text: "Enter your personal email address"
}
}
},
{
type: "input",
block_id: "phone_number",
label: {
type: "plain_text",
text: "Phone Number",
emoji: true
},
element: {
type: "plain_text_input",
action_id: "phone_number_input",
placeholder: {
type: "plain_text",
text: "Enter your phone number"
}
}
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: "Submit Information",
emoji: true
},
style: "primary",
action_id: "submit_hire_info",
value: JSON.stringify({
role: hireData.role,
startDate: hireData.startDate,
slackHandle: hireData.slackHandle
})
}
]
}
],
text: "Welcome to the team! Please fill out your onboarding information."
});
// Notify the hiring manager
await client.chat.postMessage({
channel: body.channel.id,
thread_ts: body.message.ts,
text: `✅ I've sent the onboarding form to ${hireData.slackHandle}. I'll create their Deel profile once they submit their information.`
});
} catch (error) {
console.error('Error in confirm_hire:', error);
await client.chat.postMessage({
channel: body.channel.id,
thread_ts: body.message.ts,
text: `❌ Error: ${error.message}`
});
}
});
// Handle hire info submission from new hire
app.action('submit_hire_info', async ({ body, ack, client }) => {
await ack();
try {
console.log('Received form submission:', JSON.stringify(body.state.values, null, 2));
// Get values from the form
const formData = {
fullName: body.state.values.full_name.full_name_input.value,
address: body.state.values.address.address_input.value,
personalEmail: body.state.values.personal_email.personal_email_input.value,
phoneNumber: body.state.values.phone_number.phone_number_input.value,
currentTitle: body.state.values.current_title?.current_title_input?.value
};
console.log('Parsed form data:', formData);
// Split full name into first and last name
const [firstName, ...lastNameParts] = formData.fullName.trim().split(' ');
const lastName = lastNameParts.join(' ');
if (!firstName || !lastName) {
throw new Error('Please provide both first and last name');
}
console.log('Split name:', { firstName, lastName });
// Get the hire data from the button value
const { role, startDate, slackHandle } = JSON.parse(body.actions[0].value);
console.log('Retrieved hire data:', { role, startDate, slackHandle });
// Verify we have a Deel API token
if (!process.env.DEEL_API_TOKEN) {
throw new Error('DEEL_API_TOKEN is not set in environment variables');
}
// Create candidate in Deel with the provided information
console.log('Creating Deel candidate...');
const deel = new DeelAPI(process.env.DEEL_API_TOKEN);
// Log the API token (first few characters)
const tokenPreview = `${process.env.DEEL_API_TOKEN.substring(0, 5)}...`;
console.log('Using Deel API token:', tokenPreview);
const deelResult = await deel.createCandidate({
firstName,
lastName,
email: formData.personalEmail,
role,
startDate,
country: 'US'
});
console.log('Deel API response:', JSON.stringify(deelResult, null, 2));
if (!deelResult.success) {
throw new Error(`Error creating Deel profile: ${deelResult.error}`);
}
// Update Google Sheets with the submitted information
console.log('Updating Google Sheets...');
const { updateHireData } = require('./sheets');
await updateHireData(slackHandle, {
fullName: formData.fullName,
address: formData.address,
personalEmail: formData.personalEmail,
phoneNumber: formData.phoneNumber,
deelCandidateId: deelResult.candidateId
});
console.log('Google Sheets updated successfully');
// Update the form message to show confirmation
await client.chat.update({
channel: body.channel.id,
ts: body.message.ts,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Thanks for submitting your information! 🎉\n\nI've created your Deel profile and recorded your details. Someone from the team will be in touch with next steps. We're looking forward to working with you! 🚀\n\n*Deel Profile ID:* \`${deelResult.candidateId}\``
}
}
],
text: "Information submitted successfully"
});
// Notify in the original hiring thread
if (body.container?.thread_ts) {
await client.chat.postMessage({
channel: body.channel.id,
thread_ts: body.container.thread_ts,
text: `✅ ${slackHandle} has submitted their information and their Deel profile has been created (ID: ${deelResult.candidateId})`
});
}
} catch (error) {
console.error('Error processing hire info submission:', error);
// Send error message
await client.chat.postMessage({
channel: body.channel.id,
thread_ts: body.message.ts,
text: `Sorry, there was an error processing your information: ${error.message}. Please contact HR for assistance.`
});
}
});
app.action('reject_hire', async ({ body, ack, client }) => {
await ack();
await client.chat.update({
channel: body.channel.id,
ts: body.message.ts,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "❌ Hire cancelled. Please submit a new `/hire` command with the correct details."
}
}
],
text: "Hire cancelled"
});
});
// Error handling for global app errors
app.error(async (error) => {
console.error('Global app error:', error);
});
(async () => {
await app.start(process.env.PORT || 3000);
console.log('⚡️ Bolt app is running!');
})();