Working telegram functionality

This commit is contained in:
headlesdev
2025-05-25 22:37:20 +02:00
parent a218a7f8f3
commit d982a69f4d
3 changed files with 35 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
@@ -99,10 +100,33 @@ func sendTelegramNotification(config json.RawMessage, test NotificationTest) err
return fmt.Errorf("invalid Telegram config: %w", err)
}
fmt.Println("Token:", cfg.Token)
fmt.Println("ChatID:", cfg.ChatID)
apiUrl := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",
cfg.Token, cfg.ChatID, "Test Notification")
log.Printf("Sending Telegram test to chat %s", cfg.ChatID)
resp, err := http.Get(apiUrl)
if err != nil {
return fmt.Errorf("failed to send Telegram message: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("telegram API returned error status: %d", resp.StatusCode)
}
var result struct {
OK bool `json:"ok"`
Error string `json:"description,omitempty"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to decode telegram response: %w", err)
}
if !result.OK {
return fmt.Errorf("telegram API error: %s", result.Error)
}
log.Printf("Successfully sent Telegram notification to chat %s", cfg.ChatID)
return nil
}