PEP-100: opening the bot keeps Peppa open — new tab, the page refreshes until Start is pressed
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7f9b697c62
commit
bb7b1b4158
@ -418,6 +418,15 @@ export async function removeProjectMember(f: FormData) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** PEP-100: the pairing link for the client to open in a new tab — Peppa stays where it is. */
|
||||||
|
export async function pairingLinkAction(): Promise<{ url?: string; error?: string }> {
|
||||||
|
const user = asWebActor(await requireUser());
|
||||||
|
const token = loadEnv().TELEGRAM_BOT_TOKEN;
|
||||||
|
const username = user.telegramBotUsername ?? (await getSetting<{ username: string }>(db, "telegram.bot"))?.username;
|
||||||
|
if (!token || !username) return { error: "Telegram-бот Пеппы не запущен" };
|
||||||
|
return { url: `https://t.me/${username}?start=${await createPairingCode(db, token, user.id)}` };
|
||||||
|
}
|
||||||
|
|
||||||
export async function pairTelegram(f: FormData) {
|
export async function pairTelegram(f: FormData) {
|
||||||
const user = asWebActor(await requireUser());
|
const user = asWebActor(await requireUser());
|
||||||
await attempt(f, async () => {
|
await attempt(f, async () => {
|
||||||
|
|||||||
52
apps/web/components/OpenBotButton.tsx
Normal file
52
apps/web/components/OpenBotButton.tsx
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { pairingLinkAction } from "@/app/(app)/actions";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PEP-100: opens the user's bot (with the pairing code) in a new tab / the Telegram app and keeps
|
||||||
|
* Peppa open — a plain redirect used to take the page away mid-steps. While the user presses
|
||||||
|
* Start, the page refreshes itself so the step turns done.
|
||||||
|
*/
|
||||||
|
export function OpenBotButton({ label, variant = "default", size = "sm" }: { label: string; variant?: "default" | "ghost" | "outline"; size?: "sm" | "xs" }) {
|
||||||
|
const router = useRouter();
|
||||||
|
const [waiting, setWaiting] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!waiting) return;
|
||||||
|
const t = setInterval(() => router.refresh(), 3000);
|
||||||
|
const stop = setTimeout(() => setWaiting(false), 3 * 60_000);
|
||||||
|
return () => {
|
||||||
|
clearInterval(t);
|
||||||
|
clearTimeout(stop);
|
||||||
|
};
|
||||||
|
}, [waiting, router]);
|
||||||
|
|
||||||
|
async function open() {
|
||||||
|
setError(null);
|
||||||
|
// Opened synchronously in the click, so the browser doesn't block it as a popup.
|
||||||
|
const tab = window.open("about:blank", "_blank");
|
||||||
|
const r = await pairingLinkAction();
|
||||||
|
if (!r.url) {
|
||||||
|
tab?.close();
|
||||||
|
setError(r.error ?? "Не получилось открыть бота");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (tab) tab.location.href = r.url;
|
||||||
|
else window.location.href = r.url;
|
||||||
|
setWaiting(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<span className="inline-flex flex-col items-start gap-1">
|
||||||
|
<Button type="button" variant={variant} size={size} onClick={() => void open()}>
|
||||||
|
{label}
|
||||||
|
</Button>
|
||||||
|
{waiting && <span className="text-xs text-muted-foreground">Нажмите Start в Telegram и вернитесь сюда — шаг отметится сам.</span>}
|
||||||
|
{error && <span className="text-xs text-destructive">{error}</span>}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
import { connectOwnBotAction, disconnectOwnBotAction, pairTelegram } from "@/app/(app)/actions";
|
import { connectOwnBotAction, disconnectOwnBotAction } from "@/app/(app)/actions";
|
||||||
|
import { OpenBotButton } from "@/components/OpenBotButton";
|
||||||
import { Hidden } from "@/components/cards";
|
import { Hidden } from "@/components/cards";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
@ -93,10 +94,7 @@ export function OwnBotSteps({ botUsername, back, bare = false }: { botUsername:
|
|||||||
телефоне: туда и будут приходить сообщения.
|
телефоне: туда и будут приходить сообщения.
|
||||||
</span>
|
</span>
|
||||||
{hasBot && (
|
{hasBot && (
|
||||||
<form action={pairTelegram}>
|
<OpenBotButton label={`Открыть @${botUsername}`} />
|
||||||
<Hidden fields={fields} />
|
|
||||||
<Button variant="default" size="sm">Открыть @{botUsername}</Button>
|
|
||||||
</form>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
@ -107,11 +105,10 @@ export function OwnBotSteps({ botUsername, back, bare = false }: { botUsername:
|
|||||||
<Button size="xs" variant="ghost" className="text-muted-foreground">Отключить этого бота</Button>
|
<Button size="xs" variant="ghost" className="text-muted-foreground">Отключить этого бота</Button>
|
||||||
</form>
|
</form>
|
||||||
) : (
|
) : (
|
||||||
<form action={pairTelegram} className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
|
<div className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
|
||||||
<Hidden fields={fields} />
|
|
||||||
Не хотите создавать бота?
|
Не хотите создавать бота?
|
||||||
<Button size="xs" variant="ghost">Использовать общего бота Пеппы</Button>
|
<OpenBotButton label="Использовать общего бота Пеппы" variant="ghost" size="xs" />
|
||||||
</form>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -7,3 +7,4 @@ PM не сразу понял, что бота нужно создать у @Bot
|
|||||||
- Шаг 1 «Создайте бота у @BotFather» — переписка по сообщениям: Start → `/newbot` → имя → адрес на …bot → где в ответе токен.
|
- Шаг 1 «Создайте бота у @BotFather» — переписка по сообщениям: Start → `/newbot` → имя → адрес на …bot → где в ответе токен.
|
||||||
- Шаг 2 — как выглядит токен; шаг 3 — что это уже свой бот, не BotFather.
|
- Шаг 2 — как выглядит токен; шаг 3 — что это уже свой бот, не BotFather.
|
||||||
- Подсказка онбординга называет эти шаги.
|
- Подсказка онбординга называет эти шаги.
|
||||||
|
- «Открыть @бота» и «общий бот» открывают Telegram в новой вкладке (`pairingLinkAction` + `OpenBotButton`), страница Пеппы остаётся и сама обновляется, пока пользователь жмёт Start (раньше redirect уводил страницу).
|
||||||
|
|||||||
@ -1 +1,2 @@
|
|||||||
- [x] 1.1 шаги BotFather по сообщениям
|
- [x] 1.1 шаги BotFather по сообщениям
|
||||||
|
- [x] 1.2 бот открывается в новой вкладке, страница остаётся и обновляется
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user