-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (130 loc) · 4.81 KB
/
index.js
File metadata and controls
148 lines (130 loc) · 4.81 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
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 messages = [process.env.MESSAGE, process.env.MESSAGE_2, process.env.MESSAGE_3]
.filter(Boolean)
.map((msg, i) => {
const [label, ...rest] = msg.split('|||');
return { label: label.trim(), template: rest.join('|||').trim(), actionId: 'welcome_msg_' + i };
});
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: [
...messages.map(msg => ({
type: 'button',
text: {
type: 'plain_text',
text: msg.label
},
style: 'primary',
action_id: msg.actionId,
value: event.user.id + ',' + event.user.real_name
})),
{
type: 'button',
text: {
type: 'plain_text',
text: 'Nope'
},
style: 'danger',
action_id: 'cancel',
value: event.user.real_name
}
]
}
]
});
} catch (error) {
console.error(error);
}
});
// Listen to welcome message buttons
messages.forEach(msg => {
app.action(msg.actionId, async ({ action, ack, respond }) => {
await ack();
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: msg.template.replace('{{name}}', titleCase(newUser[1].split(' ')[0])),
as_user: 'yes',
unfurl_links: false,
unfurl_media: false
});
respond({
text: '*' + msg.label + '* message sent to *' + newUser[1] + '*',
replace_original: true
});
} catch (error) {
console.error(error);
}
});
});
// Listen to cancelling
app.action('cancel', async ({ action, ack, respond }) => {
await ack();
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');
});
})();