-
-
Notifications
You must be signed in to change notification settings - Fork 16
Functional DSL
Although the basic mechanism of working with the bot involves working with annotations, but nevertheless it does not prevent the use of functional update processing.
Moreover, the flexibility of the bot interface also allows you to combine the two modes.
In most manual processing methods differ in the types of Update
supported, in simple terms you can put a listener on a certain type of data.
By way of example:
suspend fun main() {
val bot = TelegramBot("BOT_TOKEN")
bot.handleUpdates() {
onChosenInlineResult {
println("got a result ${update.resultId} from ${update.from}")
}
}
}
It is also possible to process both commands
and inputs
.
See example:
fun main() = runBlocking {
val bot = TelegramBot("BOT_TOKEN")
bot.handleUpdates() {
// regular command
onCommand("/start") {
message { "Hello" }.send(user, bot)
}
onCommand("""(red|green|blue)""".toRegex()) {
message { "you typed ${update.message.text} color" }.send(user, bot)
}
}
}
In the context of the onCommand
function, parameters in the format Map<String, String>
are passed,
parsed appropriately specified in configuration.
It is also possible to use inputs through the familiar bot.inputListener
mechanism.
fun main() = runBlocking {
val bot = TelegramBot("BOT_TOKEN")
bot.handleUpdates() {
// regular command
onCommand("/start") {
message { "Hello, what's your name?" }.send(user, bot)
bot.inputListener[user] = "testInput"
}
onInput("testInput") {
message { "Hey, nice to meet you, ${update.text}" }.send(user, bot)
}
}
}
Also, you can use input chaining:
inputChain("conversation") {
message { "Nice to meet you, ${update.text}" }.send(user, bot)
message { "What is your favorite food?" }.send(user, bot)
}.breakIf({ update.text == "peanut butter" }) { // chain break condition
message { "Oh, too bad, I'm allergic to it." }.send(user, bot)
// action that will be applied when match
}.andThen {
// next input point if break condition doesn't match
}
You can read more about methods in the FunctionalHandlingDsl
class documentation.
Telegram bot Wiki © KtGram