mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-17 15:36:50 +00:00
Working telegram functionality
This commit is contained in:
parent
a218a7f8f3
commit
d982a69f4d
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -82,7 +82,7 @@ export const NotificationSettings = ({ onError, onSuccess }: { onError: (message
|
||||
<DeleteNotification notificationId={deleteNotificationId} onNotificationDeleted={loadNotifications} onError={onError} onSuccess={onSuccess} />
|
||||
)}
|
||||
{notificationTestId && (
|
||||
<TestNotification notificationTestId={notificationTestId} />
|
||||
<TestNotification notificationTestId={notificationTestId} onError={onError} />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user