<# .SYNOPSIS Start ROA2WEB Telegram Bot Service .DESCRIPTION Starts the ROA2WEB Telegram Bot Windows service and validates it's running properly. .EXAMPLE .\Start-TelegramBot.ps1 #> [CmdletBinding()] param( [string]$ServiceName = "ROA2WEB-TelegramBot", [int]$Timeout = 30, [int]$HealthPort = 8002 ) $ErrorActionPreference = "Stop" Write-Host "`n[*] Starting ROA2WEB Telegram Bot Service..." -ForegroundColor Cyan try { $service = Get-Service -Name $ServiceName -ErrorAction Stop if ($service.Status -eq "Running") { Write-Host " [OK] Service is already running" -ForegroundColor Green exit 0 } # Start service Start-Service -Name $ServiceName Write-Host " [*] Service start command issued" -ForegroundColor Yellow # Wait for service to start $elapsed = 0 while ($service.Status -ne "Running" -and $elapsed -lt $Timeout) { Start-Sleep -Seconds 1 $service.Refresh() $elapsed++ Write-Host " [*] Waiting for service to start... ($elapsed/$Timeout)" -ForegroundColor Yellow } if ($service.Status -eq "Running") { Write-Host " [OK] Service started successfully" -ForegroundColor Green # Wait a bit and test health endpoint Start-Sleep -Seconds 5 try { $response = Invoke-WebRequest -Uri "http://localhost:$HealthPort/internal/health" -UseBasicParsing -TimeoutSec 10 if ($response.StatusCode -eq 200) { $content = $response.Content | ConvertFrom-Json Write-Host " [OK] Health check passed: $($content.status)" -ForegroundColor Green Write-Host " [OK] Database: $($content.database.status)" -ForegroundColor Green } } catch { Write-Host " [WARN] Health check failed (service may still be starting): $_" -ForegroundColor Yellow Write-Host " Check logs: C:\inetpub\wwwroot\roa2web\telegram-bot\logs\stderr.log" -ForegroundColor Yellow } exit 0 } else { Write-Host " [ERROR] Service failed to start (Status: $($service.Status))" -ForegroundColor Red Write-Host " Check logs: C:\inetpub\wwwroot\roa2web\telegram-bot\logs\stderr.log" -ForegroundColor Yellow exit 1 } } catch { Write-Host " [ERROR] Failed to start service: $_" -ForegroundColor Red exit 1 }