Teact
Guides

Conversations

useConversation, the declarative Conversation component, and grammY conversationsPlugin for imperative flows.

Conversations collect answers one step at a time. Use the useConversation hook or the <Conversation> component for JSX-driven flows, or defineConversation + conversationsPlugin() for imperative async/await flows.

Hook API

Each step is either a prompt (free-text, optional validate) or an ask (inline-keyboard choice). Validators return true to accept or an error string.

import { useConversation } from "@teactjs/core";
import { Message } from "@teactjs/ui";

function Onboarding() {
  const conv = useConversation({
    name: {
      prompt: "What's your name?",
      validate: (v) => v.trim().length > 0 || "Name required",
    },
    color: { ask: "Pick a color:", options: [["Red", "Blue"]] },
  });

  return conv.render(() => (
    <Message text={`Hi ${conv.data.name}, you picked ${conv.data.color}`} />
  ));
}

conv.render(onComplete) renders the current step and calls onComplete when done. Read answers from conv.data; inspect conv.current, conv.complete, and conv.lastError; navigate with conv.back(), conv.goTo(key), and conv.reset().

Declarative API

import { Conversation } from "@teactjs/core";
import { Message } from "@teactjs/ui";

<Conversation>
  <Conversation.Prompt name="name" text="What's your name?" />
  <Conversation.Ask name="color" text="Pick:" options={[["Red", "Blue"]]} />
  <Conversation.Complete>
    {(conv) => <Message text={`Hi ${conv.data.name}`} />}
  </Conversation.Complete>
</Conversation>;

Imperative flows (grammY)

For async/await style flows outside JSX, register conversationsPlugin() from @teactjs/telegram and define named conversations:

import { conversationsPlugin, defineConversation } from "@teactjs/telegram";
import { createBot } from "@teactjs/core";

defineConversation("onboarding", async (convo) => {
  const name = await convo.prompt("What's your name?");
  await convo.send(`Welcome, ${name}!`);
});

const bot = createBot({
  // ...
  plugins: [conversationsPlugin()],
});

Enter a named flow from a button with the conversation prop:

<Button text="Start" conversation="onboarding" />

The conversation prop and imperative flows require conversationsPlugin() — it drives the grammY conversation engine. Without it the button does nothing.

The convo object exposes prompt, send, wait, ask, stream, reply helpers, requestContact, requestLocation, and api / raw access.

Do / Don't

  • Do return conv.render(() => <Done />) — it handles the current step and completion for you.
  • Do register conversationsPlugin() before using defineConversation or the conversation button prop.
  • Don't read conv.data.field before the step is answered — check conv.current / conv.complete first.
  • Don't expect imperative conversations to run on serverless/edge — the isolate freezes after the first commit (see Streaming).

On this page