Merge pull request 'implementing better error handling' (#2) from v0.2 into main

Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
2026-05-04 21:40:28 -05:00
+54 -24
View File
@@ -9,6 +9,55 @@
[string] $appriseWebhookURL = $ENV:APPRISEWEBHOOKURL
[string] $appriseWebhookTag = $ENV:APPRISEWEBHOOKTAG
function Send-Notification
{
param(
# Content of the notification
[Parameter(Mandatory=$true)]
[string]
$content
)
if ($notificationWebhookURL -or $discordWebhookURL)
{
if ($discordWebhookURL) { $notificationWebhookURL = $discordWebhookURL }
$headers = @{
"Content-Type"="application/json"
}
$body = @{
content=$content
# content="QBitTorrent Listening Port updated to $gluetunForwardedPort"
}
$json = $body | ConvertTo-Json
try {
Invoke-WebRequest -URI $notificationWebhookURL -Method post -Body $json -Headers $headers
}
catch {
Write-Host "Notification Error Encountered: $($global:Error[0])"
}
}
elseif ($appriseWebhookURL)
{
$headers = @{
"Content-Type"="application/json"
}
$body = @{
title="QBitTorrent Port Manager"
body=$content
# body="QBitTorrent Listening Port updated to $gluetunForwardedPort"
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])"
}
}
}
while ($true)
{
$gluetunForwardedPort = Get-Content $gluetunForwardedPortFile
@@ -21,9 +70,9 @@ while ($true)
username = $qbtUser
password = $qbtPassword
}
try {
$qbtSession = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
$qbtResponse = Invoke-WebRequest -Uri "http://$($qbtHost):$($qbtPort)/api/v2/auth/login" -WebSession $qbtSession -Method POST -Headers $qbtHeaders -Body $qbtBody
$qbtQueryResponse = Invoke-WebRequest -Uri "http://$($qbtHost):$($qbtPort)/api/v2/app/preferences" -WebSession $qbtSession -Method GET
$qbtListenPort = (ConvertFrom-Json $qbtQueryResponse.Content).listen_port
Write-Host "Current QBT Listening port: $qbtListenPort"
@@ -36,31 +85,12 @@ while ($true)
{
$qbtPortChangeBody = 'json={"listen_port":' + $gluetunForwardedPort + '}'
$qbtPortChangeResponse = Invoke-WebRequest -Uri "http://$($qbtHost):$($qbtPort)/api/v2/app/setPreferences" -WebSession $qbtSession -Method POST -Headers $qbtHeaders -Body $qbtPortChangeBody
if ($notificationWebhookURL -or $discordWebhookURL)
{
if ($discordWebhookURL) { $notificationWebhookURL = $discordWebhookURL }
$headers = @{
"Content-Type"="application/json"
Send-Notification -content "QBitTorrent Listening Port updated to $gluetunForwardedPort"
}
$body = @{
content="QBitTorrent Listening Port updated to $gluetunForwardedPort"
}
$json = $body | ConvertTo-Json
Invoke-WebRequest -URI $notificationWebhookURL -Method post -Body $json -Headers $headers
}
elseif ($appriseWebhookURL)
{
$headers = @{
"Content-Type"="application/json"
}
$body = @{
title="QBitTorrent Port Manager"
body="QBitTorrent Listening Port updated to $gluetunForwardedPort"
tags="$appriseWebhookTag"
}
$json = $body | ConvertTo-Json
Invoke-WebRequest -URI $appriseWebhookURL -Method post -Body $json -Headers $headers
}
catch {
Write-Host "Error encountered: $($global:Error[0])"
Send-Notification -content "Error encountered: $($global:Error[0])"
}
Write-Host "Sleeping for $schedule minutes..."
Start-Sleep ($schedule * 60)