Skip to content
Abdi Urgessa edited this page Oct 30, 2024 · 6 revisions

Managing Stages in Telesun

Stages in Telesun make it easy to guide users through a series of steps, like gathering registration information. Each user has their own independent stages, so multiple users can interact with the bot without interfering with each other.

Why Use Stages?

  • Structured Interactions: Useful for step-by-step processes like forms or registrations.
  • User-Based Stages: Each user’s stage is tracked separately.
  • Temporary Data: Stages are stored in temporary sessions, automatically clearing after 10 minutes of inactivity to keep things simple and secure.

Example: Building a User Registration Process

Let’s say you want to create a registration flow to collect a username and password from the user. Here’s how to do it with stages:

Step 1: Ask for Username

First, prompt the user for their username and set the stage to username to indicate that’s what we’re waiting for.

const telesun = new Telesun({
  telegram: "7172550832:AAEmrWIvLICZn-pAiIdNXbIr1s1P-GsLAVw",
  tmemory: CacheService.getScriptCache(),
});

telesun.text((ctx) => {
  ctx.reply("Please enter your username:");
  ctx.setStage("username"); // Set stage to "username"
});

Step 2: Ask for Password

When the user provides a username, move to the next step by asking for their password. Set the stage to password to update the bot’s expectation.

telesun.stage("username", (ctx) => {
  ctx.reply("Great! Now, please enter your password:");
  ctx.setStage("password"); // Set stage to "password"
});

Step 3: Complete Registration

After receiving the password, complete the registration and thank the user.

telesun.stage("password", (ctx) => {
  ctx.reply("Thank you! Your registration is now complete.");
  ctx.clearStage(); // Optional: Clear the stage after finishing
});

Key Methods for Stages

Here’s a quick overview of the main stage-related methods:

  • setStage(stageIdentifier): Sets a stage for the user, marking what information or action you’re expecting.
  • getStage(): Retrieves the current stage.
  • clearStage(): Clears the current stage, helpful once the process is complete.

Summary

With stages, you can easily create organized and interactive bot flows like registration forms, quizzes, or any step-by-step interaction. By tracking the user’s current stage, Telesun keeps these flows smooth and user-friendly.

Clone this wiki locally