136 lines
4.1 KiB
PowerShell
136 lines
4.1 KiB
PowerShell
[string] $dtHost = $ENV:DONETICKHOST
|
|
[string] $dtPort = $ENV:DONETICKPORT
|
|
[string] $dtAPIKey = $ENV:DONETICKAPIKEY
|
|
[string] $appriseWebhookURL = $ENV:APPRISEWEBHOOKURL
|
|
[string] $appriseWebhookTag = $ENV:APPRISEWEBHOOKTAG
|
|
[int[]] $notificationTimes = $ENV:NOTIFICATIONTIMES -split ","
|
|
|
|
|
|
function Send-Notification
|
|
{
|
|
param(
|
|
# Title of the notification
|
|
[Parameter(Mandatory=$true)]
|
|
[string]
|
|
$title,
|
|
|
|
# Content of the notification
|
|
[Parameter(Mandatory=$true)]
|
|
[string]
|
|
$content
|
|
)
|
|
|
|
$headers = @{
|
|
"Content-Type"="application/json"
|
|
}
|
|
$body = @{
|
|
title=$title
|
|
body=$content
|
|
tags="$appriseWebhookTag"
|
|
}
|
|
$json = $body | ConvertTo-Json
|
|
try {
|
|
Invoke-WebRequest -URI $appriseWebhookURL -Method post -Body $json -Headers $headers
|
|
}
|
|
catch {
|
|
Write-Host "Notification Error Encountered: $($global:Error[0])"
|
|
}
|
|
}
|
|
|
|
function Get-Chores
|
|
{
|
|
try {
|
|
$headers = @{
|
|
secretkey = $dtAPIKey
|
|
}
|
|
$results = Invoke-WebRequest -Uri "https://$dtHost`:$dtPort/eapi/v1/chore" -Method Get -Headers $headers
|
|
return ($results.Content | ConvertFrom-Json)
|
|
}
|
|
catch {
|
|
Write-Error "Error fetching chores: $($global:Error[0])"
|
|
}
|
|
}
|
|
|
|
if (-not $notificationTimes) { $notificationTimes = @(8) }
|
|
else { $notificationTimes = $notificationTimes | Sort-Object }
|
|
Write-Host "Notification times: $notificationTimes"
|
|
|
|
while ($true) {
|
|
$today = (Get-Date "23:59:59")
|
|
$chores = Get-Chores
|
|
|
|
$overdueTasks = @()
|
|
$todaysTasks = @()
|
|
|
|
foreach($chore in $chores)
|
|
{
|
|
if ($chore.nextDueDate)
|
|
{
|
|
$dueDate = Get-Date $chore.nextDueDate
|
|
if (($dueDate - $today).Days -lt 0) #OVERDUE
|
|
{
|
|
write-host "$($chore.name) $dueDate is overdue!"
|
|
$overdueTasks += $chore
|
|
}
|
|
elseif (($dueDate - $today) -lt 1) #due today
|
|
{
|
|
write-host "$($chore.name) $dueDate is due today!"
|
|
$todaysTasks += $chore
|
|
# Send-Notification -title "TASK DUE TODAY" -content "$($chore.Name) is due today!"
|
|
}
|
|
else
|
|
{
|
|
write-host "$($chore.name) $dueDate is due in the future"
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if ($overdueTasks.Count -ne 0)
|
|
{
|
|
Write-Host "Sending a notification for $($overdueTasks.Count) overdue tasks"
|
|
$content = "The following tasks are overdue!`n"
|
|
foreach($overdueTask in $overdueTasks)
|
|
{
|
|
$content += "$($overdueTask.Name) was due $(Get-Date $overdueTask.nextDueDate -Format "MM/dd/yyyy HH:mm")`n"
|
|
}
|
|
Send-Notification -title "OVERDUE TASKS" -content $content
|
|
}
|
|
|
|
if ($todaysTasks.Count -ne 0)
|
|
{
|
|
Write-Host "Sending a notification for $($todaysTasks.Count) tasks due today"
|
|
$content = "The following tasks are due today!`n"
|
|
foreach($task in $todaysTasks)
|
|
{
|
|
$content += "$($task.Name) is due $(Get-Date $task.nextDueDate -Format "MM/dd/yyyy HH:mm")`n"
|
|
}
|
|
Send-Notification -title "TODAY'S TASKS" -content $content
|
|
}
|
|
|
|
Write-Host "Finding next notification time..."
|
|
$nextNotificationTomorrow = $true
|
|
foreach($time in $notificationTimes)
|
|
{
|
|
$now = Get-Date
|
|
$diff = $now.Hour - $time
|
|
if ($diff -lt 0) # next notification time
|
|
{
|
|
$nextNotificationTomorrow = $false
|
|
Write-Host "Next notification time is $time`:00"
|
|
$sleepTime = ($diff * -1) * 60 * 60 # hours * mins * seconds
|
|
Write-Host "Sleeping for $sleepTime seconds"
|
|
Start-Sleep -Seconds $sleepTime
|
|
continue # leave loop
|
|
}
|
|
}
|
|
if ($nextNotificationTomorrow)
|
|
{
|
|
$time = $notificationTimes[0]
|
|
Write-Host "Next notification time is $time`:00"
|
|
$diff = 24 - $now.Hour + $time
|
|
$sleepTime = $diff * 60 * 60 # hours * mins * seconds
|
|
Write-Host "Sleeping for $sleepTime seconds"
|
|
Start-Sleep -Seconds $sleepTime
|
|
}
|
|
} |