-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalendar-meeting-notifier.js
More file actions
1431 lines (1214 loc) · 49.2 KB
/
Copy pathcalendar-meeting-notifier.js
File metadata and controls
1431 lines (1214 loc) · 49.2 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* LLM-D Calendar Meeting Notifier
*
* Automatically checks a shared Google Calendar every minute and posts Slack notifications
* for meetings that are starting RIGHT NOW.
*
* Features:
* - Runs every minute for precise timing
* - Notifies only when meetings are actually starting (1min early to 15sec late window)
* - Sends only ONE notification per meeting (prevents duplicate alerts)
* - Posts to specific SIG channels + #community (except Community Meeting which only goes to #community)
* - Includes Google Meet links in visually appealing format
* - Automatic cleanup of old notification records
* - Debug mode for testing message formatting
*
* Timing Logic:
* - Searches for meetings starting within 1 minute early to 15 seconds late of current time
* - Prevents early notifications while accounting for trigger timing variations
* - Notifications sent AT meeting start time, with minimal early notification window
*
* Storage Management:
* - Uses PropertiesService to track which meetings have been notified
* - Smart cleanup strategy adapts to current storage usage
* - Daily end-of-day cleanup (11:30 PM) removes records older than 6 hours
* - Emergency cleanup prevents hitting 50-property limit
* - Storage monitoring and health alerts
* - Prevents duplicate notifications when script runs every minute
*
* Setup Instructions: See CALENDAR_MEETING_NOTIFIER.md
*/
// Load configuration - create config.js based on config.example.js
// This will be loaded automatically when the script runs
/**
* Main function to check calendar and send notifications
* Called by the scheduled trigger every minute
*/
function checkCalendarAndNotify() {
try {
console.log('🕐 Starting calendar check at:', new Date().toISOString());
// Clean up old notification records periodically (every 10 minutes)
const currentMinute = new Date().getMinutes();
if (currentMinute % 10 === 0) {
console.log('🧹 Running cleanup of old notification records...');
cleanupOldNotificationRecords();
}
// Configuration should be loaded from config.js in this script
// Get meetings starting RIGHT NOW (within tighter notification window)
const meetingsStartingNow = getUpcomingMeetings(CONFIG);
if (meetingsStartingNow.length === 0) {
console.log('📅 No meetings starting now');
return;
}
console.log(`📅 Found ${meetingsStartingNow.length} meeting(s) starting NOW`);
// Process each meeting
for (const meeting of meetingsStartingNow) {
processMeeting(meeting, CONFIG);
}
} catch (error) {
console.error('❌ Error in checkCalendarAndNotify:', error);
sendErrorNotification('Calendar check failed', error.toString());
}
}
/**
* Get meetings starting RIGHT NOW (within tighter 1min early to 15sec late window)
*/
function getUpcomingMeetings() {
try {
const now = new Date();
// Look for meetings starting within the next 3 minutes (broader search to find candidates)
const searchStart = new Date(now.getTime() - (90 * 1000)); // 90 seconds ago
const searchEnd = new Date(now.getTime() + (3 * 60 * 1000)); // 3 minutes from now
console.log(`🕐 Current time: ${now.toLocaleTimeString()}`);
console.log(`📅 Search window: ${searchStart.toLocaleTimeString()} - ${searchEnd.toLocaleTimeString()}`);
// Get the calendar by ID (configured in CONFIG)
const calendar = CalendarApp.getCalendarById(CONFIG.CALENDAR_ID);
if (!calendar) {
throw new Error(`Calendar not found with ID: ${CONFIG.CALENDAR_ID}`);
}
// Get events in the search window
const events = calendar.getEvents(searchStart, searchEnd);
const meetingsStartingNow = [];
for (const event of events) {
const title = event.getTitle();
const startTime = event.getStartTime();
// Skip meetings with "Canceled" in the title (case-insensitive)
if (title.toLowerCase().includes('canceled')) {
console.log(`⏭️ Skipping canceled meeting: "${title}"`);
continue;
}
// Calculate how many seconds until/since the meeting starts
const timeUntilStart = startTime.getTime() - now.getTime();
const secondsUntilStart = Math.floor(timeUntilStart / 1000);
console.log(`📋 Checking meeting: "${title}"`);
console.log(`🕐 Meeting starts: ${startTime.toLocaleTimeString()}`);
console.log(`⏱️ Seconds until start: ${secondsUntilStart}`);
// Only include meetings starting within 1 minute early to 15 seconds late (tighter window)
// This prevents 2-minute early notifications while still allowing for trigger timing variations
if (secondsUntilStart >= -60 && secondsUntilStart <= 15) {
console.log(`✅ Meeting is starting NOW (within 1min early to 15sec late)`);
// Check if this meeting matches any of our configured prefixes
const matchedConfig = findMatchingMeetingConfig(title);
if (matchedConfig) {
// EARLY CACHE CHECK: Skip expensive processing if already notified
if (hasAlreadyNotifiedEvent(event)) {
console.log(`⏭️ Skipping - already notified for "${title}"`);
continue;
}
// Only do expensive meeting details extraction if we haven't notified yet
const meetingDetails = extractMeetingDetails(event);
meetingsStartingNow.push({
title: title,
startTime: startTime,
meetLink: meetingDetails.meetLink,
documents: meetingDetails.documents,
hasDocuments: meetingDetails.hasDocuments,
config: matchedConfig,
event: event
});
console.log(`📋 Added to notification queue: ${title}`);
} else {
console.log(`⏭️ Meeting starting now but no config match: ${title}`);
}
} else {
console.log(`⏭️ Meeting not starting now (${secondsUntilStart}s away) - skipping`);
}
}
if (meetingsStartingNow.length > 0) {
console.log(`🎯 Found ${meetingsStartingNow.length} meeting(s) starting NOW`);
} else {
console.log(`📅 No meetings starting within notification window (1min early to 15sec late)`);
}
return meetingsStartingNow;
} catch (error) {
console.error('❌ Error getting meetings starting now:', error);
throw error;
}
}
/**
* Extract file ID from Google Drive URL
*/
function extractFileIdFromUrl(url) {
const patterns = [
/\/file\/d\/([a-zA-Z0-9-_]+)/,
/\/document\/d\/([a-zA-Z0-9-_]+)/,
/\/spreadsheets\/d\/([a-zA-Z0-9-_]+)/,
/\/presentation\/d\/([a-zA-Z0-9-_]+)/,
/[?&]id=([a-zA-Z0-9-_]+)/,
/\/open\?id=([a-zA-Z0-9-_]+)/
];
for (const pattern of patterns) {
const match = url.match(pattern);
if (match) {
console.log(`✅ Extracted file ID ${match[1]} from URL using pattern: ${pattern}`);
return match[1];
}
}
console.log(`❌ Could not extract file ID from URL: ${url}`);
return null;
}
/**
* Get file name from Google Drive using the Drive API
*/
function getFileNameFromDrive(fileId) {
try {
const file = DriveApp.getFileById(fileId);
return file.getName();
} catch (error) {
console.log(`ℹ️ Could not get file name for ID ${fileId}:`, error.message);
return null;
}
}
/**
* Try to use the advanced Calendar API to get more event details
*/
function getAdvancedEventDetails(event) {
try {
// Try to use the advanced Calendar API if available
const eventId = event.getId();
const calendarId = CONFIG.CALENDAR_ID;
console.log('🔍 Attempting advanced Calendar API access...');
console.log('📋 Event ID:', eventId);
console.log('📅 Calendar ID:', calendarId);
// Try to access the Calendar API directly for more detailed information
// This requires the Calendar API to be enabled
const calendarService = Calendar.Events;
if (calendarService) {
const detailedEvent = calendarService.get(calendarId, eventId.split('@')[0]);
console.log('✅ Advanced Calendar API access successful');
console.log('🔍 Conference data:', detailedEvent.conferenceData ? 'Found' : 'None');
console.log('🔍 Attachments:', detailedEvent.attachments ? detailedEvent.attachments.length : 0);
console.log('🔍 Extended properties:', detailedEvent.extendedProperties ? 'Found' : 'None');
return {
conferenceData: detailedEvent.conferenceData,
attachments: detailedEvent.attachments || [],
extendedProperties: detailedEvent.extendedProperties,
htmlLink: detailedEvent.htmlLink,
description: detailedEvent.description
};
}
} catch (apiError) {
console.log('ℹ️ Advanced Calendar API not available or not enabled:', apiError.message);
console.log('💡 You may need to enable the Calendar API in your Google Apps Script project');
}
return null;
}
/**
* Extract Google Meet link and attached documents from calendar event
*/
function extractMeetingDetails(event) {
try {
const description = event.getDescription() || '';
const location = event.getLocation() || '';
// Debug: Show what we're working with
console.log('🔍 Event debugging:');
console.log('📝 Description length:', description.length);
console.log('📍 Location:', location);
console.log('📝 Description preview:', description.substring(0, 200) + (description.length > 200 ? '...' : ''));
// Try advanced Calendar API first
const advancedDetails = getAdvancedEventDetails(event);
let meetLink = null;
const driveLinks = [];
if (advancedDetails) {
// Extract Meet link from conference data
if (advancedDetails.conferenceData && advancedDetails.conferenceData.entryPoints) {
const meetEntry = advancedDetails.conferenceData.entryPoints.find(ep =>
ep.entryPointType === 'video' && ep.uri && ep.uri.includes('meet.google.com')
);
if (meetEntry) {
meetLink = meetEntry.uri;
console.log('✅ Found Meet link from conference data:', meetLink);
}
}
// Extract documents from attachments
if (advancedDetails.attachments && advancedDetails.attachments.length > 0) {
advancedDetails.attachments.forEach(attachment => {
if (attachment.fileUrl) {
driveLinks.push(attachment.fileUrl);
console.log('✅ Found attachment:', attachment.fileUrl);
}
});
}
// Also check the HTML description for additional links
if (advancedDetails.description && advancedDetails.description.length > description.length) {
console.log('✅ Found richer description from API');
const richDescription = advancedDetails.description;
const richDriveRegex = /https:\/\/(?:docs|drive)\.google\.com\/[^\s\)\>\"]+/g;
const richDriveLinks = richDescription.match(richDriveRegex) || [];
richDriveLinks.forEach(link => {
if (!driveLinks.includes(link)) {
driveLinks.push(link);
console.log('✅ Found additional Drive link from rich description:', link);
}
});
}
}
// Fallback to basic description/location parsing if advanced API didn't work
if (!meetLink) {
console.log('🔍 Falling back to basic description/location parsing...');
const meetRegex = /https:\/\/meet\.google\.com\/[a-z-]+/g;
let meetMatch = description.match(meetRegex);
if (!meetMatch) {
meetMatch = location.match(meetRegex);
}
if (meetMatch) {
meetLink = meetMatch[0];
console.log('✅ Found Meet link in description/location:', meetLink);
}
}
// Fallback document parsing if we didn't find any from attachments
if (driveLinks.length === 0) {
console.log('🔍 Falling back to basic document link parsing...');
const driveRegex = /https:\/\/(?:docs|drive)\.google\.com\/[^\s\)\>]+/g;
const basicDriveLinks = description.match(driveRegex) || [];
driveLinks.push(...basicDriveLinks);
}
// Extract file names for each document
const documentsWithNames = driveLinks.map((url, index) => {
console.log(`🔍 Processing document ${index + 1}: ${url}`);
const fileId = extractFileIdFromUrl(url);
let fileName = null;
let fileType = '📁';
if (fileId) {
console.log(`📎 Extracted file ID: ${fileId}`);
fileName = getFileNameFromDrive(fileId);
console.log(`📎 File name: ${fileName}`);
// Determine file type icon based on URL
if (url.includes('/document/')) {
fileType = '📄';
} else if (url.includes('/spreadsheets/')) {
fileType = '📊';
} else if (url.includes('/presentation/')) {
fileType = '📑';
}
} else {
console.log(`❌ Could not extract file ID from: ${url}`);
}
return {
url: url,
fileName: fileName,
fileType: fileType,
displayName: fileName || 'Google Drive File'
};
});
console.log(`📊 Final results: Meet link: ${meetLink ? 'Found' : 'Not found'}, Documents: ${documentsWithNames.length}`);
return {
meetLink: meetLink,
documents: documentsWithNames,
hasDocuments: documentsWithNames.length > 0
};
} catch (error) {
console.error('❌ Error extracting meeting details:', error);
return {
meetLink: null,
documents: [],
hasDocuments: false
};
}
}
/**
* Find matching meeting configuration based on title prefix
*/
function findMatchingMeetingConfig(title) {
for (const [prefix, config] of Object.entries(CONFIG.MEETING_CONFIGS)) {
if (title.startsWith(prefix)) {
return {
prefix: prefix,
...config
};
}
}
return null;
}
/**
* Get unique identifier for a meeting event to track notifications
*/
function getMeetingNotificationKey(meeting) {
// Use event ID combined with start time to create unique key
const eventId = meeting.event.getId();
const startTimeKey = meeting.startTime.toISOString();
return `notified_${eventId}_${startTimeKey}`;
}
/**
* Get unique identifier for a meeting using basic event info (for early cache check)
*/
function getMeetingNotificationKeyFromEvent(event) {
// Use event ID combined with start time to create unique key
const eventId = event.getId();
const startTimeKey = event.getStartTime().toISOString();
return `notified_${eventId}_${startTimeKey}`;
}
/**
* Check if we have already sent notifications for this event (early check without full processing)
*/
function hasAlreadyNotifiedEvent(event) {
const key = getMeetingNotificationKeyFromEvent(event);
const properties = PropertiesService.getScriptProperties();
const notificationRecord = properties.getProperty(key);
if (notificationRecord) {
const recordData = JSON.parse(notificationRecord);
console.log(`✅ Already notified for event "${event.getTitle()}" at ${recordData.notifiedAt}`);
return true;
}
return false;
}
/**
* Check if we have already sent notifications for this meeting
*/
function hasAlreadyNotified(meeting) {
const key = getMeetingNotificationKey(meeting);
const properties = PropertiesService.getScriptProperties();
const notificationRecord = properties.getProperty(key);
if (notificationRecord) {
const recordData = JSON.parse(notificationRecord);
console.log(`✅ Already notified for meeting "${meeting.title}" at ${recordData.notifiedAt}`);
return true;
}
return false;
}
/**
* Record that we have sent notifications for this meeting
*/
function recordNotificationSent(meeting) {
// Skip saving notification records in debug mode to avoid blocking real notifications
if (CONFIG.DEBUG_MODE) {
console.log(`🧪 Debug mode - NOT recording notification for "${meeting.title}" (test only)`);
return;
}
const key = getMeetingNotificationKey(meeting);
const properties = PropertiesService.getScriptProperties();
const recordData = {
meetingTitle: meeting.title,
meetingStart: meeting.startTime.toISOString(),
notifiedAt: new Date().toISOString()
};
properties.setProperty(key, JSON.stringify(recordData));
console.log(`📝 Recorded notification sent for meeting "${meeting.title}"`);
}
/**
* Clean up old notification records with smart aging strategy
* Prevents PropertiesService storage from hitting the 50 property limit
*/
function cleanupOldNotificationRecords() {
try {
const properties = PropertiesService.getScriptProperties();
const allProperties = properties.getProperties();
const now = new Date();
// Count notification records to check against limits
const notificationRecords = [];
for (const [key, value] of Object.entries(allProperties)) {
if (key.startsWith('notified_')) {
try {
const recordData = JSON.parse(value);
notificationRecords.push({
key: key,
data: recordData,
notifiedTime: new Date(recordData.notifiedAt)
});
} catch (parseError) {
// Corrupted record - mark for deletion
notificationRecords.push({
key: key,
data: null,
notifiedTime: new Date(0) // Very old date to ensure deletion
});
}
}
}
console.log(`📊 Current notification records: ${notificationRecords.length}/50 (PropertiesService limit)`);
let cleanedCount = 0;
// Strategy 1: Always remove corrupted records
const corruptedRecords = notificationRecords.filter(record => !record.data);
for (const record of corruptedRecords) {
properties.deleteProperty(record.key);
cleanedCount++;
console.log(`🧹 Cleaned up corrupted notification record: ${record.key}`);
}
// Strategy 2: Aggressive cleanup based on current load
let cutoffHours;
if (notificationRecords.length >= 45) {
// Near limit - very aggressive cleanup (4 hours)
cutoffHours = 4;
console.log('⚠️ Near PropertiesService limit - using aggressive 4-hour cleanup');
} else if (notificationRecords.length >= 30) {
// Getting full - moderate cleanup (8 hours)
cutoffHours = 8;
console.log('📈 Storage getting full - using 8-hour cleanup');
} else {
// Normal cleanup (24 hours)
cutoffHours = 24;
}
const cutoffTime = new Date(now.getTime() - (cutoffHours * 60 * 60 * 1000));
// Strategy 3: Time-based cleanup
for (const record of notificationRecords) {
if (record.data && record.notifiedTime < cutoffTime) {
properties.deleteProperty(record.key);
cleanedCount++;
console.log(`🧹 Cleaned up old notification record (${cutoffHours}h): ${record.data.meetingTitle}`);
}
}
// Strategy 4: Emergency cleanup if still near limit
const remainingRecords = notificationRecords.length - cleanedCount;
if (remainingRecords >= 48) { // Leave room for new meetings
console.log('🚨 EMERGENCY: Still near limit after cleanup - removing oldest records');
// Sort by notification time and remove oldest
const sortedRecords = notificationRecords
.filter(record => record.data) // Only valid records
.sort((a, b) => a.notifiedTime - b.notifiedTime);
const recordsToRemove = remainingRecords - 40; // Keep it well under 50
for (let i = 0; i < recordsToRemove && i < sortedRecords.length; i++) {
const record = sortedRecords[i];
properties.deleteProperty(record.key);
cleanedCount++;
console.log(`🚨 Emergency cleanup: ${record.data.meetingTitle}`);
}
}
if (cleanedCount > 0) {
console.log(`🧹 Cleaned up ${cleanedCount} notification record(s) using ${cutoffHours}h cutoff`);
// Send alert if we had to use aggressive cleanup
if (cutoffHours < 24) {
const remainingAfterCleanup = notificationRecords.length - cleanedCount;
sendDebugMessage(`⚠️ Used aggressive cleanup (${cutoffHours}h) - had ${notificationRecords.length} records, now ${remainingAfterCleanup}`);
}
} else {
console.log(`✅ No cleanup needed - ${notificationRecords.length} records, ${cutoffHours}h cutoff`);
}
} catch (error) {
console.error('❌ Error cleaning up notification records:', error);
}
}
/**
* Process a single meeting and send notifications
*/
function processMeeting(meeting) {
try {
console.log(`📋 Processing meeting: ${meeting.title}`);
console.log(`🕐 Start time: ${meeting.startTime}`);
// Check if we've already sent notifications for this meeting
if (hasAlreadyNotified(meeting)) {
console.log(`⏭️ Skipping notifications - already sent for "${meeting.title}"`);
return;
}
// Determine which channels to notify
const channelsToNotify = getChannelsToNotify(meeting.config);
for (const channel of channelsToNotify) {
// Format message based on target channel
const message = formatSlackMessage(meeting, channel.name);
sendSlackNotification(channel.webhook, message, channel.name);
}
// Record that we've sent notifications for this meeting
recordNotificationSent(meeting);
} catch (error) {
console.error(`❌ Error processing meeting ${meeting.title}:`, error);
sendErrorNotification(`Failed to process meeting: ${meeting.title}`, error.toString());
}
}
/**
* Determine which channels should receive notifications
*/
function getChannelsToNotify(meetingConfig) {
const channels = [];
// Community Meeting only goes to #community
if (meetingConfig.prefix === '[PUBLIC] llm-d Community Meeting') {
channels.push({
webhook: meetingConfig.slackWebhook,
name: meetingConfig.slackChannel
});
} else {
// SIG meetings go to both their specific channel and #community
channels.push({
webhook: meetingConfig.slackWebhook,
name: meetingConfig.slackChannel
});
// Also send to community channel (find community config)
const communityConfig = CONFIG.MEETING_CONFIGS['[PUBLIC] llm-d Community Meeting'];
if (communityConfig) {
channels.push({
webhook: communityConfig.slackWebhook,
name: communityConfig.slackChannel
});
}
}
return channels;
}
/**
* Extract SIG name from meeting title for a cleaner message
*/
function extractSigName(title) {
// Extract SIG name from titles like "[PUBLIC] llm-d sig-autoscaling"
const sigMatch = title.match(/sig-([a-z-]+)/i);
if (sigMatch) {
return `sig-${sigMatch[1]}`;
}
// Handle Community Meeting
if (title.includes('Community Meeting')) {
return 'Community Meeting';
}
// Fallback to full title
return title;
}
/**
* Format the Slack message with meeting details
* Channel parameter determines the message content
*/
function formatSlackMessage(meeting, targetChannel) {
const sigName = extractSigName(meeting.title);
const isSigMeeting = sigName.startsWith('sig-');
const isCommunityChannel = targetChannel === '#community';
// Create the main message text with status
let messageText;
// Meeting is starting right now
if (isSigMeeting && isCommunityChannel) {
// SIG meeting posted to community channel - include channel link
messageText = `:bell: The weekly public llm-d ${sigName} meeting is starting.\n\nJoin the ${meeting.config.slackChannel} channel for detailed discussion.`;
} else {
// SIG meeting posted to SIG channel OR Community meeting - use simple format
messageText = `:bell: The weekly public llm-d ${sigName} meeting is starting. Join us!`;
}
// Add Google Meet link if available
if (meeting.meetLink) {
messageText += `\n\n:video_camera: <${meeting.meetLink}|Join Google Meet>`;
}
// Add meeting documents if available
if (meeting.hasDocuments && meeting.documents.length > 0) {
messageText += `\n\n:memo: Meeting Notes:`;
meeting.documents.forEach((doc) => {
messageText += `\n• <${doc.url}|${doc.displayName}>`;
});
}
let message = {
"text": messageText,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": messageText
}
}
]
};
return message;
}
/**
* Send Slack notification
*/
function sendSlackNotification(webhookUrl, message, channelName) {
try {
// In debug mode, send to error channel instead
const targetWebhook = CONFIG.DEBUG_MODE ? CONFIG.DEFAULT_WEBHOOK : webhookUrl;
// Create a deep copy of the message to avoid modifying the original
let messageToSend = JSON.parse(JSON.stringify(message));
// Add debug information as a separate block if in debug mode
if (CONFIG.DEBUG_MODE) {
// Add debug header to the message text
const debugPrefix = `🧪 *TEST NOTIFICATION* - This would normally be posted to ${channelName}\n\n`;
messageToSend.text = debugPrefix + messageToSend.text;
messageToSend.blocks[0].text.text = debugPrefix + messageToSend.blocks[0].text.text;
}
const response = UrlFetchApp.fetch(targetWebhook, {
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
},
'payload': JSON.stringify(messageToSend)
});
if (response.getResponseCode() === 200) {
console.log(`✅ Notification sent successfully to ${channelName}`);
} else {
console.error(`❌ Failed to send notification to ${channelName}:`, response.getContentText());
}
} catch (error) {
console.error(`❌ Error sending Slack notification to ${channelName}:`, error);
throw error;
}
}
/**
* Send error notification to debug channel
*/
function sendErrorNotification(title, errorMessage) {
try {
const message = {
"text": `❌ Calendar Notifier Error`,
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "❌ Calendar Notifier Error"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `*${title}*\n\`\`\`${errorMessage}\`\`\``
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": `🕐 ${new Date().toISOString()}`
}
]
}
]
};
UrlFetchApp.fetch(CONFIG.DEFAULT_WEBHOOK, {
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
},
'payload': JSON.stringify(message)
});
} catch (error) {
console.error('❌ Failed to send error notification:', error);
}
}
/**
* Setup function to create the automatic trigger
* Run this once to enable automatic calendar checking on the hour and half hour
*/
function setupCalendarTrigger() {
try {
// Delete existing triggers for this function
const triggers = ScriptApp.getProjectTriggers();
for (const trigger of triggers) {
if (trigger.getHandlerFunction() === 'checkCalendarAndNotify') {
ScriptApp.deleteTrigger(trigger);
}
}
// Create a trigger that runs every minute for maximum precision
// This ensures notifications are sent within 30 seconds of meeting start time
// The function's smart timing logic finds the nearest :00/:30 and prevents duplicate notifications
ScriptApp.newTrigger('checkCalendarAndNotify')
.timeBased()
.everyMinutes(1)
.create();
console.log('✅ Calendar trigger setup successfully - will run every minute');
sendDebugMessage('Calendar trigger setup successfully - will run every minute');
} catch (error) {
console.error('❌ Error setting up calendar trigger:', error);
sendErrorNotification('Failed to setup calendar trigger', error.toString());
}
}
/**
* Test function to show the current timing window logic
*/
function testTimingWindow() {
console.log('🕐 Testing timing window logic...');
const now = new Date();
// Current logic: Look for meetings starting within tighter notification window
const searchStart = new Date(now.getTime() - (90 * 1000)); // 90 seconds ago
const searchEnd = new Date(now.getTime() + (3 * 60 * 1000)); // 3 minutes from now
const notifyStart = new Date(now.getTime() - (60 * 1000)); // 60 seconds ago (1min early)
const notifyEnd = new Date(now.getTime() + (15 * 1000)); // 15 seconds from now
console.log(`🕐 Current time: ${now.toLocaleTimeString()}`);
console.log(`📅 Search window (3min): ${searchStart.toLocaleTimeString()} - ${searchEnd.toLocaleTimeString()}`);
console.log(`🎯 Notification window (1min early-15sec late): ${notifyStart.toLocaleTimeString()} - ${notifyEnd.toLocaleTimeString()}`);
console.log(`✅ Only meetings starting within tighter window get notifications`);
// Show examples of what would happen with meetings at different times
const exampleMeetings = [
{ startTime: new Date(now.getTime() - (2 * 60 * 1000)), desc: '2 minutes ago' },
{ startTime: new Date(now.getTime() - (60 * 1000)), desc: '1 minute ago' },
{ startTime: new Date(now.getTime()), desc: 'right now' },
{ startTime: new Date(now.getTime() + (60 * 1000)), desc: 'in 1 minute' },
{ startTime: new Date(now.getTime() + (2 * 60 * 1000)), desc: 'in 2 minutes' }
];
console.log('\n📋 Examples of meeting timing:');
for (const meeting of exampleMeetings) {
const timeUntilStart = meeting.startTime.getTime() - now.getTime();
const secondsUntilStart = Math.floor(timeUntilStart / 1000);
const wouldNotify = secondsUntilStart >= -60 && secondsUntilStart <= 15;
console.log(` Meeting starting ${meeting.desc} (${secondsUntilStart}s): ${wouldNotify ? '✅ NOTIFY' : '❌ Skip'}`);
}
}
/**
* Test function to run in debug mode
* Use this to test the calendar checking without waiting for the trigger
*/
function testCalendarNotifier() {
console.log('🧪 Running calendar notifier in debug mode...');
try {
// Force debug mode for testing
checkCalendarAndNotifyDebug();
console.log('✅ Test completed successfully');
} catch (error) {
console.error('❌ Test failed:', error);
}
}
/**
* Debug version of the main function that forces debug mode
*/
function checkCalendarAndNotifyDebug() {
// Store original debug mode outside try block to ensure it's available in finally
const originalDebugMode = CONFIG.DEBUG_MODE;
try {
console.log('🕐 Starting calendar check at:', new Date().toISOString());
// Force debug mode for testing
CONFIG.DEBUG_MODE = true;
// Get calendar events for the next 30 minutes
const upcomingMeetings = getUpcomingMeetings();
if (upcomingMeetings.length === 0) {
console.log('📅 No upcoming meetings found');
sendDebugMessage('Test run completed - no upcoming meetings found');
return;
}
console.log(`📅 Found ${upcomingMeetings.length} upcoming meeting(s)`);
// Process each meeting
for (const meeting of upcomingMeetings) {
processMeeting(meeting);
}
} catch (error) {
console.error('❌ Error in checkCalendarAndNotifyDebug:', error);
sendErrorNotification('Debug calendar check failed', error.toString());
} finally {
// Restore original debug mode
CONFIG.DEBUG_MODE = originalDebugMode;
}
}
/**
* Send a debug message to verify Slack connectivity
*/
function sendDebugMessage(message) {
try {
const debugMessage = {
"text": "🧪 Calendar Notifier Debug",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `🧪 *Calendar Notifier Debug*\n${message}\n🕐 ${new Date().toISOString()}`
}
}
]
};
UrlFetchApp.fetch(CONFIG.DEFAULT_WEBHOOK, {
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
},
'payload': JSON.stringify(debugMessage)
});
} catch (error) {
console.error('❌ Failed to send debug message:', error);
}
}
/**
* Function to list upcoming events for debugging
* Use this to see what events are found in your calendar
*/
function debugListUpcomingEvents() {
try {
console.log('🔍 Debugging: Listing upcoming events...');
const now = new Date();
const thirtyMinutesFromNow = new Date(now.getTime() + (30 * 60 * 1000));
const calendar = CalendarApp.getCalendarById(CONFIG.CALENDAR_ID);
if (!calendar) {
console.error(`❌ Calendar not found with ID: ${CONFIG.CALENDAR_ID}`);
return;
}
const events = calendar.getEvents(now, thirtyMinutesFromNow);
console.log(`📅 Found ${events.length} events in the next 30 minutes:`);
for (const event of events) {
console.log('='.repeat(50));
console.log(`📋 Event: ${event.getTitle()}`);
console.log(`🕐 Start: ${event.getStartTime()}`);
console.log(`📍 Location: ${event.getLocation()}`);
console.log(`📝 Description (first 500 chars):`);
const desc = event.getDescription() || '';
console.log(desc.substring(0, 500) + (desc.length > 500 ? '...' : ''));
console.log(`📏 Full description length: ${desc.length} characters`);
// Extract meeting details with full debugging
const meetingDetails = extractMeetingDetails(event);
console.log(`🎥 Meet Link: ${meetingDetails.meetLink || 'None'}`);
console.log(`📎 Documents: ${meetingDetails.documents.length}`);
if (meetingDetails.documents.length > 0) {
meetingDetails.documents.forEach((doc, index) => {
console.log(` ${index + 1}. ${doc.displayName} (${doc.url})`);
});
}
console.log('='.repeat(50));
}
} catch (error) {
console.error('❌ Error debugging events:', error);
}
}
/**
* Test function that finds the next upcoming meeting and sends a notification
* Searches the next 7 days to find a matching meeting
* This helps you see what the actual Slack message will look like
*/
function testNextMeetingNotification() {
// Store original debug mode outside try block to ensure it's available in catch
let originalDebugMode;