Skip to content
Abdi Urgessa edited this page Apr 21, 2024 · 5 revisions

Stages in Telesun offer a structured way to handle consecutive tasks or steps such as user registrations. This feature is particularly useful for creating a step-by-step interaction flow with your bot's users. Stages are maintained on a per-user basis, ensuring that each user has a unique, independent progression through the stages. This functionality is crucial for managing tasks like multiple registrations simultaneously without confusion.

Temporary Sessions

Stages leverage temporary sessions that are automatically cleared after 10 minutes of inactivity. This temporary nature helps manage data efficiently and securely, ensuring that user information is not stored longer than necessary.

Creating Stages

You can define stages using various identifiers such as numbers or strings, offering flexibility in how you structure the user interaction flow.

Example: User Registration Process

Imagine you want to register users by collecting their username and password. Here's how you can implement it using stages:

1. Ask for Username

Start by prompting the user to enter their username. At this point, set the stage to username to indicate that you're waiting for this piece of information.

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

telesun.text((ctx) => {
    ctx.reply('Please type your username:');
    ctx.setStage('username');
});

2. Request Password

Once the user provides a username, move to the next stage by asking for their password. Change the stage to password to reflect the new expectation.

telesun.stage('username', (ctx) => {
    ctx.reply('Please type your password:');
    ctx.setStage('password');
});

3. Complete Registration

After receiving the password, finalize the registration process by acknowledging the completion.

telesun.stage('password', (ctx) => {
    ctx.reply('Registration successfully completed.');
});

Using Stages

Stages can be implemented as follows:

/**
 * Define a stage.
 * @param {string | number} name - The name of the stage.
 * @param {Function} function - The function to execute at this stage.
 */
telesun.Stage(name, (ctx) => {});

/**
 * Save a stage with a name or identifier.
 * @param {string | number} stageIdentifier - The stage name or identifier to set.
 */
ctx.setStage(stageIdentifier);

/**
 * Retrieve the currently saved stage.
 */
ctx.getStage();

This framework for stages in Telesun simplifies the creation of multi-step interactions, making it easier to manage complex user flows such as registrations or data collection sequences. By structuring interactions into stages, developers can create more engaging and interactive bots.

Clone this wiki locally