import { useState, useEffect } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { Send, TestTube } from "lucide-react"; import { apiClient } from "../lib/api"; import { Button } from "./ui/button"; import { Input } from "./ui/input"; import { Label } from "./ui/label"; import { Switch } from "./ui/switch"; import { EditButton } from "./ui/edit-button"; import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger, } from "./ui/drawer"; import type { NotificationSettings, TelegramConfig } from "../types/api"; interface TelegramConfigDrawerProps { settings: NotificationSettings | undefined; trigger?: React.ReactNode; } export default function TelegramConfigDrawer({ settings, trigger, }: TelegramConfigDrawerProps) { const [open, setOpen] = useState(false); const [config, setConfig] = useState({ token: "", chat_id: 0, enabled: true, }); const queryClient = useQueryClient(); useEffect(() => { if (settings?.telegram) { setConfig({ ...settings.telegram }); } }, [settings]); const updateMutation = useMutation({ mutationFn: (telegramConfig: TelegramConfig) => apiClient.updateNotificationSettings({ ...settings, telegram: telegramConfig, filters: settings?.filters || { case_insensitive: [], case_sensitive: [], }, }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["notificationSettings"] }); queryClient.invalidateQueries({ queryKey: ["notificationServices"] }); setOpen(false); }, onError: (error) => { console.error("Failed to update Telegram configuration:", error); }, }); const testMutation = useMutation({ mutationFn: () => apiClient.testNotification({ service: "telegram", message: "Test notification from Leggen - Telegram configuration is working!", }), onSuccess: () => { console.log("Test Telegram notification sent successfully"); }, onError: (error) => { console.error("Failed to send test Telegram notification:", error); }, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); updateMutation.mutate(config); }; const handleTest = () => { testMutation.mutate(); }; const isConfigValid = config.token.trim().length > 0 && config.chat_id !== 0; return ( {trigger || }
Telegram Configuration Configure Telegram bot notifications for transaction alerts
{/* Enable/Disable Toggle */}
setConfig({ ...config, enabled })} />
{/* Bot Token */}
setConfig({ ...config, token: e.target.value }) } disabled={!config.enabled} />

Create a bot using @BotFather on Telegram to get your token

{/* Chat ID */}
setConfig({ ...config, chat_id: parseInt(e.target.value) || 0, }) } disabled={!config.enabled} />

Send a message to your bot and visit https://api.telegram.org/bot<token>/getUpdates to find your chat ID

{/* Configuration Status */} {config.enabled && (
{isConfigValid ? "Configuration Valid" : "Missing Token or Chat ID"}
{!isConfigValid && (config.token.trim().length > 0 || config.chat_id !== 0) && (

Both bot token and chat ID are required

)}
)}
{config.enabled && isConfigValid && ( )}
); }