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" "encoding/json"
"fmt" "fmt"
"log" "log"
"net/http"
"os" "os"
"time" "time"
@ -99,10 +100,33 @@ func sendTelegramNotification(config json.RawMessage, test NotificationTest) err
return fmt.Errorf("invalid Telegram config: %w", err) return fmt.Errorf("invalid Telegram config: %w", err)
} }
fmt.Println("Token:", cfg.Token) apiUrl := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",
fmt.Println("ChatID:", cfg.ChatID) 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 return nil
} }

View File

@ -82,7 +82,7 @@ export const NotificationSettings = ({ onError, onSuccess }: { onError: (message
<DeleteNotification notificationId={deleteNotificationId} onNotificationDeleted={loadNotifications} onError={onError} onSuccess={onSuccess} /> <DeleteNotification notificationId={deleteNotificationId} onNotificationDeleted={loadNotifications} onError={onError} onSuccess={onSuccess} />
)} )}
{notificationTestId && ( {notificationTestId && (
<TestNotification notificationTestId={notificationTestId} /> <TestNotification notificationTestId={notificationTestId} onError={onError} />
)} )}
</div> </div>
</div> </div>

View File

@ -5,15 +5,20 @@ import { useEffect } from "react";
interface TestNotificationProps { interface TestNotificationProps {
notificationTestId: number; notificationTestId: number;
onError: (message: string) => void;
} }
export default function TestNotification({ notificationTestId }: TestNotificationProps) { export default function TestNotification({ notificationTestId, onError }: TestNotificationProps) {
const { getNotificationTest, notificationTest } = useNotifications(); const { getNotificationTest, notificationTest } = useNotifications();
const checkNotificationTest = async () => { const checkNotificationTest = async () => {
await getNotificationTest(notificationTestId); try {
await getNotificationTest(notificationTestId);
} catch (error) {
onError(error as string);
}
} }
useEffect(() => { useEffect(() => {
checkNotificationTest(); checkNotificationTest();