Command in TPY

3. Commands in TPY

Commands are the backbone of every bot built on Telebot Creator. They define how a bot responds to specific inputs from users, such as messages or commands like /start or /help. This section explains what commands are, how to write them using TPY (Telebot Python), and advanced techniques for chaining commands, handling user interactions, and adding interactivity.


3.1 What Are Commands?

  • A command is a predefined trigger in your bot that responds to specific user messages. For example:

    • /start: Greets the user and provides an introduction.

    • /help: Displays a list of available commands and their usage.

  • Commands can perform actions like:

    • Sending messages or media.

    • Handling user inputs dynamically.

    • Running scheduled tasks or interacting with APIs.


3.2 Writing Commands in TPY

TPY (Telebot Python) is a customized, lightweight version of Python designed for Telebot Creator. It simplifies the process of writing commands while providing powerful tools for bot development.

Example of a Basic Command

Here’s how you create a /start command that sends a welcome message:

bot.sendMessage("Welcome to my bot! Use /help to see available commands.")

3.3 Using Variables and Parameters

Variables in TPY allow you to customize responses and interact dynamically with users.

Accessing User Information

You can use variables like message.from_user.first_name to personalize your messages. For example:

Handling Command Parameters

Commands can accept parameters passed by users. For example, in /start 12345, the parameter 12345 can be accessed as params:


3.4 Handling User Interactions

TPY provides several methods to manage and guide user interactions effectively.

1. handleNextCommand

This method waits for the user’s next input and routes it to a specific command.

Example:

In the process_email command:

2. runCommand

This method allows you to trigger another command immediately.

Example:

3. runCommandAfter

This schedules a command to execute after a specified delay (in seconds).

Example:

In the delayed_message command:


3.5 Advanced Command Techniques

Wildcard Master Command (*)

The wildcard (*) command captures any input that doesn’t match a predefined command. This is useful for creating fallback responses.

Example:

At Handler Command (@)

The at handler (@) command executes before any other command. Use it for preprocessing messages or logging user activity.

Example:


3.6 Examples of Common Commands

1. Greet Users

2. Display Help Menu

3. Check User Points

4. Collect User Input

In the save_color command:


3.7 Chaining Commands

Commands can be chained together to create complex workflows. For example, a multi-step form:

  1. Ask for the user’s name:

  2. Process the name and ask for the email:

  3. Process the email and confirm:


Summary

Commands in TPY are the heart of every bot on Telebot Creator. By mastering command creation, parameter handling, and advanced techniques like chaining and scheduling, you can build bots that are interactive, intelligent, and highly functional.

Last updated