diff --git a/agent/main.go b/agent/main.go index 13f12be..778de73 100644 --- a/agent/main.go +++ b/agent/main.go @@ -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 } diff --git a/components/cards/settings/NotificationSettings.tsx b/components/cards/settings/NotificationSettings.tsx index c68e7e7..2ff6c1c 100644 --- a/components/cards/settings/NotificationSettings.tsx +++ b/components/cards/settings/NotificationSettings.tsx @@ -82,7 +82,7 @@ export const NotificationSettings = ({ onError, onSuccess }: { onError: (message )} {notificationTestId && ( - + )} diff --git a/components/dialogues/TestNotification.tsx b/components/dialogues/TestNotification.tsx index 7011223..4992971 100644 --- a/components/dialogues/TestNotification.tsx +++ b/components/dialogues/TestNotification.tsx @@ -5,15 +5,20 @@ import { useEffect } from "react"; interface TestNotificationProps { notificationTestId: number; + onError: (message: string) => void; } -export default function TestNotification({ notificationTestId }: TestNotificationProps) { +export default function TestNotification({ notificationTestId, onError }: TestNotificationProps) { const { getNotificationTest, notificationTest } = useNotifications(); const checkNotificationTest = async () => { - await getNotificationTest(notificationTestId); + try { + await getNotificationTest(notificationTestId); + } catch (error) { + onError(error as string); + } } useEffect(() => { checkNotificationTest();