From 76052a87e412f154950804fe38791a8ffb6344a5 Mon Sep 17 00:00:00 2001 From: Kelly Thomas Reardon Date: Sun, 31 May 2026 20:23:49 -0500 Subject: [PATCH] broke apprise notification function into its own module to be used by multiple scripts --- AppriseNotification.psm1 | 33 ++++++++++++++++++++++++++++++++ Dockerfile | 2 +- Start-DoneTickConsumer.ps1 | 5 +++-- Start-DoneTickNotifier.ps1 | 39 +++----------------------------------- 4 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 AppriseNotification.psm1 diff --git a/AppriseNotification.psm1 b/AppriseNotification.psm1 new file mode 100644 index 0000000..57921c8 --- /dev/null +++ b/AppriseNotification.psm1 @@ -0,0 +1,33 @@ +[string] $appriseWebhookURL = $ENV:APPRISEWEBHOOKURL +[string] $appriseWebhookTag = $ENV:APPRISEWEBHOOKTAG + +function Send-AppriseNotification +{ + 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])" + } +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 87dae40..458b630 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ USER 1000:1000 WORKDIR /data COPY ["crontab", "/data/"] COPY --chmod=755 ["main.sh", "/data/"] -COPY ["Start-DoneTickNotifier.ps1", "Start-DoneTickConsumer.ps1", "/data/"] +COPY ["Start-DoneTickNotifier.ps1", "Start-DoneTickConsumer.ps1", "AppriseNotification.psm1", "/data/"] ENTRYPOINT ["/data/main.sh"] diff --git a/Start-DoneTickConsumer.ps1 b/Start-DoneTickConsumer.ps1 index 49f5338..31ad7b4 100644 --- a/Start-DoneTickConsumer.ps1 +++ b/Start-DoneTickConsumer.ps1 @@ -1,5 +1,7 @@ [string] $ListenUrl = "http://+:8080/" +Import-Module ./AppriseNotification.psm1 + function Write-JsonResponse { param( @@ -91,8 +93,7 @@ function Receive-Webhook Write-Host "Payload: " } - # Add your real webhook handling here. - # Example: inspect $payload.event, save data, or call another API. + Send-AppriseNotification -title "Donetick Webhook Received" -content ($payload | ConvertTo-Json -Depth 20) Write-JsonResponse -Response $response -StatusCode 200 -Body @{ ok = $true diff --git a/Start-DoneTickNotifier.ps1 b/Start-DoneTickNotifier.ps1 index 2b670cd..db0f3c7 100644 --- a/Start-DoneTickNotifier.ps1 +++ b/Start-DoneTickNotifier.ps1 @@ -1,40 +1,8 @@ [string] $dtHost = $ENV:DONETICKHOST [string] $dtPort = $ENV:DONETICKPORT [string] $dtAPIKey = $ENV:DONETICKAPIKEY -[string] $appriseWebhookURL = $ENV:APPRISEWEBHOOKURL -[string] $appriseWebhookTag = $ENV:APPRISEWEBHOOKTAG - -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])" - } -} +Import-Module ./AppriseNotification.psm1 function Get-Chores { @@ -70,7 +38,6 @@ foreach($chore in $chores) { write-host "$($chore.name) $dueDate is due today!" $todaysTasks += $chore - # Send-Notification -title "TASK DUE TODAY" -content "$($chore.Name) is due today!" } else { @@ -88,7 +55,7 @@ if ($overdueTasks.Count -ne 0) { $content += "$($overdueTask.Name) was due $((Get-Date $overdueTask.nextDueDate).ToLocalTime())`n" } - Send-Notification -title "OVERDUE TASKS" -content $content + Send-AppriseNotification -title "OVERDUE TASKS" -content $content } if ($todaysTasks.Count -ne 0) @@ -99,5 +66,5 @@ if ($todaysTasks.Count -ne 0) { $content += "$($task.Name) is due $((Get-Date $task.nextDueDate).ToLocalTime())`n" } - Send-Notification -title "TODAY'S TASKS" -content $content + Send-AppriseNotification -title "TODAY'S TASKS" -content $content } \ No newline at end of file