Skip to content

Commit 0999083

Browse files
committed
add 3 messages options
1 parent fb2de89 commit 0999083

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

index.js

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ const app = new App({
99
});
1010
const engagementUserId = process.env.YOUR_USER_ID;
1111
const engagementUserToken = process.env.YOUR_USER_TOKEN;
12-
const welcomeMessagePart1 = process.env.WELCOME_PART1;
13-
const welcomeMessagePart2 = process.env.WELCOME_PART2;
12+
const messages = [process.env.MESSAGE, process.env.MESSAGE_2, process.env.MESSAGE_3]
13+
.filter(Boolean)
14+
.map((msg, i) => {
15+
const [label, ...rest] = msg.split('|||');
16+
return { label: label.trim(), template: rest.join('|||').trim(), actionId: 'welcome_msg_' + i };
17+
});
1418

1519
function titleCase(str) {
1620
str = str.toLowerCase();
@@ -58,23 +62,23 @@ function titleCase(str) {
5862
type: 'actions',
5963
block_id: 'welcomeFolks',
6064
elements: [
61-
{
65+
...messages.map(msg => ({
6266
type: 'button',
6367
text: {
6468
type: 'plain_text',
65-
text: 'Now'
69+
text: msg.label
6670
},
6771
style: 'primary',
68-
action_id: 'welcome_new_user',
72+
action_id: msg.actionId,
6973
value: event.user.id + ',' + event.user.real_name
70-
},
74+
})),
7175
{
7276
type: 'button',
7377
text: {
7478
type: 'plain_text',
7579
text: 'Nope'
7680
},
77-
style: 'primary',
81+
style: 'danger',
7882
action_id: 'cancel',
7983
value: event.user.real_name
8084
}
@@ -87,39 +91,45 @@ function titleCase(str) {
8791
}
8892
});
8993

90-
// Listen to welcome_new_user
91-
app.action('welcome_new_user', async ({ action, ack, respond }) => {
92-
93-
const newUser = action.value.split(',');
94-
95-
try {
96-
// Retrieve the ID for the direct message to the new member
97-
let result = await app.client.conversations.open({
98-
token: engagementUserToken,
99-
users: newUser[0]
100-
});
101-
const imChannelId = result.channel.id;
102-
103-
// Send the welcome message
104-
result = await app.client.chat.postMessage({
105-
token: engagementUserToken,
106-
channel: imChannelId,
107-
link_names: true,
108-
text: welcomeMessagePart1 + titleCase(newUser[1].split(' ')[0]) + welcomeMessagePart2,
109-
as_user: 'yes'
110-
});
111-
112-
respond({
113-
text: 'Message sent to *' + newUser[1] + '*',
114-
replace_original: true
115-
});
116-
} catch (error) {
117-
console.error(error);
118-
}
94+
// Listen to welcome message buttons
95+
messages.forEach(msg => {
96+
app.action(msg.actionId, async ({ action, ack, respond }) => {
97+
await ack();
98+
99+
const newUser = action.value.split(',');
100+
101+
try {
102+
// Retrieve the ID for the direct message to the new member
103+
let result = await app.client.conversations.open({
104+
token: engagementUserToken,
105+
users: newUser[0]
106+
});
107+
const imChannelId = result.channel.id;
108+
109+
// Send the welcome message
110+
result = await app.client.chat.postMessage({
111+
token: engagementUserToken,
112+
channel: imChannelId,
113+
link_names: true,
114+
text: msg.template.replace('{{name}}', titleCase(newUser[1].split(' ')[0])),
115+
as_user: 'yes',
116+
unfurl_links: false,
117+
unfurl_media: false
118+
});
119+
120+
respond({
121+
text: '*' + msg.label + '* message sent to *' + newUser[1] + '*',
122+
replace_original: true
123+
});
124+
} catch (error) {
125+
console.error(error);
126+
}
127+
});
119128
});
120129

121130
// Listen to cancelling
122131
app.action('cancel', async ({ action, ack, respond }) => {
132+
await ack();
123133

124134
try {
125135
respond({

0 commit comments

Comments
 (0)