Skip to content

Commit d8e74a2

Browse files
committed
Send reminder message to both #vc-events and optional second channel
The diff does not look nice, but there are two main changes: For each event, instead of choosing between DEFAULT_SLACK_EVENT_CHANNEL and event.eventSlackAnnouncementsChannelId (and preferring event.eventSlackAnnouncementsChannelId over DEFAULT_SLACK_EVENT_CHANNEL) we create a message for DEFAULT_SLACK_EVENT_CHANNEL and then if event.eventSlackAnnouncementsChannelId is set, then we also create a message for that channel. We apply the same logic for the hourlyAdminMessage to generate a list of channels it got posted to.
1 parent e1a469a commit d8e74a2

File tree

1 file changed

+86
-74
lines changed
  • functions/event-reminders-hourly

1 file changed

+86
-74
lines changed

functions/event-reminders-hourly/index.js

Lines changed: 86 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function createEventsQuery(calendars) {
5151
`;
5252
}
5353

54-
const handler = async function (event, context) {
54+
const handler = async function (handlerEvent, handlerContext) {
5555
const graphQLClient = new GraphQLClient(`${process.env.CMS_URL}/api`, {
5656
headers: {
5757
Authorization: `bearer ${process.env.CMS_TOKEN}`,
@@ -89,90 +89,98 @@ const handler = async function (event, context) {
8989
});
9090

9191
if (filteredList.length) {
92-
const hourlyMessages = filteredList.map((event) => {
92+
const hourlyMessages = filteredList.flatMap((event) => {
9393
const eventDate = DateTime.fromISO(event.startDateLocalized);
9494

95-
const message = {
96-
channel:
97-
event.eventSlackAnnouncementsChannelId ||
98-
DEFAULT_SLACK_EVENT_CHANNEL,
99-
text: `Starting soon: ${event.title}: ${eventDate.toFormat(
100-
'EEEE, fff'
101-
)}`,
102-
unfurl_links: false,
103-
unfurl_media: false,
104-
blocks: [
105-
{
106-
type: 'header',
107-
text: {
108-
type: 'plain_text',
109-
text: '⏰ Starting Soon:',
110-
emoji: true,
111-
},
112-
},
113-
],
114-
};
95+
const announcementsChannels = [DEFAULT_SLACK_EVENT_CHANNEL];
96+
if (event.eventSlackAnnouncementsChannelId && event.eventSlackAnnouncementsChannelId !== DEFAULT_SLACK_EVENT_CHANNEL) {
97+
announcementsChannels.push(event.eventSlackAnnouncementsChannelId);
98+
}
11599

116-
const titleBlock = {
117-
type: 'section',
118-
text: {
119-
type: 'mrkdwn',
120-
text: `*${
121-
event.title
122-
}*\n<!date^${eventDate.toSeconds()}^{date_long_pretty} {time}|${eventDate.toFormat(
100+
const createMessage = (channel) => {
101+
const message = {
102+
channel: channel,
103+
text: `Starting soon: ${event.title}: ${eventDate.toFormat(
123104
'EEEE, fff'
124-
)}>`,
125-
},
126-
};
127-
128-
if (
129-
event.eventJoinLink &&
130-
event.eventJoinLink.substring(0, 4) === 'http'
131-
) {
132-
titleBlock.accessory = {
133-
type: 'button',
134-
text: {
135-
type: 'plain_text',
136-
text: 'Join Event',
137-
emoji: true,
138-
},
139-
value: `join_event_${event.id}`,
140-
url: event.eventJoinLink,
141-
action_id: 'button-join-event',
105+
)}`,
106+
unfurl_links: false,
107+
unfurl_media: false,
108+
blocks: [
109+
{
110+
type: 'header',
111+
text: {
112+
type: 'plain_text',
113+
text: '⏰ Starting Soon:',
114+
emoji: true,
115+
},
116+
},
117+
],
142118
};
143-
}
144119

145-
message.blocks.push(titleBlock);
146-
147-
if (
148-
event.eventJoinLink &&
149-
event.eventJoinLink.substring(0, 4) !== 'http'
150-
) {
151-
message.blocks.push({
120+
const titleBlock = {
152121
type: 'section',
153122
text: {
154123
type: 'mrkdwn',
155-
text: `*Location:* ${event.eventJoinLink}`,
124+
text: `*${
125+
event.title
126+
}*\n<!date^${eventDate.toSeconds()}^{date_long_pretty} {time}|${eventDate.toFormat(
127+
'EEEE, fff'
128+
)}>`,
156129
},
157-
});
158-
}
130+
};
159131

160-
message.blocks.push(
161-
{
162-
type: 'context',
163-
elements: [
164-
{
132+
if (
133+
event.eventJoinLink &&
134+
event.eventJoinLink.substring(0, 4) === 'http'
135+
) {
136+
titleBlock.accessory = {
137+
type: 'button',
138+
text: {
139+
type: 'plain_text',
140+
text: 'Join Event',
141+
emoji: true,
142+
},
143+
value: `join_event_${event.id}`,
144+
url: event.eventJoinLink,
145+
action_id: 'button-join-event',
146+
};
147+
}
148+
149+
message.blocks.push(titleBlock);
150+
151+
if (
152+
event.eventJoinLink &&
153+
event.eventJoinLink.substring(0, 4) !== 'http'
154+
) {
155+
message.blocks.push({
156+
type: 'section',
157+
text: {
165158
type: 'mrkdwn',
166-
text: slackify(event.eventCalendarDescription),
159+
text: `*Location:* ${event.eventJoinLink}`,
167160
},
168-
],
169-
},
170-
{
171-
type: 'divider',
161+
});
172162
}
173-
);
174163

175-
return message;
164+
message.blocks.push(
165+
{
166+
type: 'context',
167+
elements: [
168+
{
169+
type: 'mrkdwn',
170+
text: slackify(event.eventCalendarDescription),
171+
},
172+
],
173+
},
174+
{
175+
type: 'divider',
176+
}
177+
);
178+
179+
return message;
180+
};
181+
182+
const messages = announcementsChannels.map(createMessage);
183+
return messages;
176184
});
177185

178186
const hourlyAdminMessage = {
@@ -227,6 +235,11 @@ const handler = async function (event, context) {
227235
};
228236
}
229237

238+
const channels = [DEFAULT_SLACK_EVENT_CHANNEL];
239+
if (event.eventSlackAnnouncementsChannelId && event.eventSlackAnnouncementsChannelId !== DEFAULT_SLACK_EVENT_CHANNEL) {
240+
channels.push(event.eventSlackAnnouncementsChannelId);
241+
}
242+
230243
return [
231244
...list,
232245
titleBlock,
@@ -252,10 +265,9 @@ const handler = async function (event, context) {
252265
type: 'section',
253266
text: {
254267
type: 'mrkdwn',
255-
text: `*Announcement posted to:* <#${
256-
event.eventSlackAnnouncementsChannelId ||
257-
DEFAULT_SLACK_EVENT_CHANNEL
258-
}>`,
268+
text:
269+
`*Announcement posted to:* ` +
270+
channels.map((channel) => `<#${channel}>`).join(', '),
259271
},
260272
},
261273
{

0 commit comments

Comments
 (0)