11import { getUpcomingEvents } from './utils/googleCalendar.js' ;
2- import { MattermostClient , MattermostMessage } from './utils/mattermost.js' ; // Import MattermostMessage type
2+ import { MattermostClient , MattermostMessage , MattermostAttachment } from './utils/mattermost.js' ; // Import MattermostMessage and MattermostAttachment type
33import { getRandomColor } from './utils/color.js' ; // Import color utility
44
55// Fixed list of quotes, including open-source related ones
@@ -18,9 +18,6 @@ const QUOTES = [
1818 "其實今年的回顧文件已經可以開始填寫:https://s.coscup.org/25review"
1919] ;
2020
21- // No need for quoteIndex anymore
22- // let quoteIndex = 0;
23-
2421async function main ( ) {
2522 const mattermostWebhookUrl = process . env . MATTERMOST_WEBHOOK_URL ;
2623 if ( ! mattermostWebhookUrl ) {
@@ -38,6 +35,8 @@ async function main() {
3835 const events = await getUpcomingEvents ( calendarId ) ;
3936 const mattermostClient = new MattermostClient ( mattermostWebhookUrl ) ; // Create client once
4037
38+ const eventAttachments : MattermostAttachment [ ] = [ ] ;
39+
4140 for ( const event of events ) {
4241 if ( event . start && event . start . dateTime && event . summary ) {
4342 const startTime = new Date ( event . start . dateTime ) ;
@@ -46,37 +45,42 @@ async function main() {
4645 // Select quote randomly
4746 const randomIndex = Math . floor ( Math . random ( ) * QUOTES . length ) ;
4847 const selectedQuote = QUOTES [ randomIndex ] ;
49- // quoteIndex++; // No longer needed
50-
51- // Construct the fancier message with attachments
52- const message : MattermostMessage = {
53- channel : channel ,
54- // text: `提醒:${event.summary}`, // Optional fallback text
55- attachments : [
56- {
57- fallback : `提醒: ${ formattedStartTime } 有 ${ event . summary } 活動!` , // Plain text fallback
58- color : getRandomColor ( ) , // Use random color
59- // pretext: "📅 近期活動提醒",
60- title : `🗓️ ${ event . summary } ` ,
61- title_link : event . htmlLink ,
62- // Use description field from event if available, otherwise fallback
63- text : `**時間:** ${ formattedStartTime }
48+
49+ // Construct the attachment for each event
50+ const attachment : MattermostAttachment = {
51+ fallback : `提醒: ${ formattedStartTime } 有 ${ event . summary } 活動!` , // Plain text fallback
52+ color : getRandomColor ( ) , // Use random color
53+ title : `🗓️ ${ event . summary } ` ,
54+ title_link : event . htmlLink ,
55+ text : `**時間:** ${ formattedStartTime }
6456
6557**地點/連結:** ${ event . location || event . description || event . hangoutLink || '未指定' }
6658
67- *每日一句:${ selectedQuote } *` , // Main content with markdown and random quote. Escaped newlines for JSON.
68- }
69- ]
59+ *每日一句:${ selectedQuote } *` , // Main content with markdown and random quote.
7060 } ;
7161
72- await mattermostClient . sendMessage ( message ) ;
73- console . log ( `已發送會議提醒到 Mattermost: ${ event . summary } ` ) ;
62+ eventAttachments . push ( attachment ) ;
7463
7564 } else {
7665 console . error ( 'event:' , event ) ;
7766 console . error ( 'Error: Incomplete event data.' ) ;
7867 }
7968 }
69+
70+ // Send a single message with all event attachments
71+ if ( eventAttachments . length > 0 ) {
72+ const combinedMessage : MattermostMessage = {
73+ channel : channel ,
74+ text : "COSCUP 今天會議提醒:" , // Add the requested prefix
75+ attachments : eventAttachments ,
76+ } ;
77+
78+ await mattermostClient . sendMessage ( combinedMessage ) ;
79+ console . log ( `已發送合併會議提醒到 Mattermost,共 ${ eventAttachments . length } 個事件` ) ;
80+ } else {
81+ console . log ( "沒有即將到來的會議。" ) ;
82+ }
83+
8084 } catch ( error ) {
8185 console . error ( '處理 Google Calendar 事件或發送 Mattermost 訊息時發生錯誤:' , error ) ;
8286 }
0 commit comments