import { Button, Group, Text } from '@mantine/core'; import { IconBrandTelegram, IconCircleCheck, IconSearch, IconSend } from '@/components/icons'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useState } from 'react'; import { getJson, sendJson } from '../api/client'; import { ErrorAlert, fmtTs } from '../components/common'; /** The Telegram part of GET /api/notifications/settings. */ export interface TelegramState { token_set: boolean; chat: { chat_id: string; title: string } | null; last_delivery: { at: string; ok: boolean; status: number | null; error: string | null } | null; } interface FoundChat { chat_id: string; title: string; type: string } /** GET /api/channels/telegram/status (admin): the two-way bot. */ export interface BotStatus { token_set: boolean; bot_username: string | null; polling: boolean; last_update_at: string | null; linked_users: number } /** One line on the two-way bot: its name, whether it is listening for messages, and how many people linked a chat. */ export function BotStatusLine() { const q = useQuery({ queryKey: ['tg-bot-status'], queryFn: () => getJson('/api/channels/telegram/status'), refetchInterval: 30_000 }); const b = q.data; if (!b || !b.token_set) return null; return (

{b.bot_username ? @{b.bot_username} : 'The bot'}{' '} {b.polling ? 'Listening for messages' : 'Not listening'} {b.last_update_at ? ` Last message ${fmtTs(b.last_update_at)}.` : ''} {` ${b.linked_users} ${b.linked_users === 1 ? 'person' : 'people'} linked.`}

); } /** * Telegram, as a short guided setup with live state: the bot token (a Space secret, never typed into the browser), then * the chat (found from the /start message the person sent the bot), then a test message. */ export function TelegramCard({ tg }: { tg: TelegramState | undefined }) { const qc = useQueryClient(); const refresh = () => { void qc.invalidateQueries({ queryKey: ['notify-settings'] }); void qc.invalidateQueries({ queryKey: ['notifications'] }); }; const [found, setFound] = useState(null); const discover = useMutation({ mutationFn: () => sendJson<{ chats: FoundChat[] }>('POST', '/api/notifications/telegram/discover'), onSuccess: (r) => setFound(r?.chats ?? []), }); const choose = useMutation({ mutationFn: (c: FoundChat) => sendJson('POST', '/api/notifications/telegram/chat', { chat_id: c.chat_id, title: c.title }), onSuccess: () => { setFound(null); refresh(); }, }); const disconnect = useMutation({ mutationFn: () => sendJson('DELETE', '/api/notifications/telegram/chat'), onSuccess: refresh }); const test = useMutation({ mutationFn: () => sendJson<{ queued: boolean; id: number }>('POST', '/api/notifications/test'), onSuccess: refresh }); const last = tg?.last_delivery; const problem = discover.error || choose.error || disconnect.error || test.error; return (

Telegram

  1. {tg?.token_set ? Bot token is set : Create a bot with @BotFather (/newbot), then put its token in the Space secret FPT_TELEGRAM_BOT_TOKEN and restart the Space.}
  2. {tg?.token_set && (
  3. {tg.chat ? (
    Messages go to {tg.chat.title} {test.isSuccess && Test message queued. It should arrive within a minute.}
    ) : (
    Each person links their own chat from their Account page. For a shared team chat that gets every update, send /start to the bot from that chat, then press Find my chat. {found && found.length === 0 && No messages yet: send /start to your bot, then try again.} {found && found.length > 0 && (
      {found.map((c) => (
    • {c.title} {c.type}
    • ))}
    )}
    )}
  4. )}
{last && (

Last message: {last.ok ? 'Delivered' : 'Failed'} {fmtTs(last.at)} {last.error ? `: ${last.error}` : ''}

)} {problem ? : null}
); }