-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (120 loc) · 4.34 KB
/
index.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
const dotenv = require('dotenv');
const { App } = require('@slack/bolt');
dotenv.config();
const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_BOT_TOKEN
});
const engagementUserId = process.env.YOUR_USER_ID;
const engagementUserToken = process.env.YOUR_USER_TOKEN;
const welcomeMessagePart1 = process.env.WELCOME_PART1;
const welcomeMessagePart2 = process.env.WELCOME_PART2;
function titleCase(str) {
str = str.toLowerCase();
str = str.split(' ');
for (let i = 0; i < str.length; i++) {
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1);
}
return str.join(' ');
}
(async () => {
// Start the app
await app.start(process.env.PORT || 3000);
console.log('⚡️ Warm-welcome is running!');
// team_join
app.event('team_join', async ({ event, context }) => {
try {
// Retrieve the ID for the direct message to the new member
let result = await app.client.conversations.open({
token: context.botToken,
users: engagementUserId
});
const imChannelId = result.channel.id;
const message = event.user.real_name + ' joined Slack!';
// Message the new member
result = await app.client.chat.postMessage({
token: context.botToken,
channel: imChannelId,
text: message,
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: message + ' Welcome the new user now?'
}
},
{
type: 'actions',
block_id: 'welcomeFolks',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: 'Now'
},
style: 'primary',
action_id: 'welcome_new_user',
value: event.user.id + ',' + event.user.real_name
},
{
type: 'button',
text: {
type: 'plain_text',
text: 'Nope'
},
style: 'primary',
action_id: 'cancel',
value: event.user.real_name
}
]
}
]
});
} catch (error) {
console.error(error);
}
});
// Listen to welcome_new_user
app.action('welcome_new_user', async ({ action, ack, respond }) => {
const newUser = action.value.split(',');
try {
// Retrieve the ID for the direct message to the new member
let result = await app.client.conversations.open({
token: engagementUserToken,
users: newUser[0]
});
const imChannelId = result.channel.id;
// Send the welcome message
result = await app.client.chat.postMessage({
token: engagementUserToken,
channel: imChannelId,
link_names: true,
text: welcomeMessagePart1 + titleCase(newUser[1].split(' ')[0]) + welcomeMessagePart2,
as_user: 'yes'
});
respond({
text: 'Message sent to *' + newUser[1] + '*',
replace_original: true
});
} catch (error) {
console.error(error);
}
});
// Listen to cancelling
app.action('cancel', async ({ action, ack, respond }) => {
try {
respond({
text: 'Message *not* sent to ' + action.value,
replace_original: true
});
} catch (error) {
console.error(error);
}
});
// Test
app.message('fredisawesome', async ({ message, say }) => {
say('Hello');
});
})();