'use client'; import { useMemo, useState } from 'react'; import { Check, Copy, Loader2, Bot } from 'lucide-react'; import { useToast } from '@/components/id/toast-provider'; import { usePublicSettings } from '@/components/id/public-settings-provider'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { createManagedBot, getApiErrorMessage } from '@/lib/api'; import { useMiniAppAuth } from '@/hooks/use-mini-app-auth'; import { cn } from '@/lib/utils'; type Step = 'name' | 'username' | 'done'; export function BotCreateMiniAppContent() { const { projectName } = usePublicSettings(); const { effectiveToken, canUse, authReady, waitingForBridge, isLoading } = useMiniAppAuth(); const { showToast } = useToast(); const [step, setStep] = useState('name'); const [name, setName] = useState(''); const [username, setUsername] = useState(''); const [creating, setCreating] = useState(false); const [createdBot, setCreatedBot] = useState<{ username: string; token: string; botId: string } | null>(null); const canUseSession = canUse; const usernamePreview = useMemo(() => `${username.replace(/_bot$/i, '').trim()}_bot`, [username]); async function handleCreate() { if (!canUseSession || !effectiveToken) return; const trimmedName = name.trim(); const trimmedUsername = username.replace(/_bot$/i, '').trim(); if (!trimmedName) { showToast('Укажите название бота'); setStep('name'); return; } if (trimmedUsername.length < 5) { showToast('Username должен содержать минимум 5 символов'); return; } setCreating(true); try { const response = await createManagedBot({ name: trimmedName, username: trimmedUsername }, effectiveToken); setCreatedBot({ username: response.bot?.username ?? usernamePreview, token: response.token ?? '', botId: response.bot?.id ?? '' }); setStep('done'); showToast('Бот успешно создан'); } catch (error) { showToast(getApiErrorMessage(error, 'Не удалось создать бота') ?? 'Ошибка'); } finally { setCreating(false); } } async function copyToken() { if (!createdBot?.token) return; await navigator.clipboard.writeText(createdBot.token); showToast('Токен скопирован'); } if ((isLoading || waitingForBridge || !authReady) && !effectiveToken) { return (
Загрузка...
); } if (!canUseSession) { return (
Войдите в {projectName}, чтобы создать бота
); } return (

Создание бота

Как в BotFather Telegram

{(['name', 'username', 'done'] as Step[]).map((item, index) => (
{step === 'done' && item !== 'done' ? : index + 1} {index < 2 ? : null}
))}
{step === 'name' ? (

Alright, a new bot. How are we going to call it? Please choose a name for your bot.

Хорошо, новый бот. Как мы его назовём? Выберите название.

setName(event.target.value)} placeholder="Например: Сервис уведомлений" className="rounded-xl border-[#2a3544] bg-[#17212b] text-white placeholder:text-[#667085]" autoFocus />
) : null} {step === 'username' ? (

Good. Now let's choose a username for your bot. It must end in `bot`.

Username должен заканчиваться на `_bot` (суффикс добавится автоматически).

@ setUsername(event.target.value.replace(/[^a-zA-Z0-9_]/g, ''))} placeholder="notify_service" className="rounded-xl border-[#2a3544] bg-[#17212b] text-white placeholder:text-[#667085]" autoFocus />

Будет: @{usernamePreview || 'your_bot'}

) : null} {step === 'done' && createdBot ? (

Done! Congratulations on your new bot.

@{createdBot.username}

Сохраните токен — он больше не будет показан:

) : null}
); }