You can define bot logic using annotated functions. Common annotations include:
@CommandHandler(commands: Array<String>): Responds to specific slash commands.@InputHandler(inputs: Array<String>): Responds to specific input states (useful for Finite State Machines).@CommonHandler.Regex(pattern: String): Responds to messages matching a regular expression.
You can use @Guard(guardClass: KClass<T>) to restrict access to a handler based on specific conditions.
To manage user states (Finite State Machine), use bot.inputListener[user] = "state_name" to transition a user to a specific input handler.
suspend fun main() {
val bot = TelegramBot("BOT_TOKEN")
bot.handleUpdates()
}
@CommandHandler(["/start"])
suspend fun start(user: User, bot: TelegramBot) {
sendMessage { "Hello, what's your name?" }.send(user, bot)
bot.inputListener[user] = "conversation"
}
@InputHandler(["conversation"])
@Guard(UserPresentGuard::class)
suspend fun startConversation(update: ProcessedUpdate, user: User, bot: TelegramBot) {
sendMessage { "Nice to meet you, ${update.text}" }.send(user, bot)
sendMessage { "What is your favorite food?" }.send(user, bot)
bot.inputListener.set(user) { "conversation-2step" }
}
@CommonHandler.Regex("blue colo?r")
suspend fun color(user: User, bot: TelegramBot) {
message { "Oh you also like blue color?" }.send(user, bot)
}