/** * Telegram digest configuration card — Settings → Advanced. * * FR-2 §UI from the 2026-05-28 addictiveness audit. Wraps the four * /api/telegram/* endpoints so a user can self-serve the connection * without curl. Persona uplift comes when ScheduledJobs grows an * "output channel" dropdown that routes job results to /api/telegram/send; * this card is the prerequisite trust + connection surface. */ import { useState, useEffect } from 'react'; import { Send, Loader2, CheckCircle2, AlertCircle, Eye, EyeOff } from 'lucide-react'; import { Input } from '@/components/ui/input'; import { useToast } from '@/hooks/use-toast'; import { adapter } from '@/lib/adapter'; interface TelegramStatus { configured: boolean; hasToken: boolean; hasChatId: boolean; } async function fetchStatus(): Promise { try { const r = await fetch(`${adapter.getServerUrl()}/api/telegram/status`); if (!r.ok) return null; return await r.json(); } catch { return null; } } const TelegramDigestCard = () => { const { toast } = useToast(); const [status, setStatus] = useState(null); const [token, setToken] = useState(''); const [chatId, setChatId] = useState(''); const [showToken, setShowToken] = useState(false); const [saving, setSaving] = useState(false); const [testing, setTesting] = useState(false); useEffect(() => { fetchStatus().then(setStatus); }, []); const save = async () => { if (!token && !chatId) { toast({ title: 'Nothing to save', description: 'Enter a bot token or chat ID first.' }); return; } setSaving(true); try { const body: Record = {}; if (token) body.botToken = token; if (chatId) body.chatId = chatId; const r = await fetch(`${adapter.getServerUrl()}/api/telegram/config`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); const data = await r.json(); if (!r.ok) { toast({ title: 'Save failed', description: data?.error || `HTTP ${r.status}`, variant: 'destructive' }); } else { toast({ title: 'Saved', description: 'Telegram credentials stored in vault.' }); setToken(''); setChatId(''); setStatus(await fetchStatus()); } } catch (err) { toast({ title: 'Save failed', description: String(err), variant: 'destructive' }); } finally { setSaving(false); } }; const sendTest = async () => { setTesting(true); try { const r = await fetch(`${adapter.getServerUrl()}/api/telegram/test`, { method: 'POST' }); const data = await r.json(); if (!r.ok || data?.ok === false) { toast({ title: 'Test failed', description: data?.error || `HTTP ${r.status}`, variant: 'destructive', }); } else { toast({ title: 'Sent', description: 'Check your Telegram for the test message.' }); } } catch (err) { toast({ title: 'Test failed', description: String(err), variant: 'destructive' }); } finally { setTesting(false); } }; const configured = status?.configured ?? false; return (

Telegram digest

{configured ? : } {configured ? 'Connected' : 'Not configured'}

Push scheduled-job results and daily digests to your Telegram. Create a bot via{' '} @BotFather {' '} to get a token, then DM your bot once and read the chat ID from{' '} api.telegram.org/bot<TOKEN>/getUpdates.

setToken(e.target.value)} placeholder={status?.hasToken ? '••••••••••• (leave blank to keep)' : 'e.g. 123456789:AAH…'} className="text-[11px] pr-7" data-testid="telegram-bot-token-input" />
setChatId(e.target.value)} placeholder={status?.hasChatId ? '••••••' : 'e.g. 123456789 or -1001234567890'} className="text-[11px]" data-testid="telegram-chat-id-input" />
); }; export default TelegramDigestCard;