|
// Dealing time |
|
const getTimeDetail = (calendar) => { |
|
// 全天事件 |
|
if (calendar.isAllDayEvent()){ |
|
const startDate = getDateFormat(calendar.getAllDayStartDate()) |
|
const endDate = getDateFormat(calendar.getAllDayEndDate(), true) |
|
|
|
const isSameYear = startDate.year === endDate.year |
|
const isSameDate = startDate.text() === endDate.text() |
|
|
|
// 判斷顯示哪種格式 |
|
if (isSameDate) return `${startDate.text()} 整天` |
|
if (isSameYear) return `${startDate.text('MD')} ~ ${endDate.text('MD')}` |
|
return `${startDate.text('YMD')} ~ ${endDate.text('YMD')}` |
|
} |
|
|
|
// 非全天事件 |
|
const startTimeObj = calendar.getStartTime() |
|
const endTimeObj = calendar.getEndTime() |
|
const startTimeText = getHourAndMin(startTimeObj) |
|
const endTimeText = getHourAndMin(endTimeObj) |
|
return startTimeText && startTimeText ? `${startTimeText} ~ ${endTimeText}` : '' |
|
} |
|
|
|
function sendTodayCalendarInfoByNotify({ notify_token, calendarID }) { |
|
const Authorization = "Bearer " + notify_token |
|
const {dateStart, dateEnd} = getNowStartAndEndDate() |
|
const calendarData = CalendarApp.getCalendarById(calendarID).getEvents(dateStart, dateEnd); |
|
|
|
const emptyContentText = '空' |
|
const noEventMsg = '今天很空~' |
|
|
|
// 判斷 notify server status |
|
const {status, message, target} = notifyStatus(Authorization) |
|
if (status !== 200){ |
|
console.error('Notify failed: ', message) |
|
return |
|
} |
|
if (target === null){ |
|
console.error('Notify failed: target is null. ', message) |
|
return |
|
} |
|
|
|
// 組合訊息 |
|
const taskMsgs = [ |
|
`Hi! ${target}\n`, |
|
...[...calendarData].map((calendar, index) => { |
|
const month = calendar.getStartTime().getMonth() + 1 |
|
const date = calendar.getStartTime().getDate() |
|
|
|
const title = calendar.getTitle() || emptyContentText |
|
const description = calendar.getDescription() || emptyContentText |
|
const location = calendar.getLocation() || emptyContentText |
|
const msgSentDateText = index === 0 ? `> ${month} 月 ${date} 號\n`: '' |
|
|
|
return [ |
|
'', |
|
msgSentDateText, |
|
`--------- 任務 ${index + 1} ---------`, |
|
`| ${title} `, |
|
'', |
|
`| 地點:`, |
|
`${location}`, |
|
'', |
|
`| 內容:`, |
|
`${description}`, |
|
'', |
|
`| 時間:`, |
|
getTimeDetail(calendar), |
|
'' |
|
].join('\n') |
|
}) |
|
] |
|
|
|
// 判斷是否有活動 |
|
taskMsgs.length <= 1 && taskMsgs.push(`\n ${noEventMsg}`) |
|
|
|
// 送訊息 |
|
console.log(taskMsgs.join('')) |
|
notifySendMsg(Authorization, taskMsgs.join('')) |
|
} |