-
-
Notifications
You must be signed in to change notification settings - Fork 16
Guards
Guards are an essential feature for developers creating bots. These guards function as pre-execution checks that determine whether a particular command should be invoked. By implementing these checks, developers can enhance the functionality, security, and user experience of their bots.
The primary purpose of activity guards is to ensure that only authorized users or specific conditions trigger a activity.
This can prevent misuse, maintain the bot's integrity, and streamline interactions.
- Authentication and Authorization: Ensuring only certain users can access specific commands.
- Pre-condition Checks: Verifying that certain conditions are met before executing a activity (e.g., ensuring a user is in a particular state or context).
- Contextual Guards: Making decisions based on the current chat or user state.
Implementing Telegram Command Guards typically involves writing functions or methods that encapsulate the logic for each guard. Below are common strategies:
-
User Role Check:
- Ensuring the user has the required role (e.g., admin, moderator) before executing the command.
override suspend fun condition(user: User?, update: ProcessedUpdate, bot: TelegramBot): Boolean { // Check if the user is an admin in the given chat }
- Ensuring the user has the required role (e.g., admin, moderator) before executing the command.
-
State Verification:
- Checking the user's state before allowing command execution.
override suspend fun condition(user: User?, update: ProcessedUpdate, bot: TelegramBot): Boolean { return bot.userData[user.id, "data"] == requiredState }
- Checking the user's state before allowing command execution.
-
Custom Guards:
- Creating custom logic based on specific requirements.
override suspend fun condition(user: User?, update: ProcessedUpdate, bot: TelegramBot): Boolean { // Custom logic to determine if the command should be executed }
- Creating custom logic based on specific requirements.
To integrate these guards with your bot commands, you can create a guard that checks these conditions before the command handler is invoked.
// define somewhere your guard class that implements Guard interface
object YourGuard : Guard {
override suspend fun condition(user: User?, update: ProcessedUpdate, bot: TelegramBot): Boolean {
// write your condition here
}
}
// ...
@CommandHandler(["yourCommand"], guard = YourGuard::class) // InputHandler also is supported
fun command(bot: TelegramBot) {
// command body
}
- Modularity: Keep guard logic modular and separate from activities.
- Reusability: Write reusable guard functions that can be easily applied across different commands/inputs.
- Efficiency: Optimize guard checks to minimize performance overhead.
- User Feedback: Provide clear feedback to users when a command is blocked by a guard.
Activity Guards are a powerful tool for managing bot command/input execution.
By implementing robust guard mechanisms, developers can ensure their bots operate securely and efficiently, providing a better user experience.
Telegram bot Wiki © KtGram