metallkart-erp/scripts/notify-telegram.ps1
2026-04-08 15:48:24 +03:00

49 lines
1.7 KiB
PowerShell

#!/usr/bin/env pwsh
# notify-telegram.ps1 — Envoie une notification Telegram
# Usage: .\notify-telegram.ps1 "message texte"
# Usage: .\notify-telegram.ps1 -Status OK -TaskId "33A" -Elapsed 42.5
# Usage: .\notify-telegram.ps1 -Status ERREUR -TaskId "33A" -Elapsed 10 -Details "build failed"
param(
[Parameter(Position=0)]
[string]$Message,
[string]$Status,
[string]$TaskId,
[double]$Elapsed,
[string]$Details
)
$TG_TOKEN = "8673293524:AAFcj2F7Nl0kHgDva9yH_I0Prg6poiHVa8k"
$TG_CHAT = "889172188"
# Build message
if (-not $Message) {
if ($Status -and $TaskId) {
$icon = switch ($Status) { "OK" { "[OK]" } "ERREUR" { "[ERR]" } default { "[INFO]" } }
$Message = "$icon [$TaskId] $Status"
if ($Elapsed) { $Message += " ($($Elapsed)s)" }
if ($Details) { $Message += " - $Details" }
} else {
Write-Host "Usage: notify-telegram.ps1 'message' ou -Status OK -TaskId ID" -ForegroundColor Red
exit 1
}
}
# Truncate if too long (Telegram limit 4096)
if ($Message.Length -gt 4000) { $Message = $Message.Substring(0, 4000) + "...TRONQUE" }
# Sanitize for JSON
$Message = $Message -replace '\\','\\\\'
try {
$body = @{ chat_id = $TG_CHAT; text = $Message; parse_mode = "HTML" } | ConvertTo-Json -Compress
$uri = "https://api.telegram.org/bot$TG_TOKEN/sendMessage"
$response = Invoke-RestMethod -Uri $uri -Method Post -Body $body -ContentType "application/json; charset=utf-8" -ErrorAction Stop
if ($response.ok) {
Write-Host "TG OK" -ForegroundColor Green
} else {
Write-Host "TG FAIL: $($response | ConvertTo-Json -Compress)" -ForegroundColor Red
}
} catch {
Write-Host "TG ERROR: $($_.Exception.Message)" -ForegroundColor Red
}