Skip to content

Behaviour Builder

In the previous pages about updates handling and was mentioned that currently in the most cases you should use Flows. So, there is an improvement for that system which hide direct work with flows and allow you to create more declarative logic of your bot.

Main parts of Behaviour Builder

There are several things you should know for better understanding of behaviour builder:

  • BehaviourContext - it is the thing which contains all necessary tools for working with bots
  • Triggers - on* extensions for BehaviourContext which allow you to create reaction on some update
  • Expectations (or waiters) - wait* extensions which you may use in buildBehaviour function, but it is recommended to use it in bodies of triggers

Initialization

As was said above, there is buildBehaviour function which allow you set up your bot logic. Let’s see an example:

val bot = telegramBot("TOKEN")

bot.buildBehaviour {
  onCommand("start") { // creating of trigger
    val message = it
    val content = message.content

    reply(message, "Ok, send me one photo") // send text message with replying on incoming message

    val photoContent = waitPhoto().first() // waitPhoto will return List, so, get first element

    val photo = downloadFile(photoContent) // ByteArray of photo

    // some logic with saving of photos
  }
}

Filters

In most cases there are opportunity to filter some of messages before starting of main logic. Let’s look at this using the example above:

val bot = telegramBot("TOKEN")

bot.buildBehaviour {
  onCommand(
    "start",
    initialFilter = {
      it.content.textSources.size == 1 // make sure that user has sent /start without any additions
    }
  ) {
    // ...
  }
}

OR

val bot = telegramBot("TOKEN")

bot.buildBehaviour {
  onCommand(
    "start",
    requireOnlyCommandInMessage = true // it is default, but you can overwrite it with `requireOnlyCommandInMessage = false`
  ) {
    // ...
  }
}

Long polling

In case you wish to start long polling, you have two options:

  • Start long polling with buildBehaviour additional functions
  • Start long polling from using the result of buildBehaviour

More info

More info about updates retrieving you may read at the Long Polling and Webhooks articles

Long polling from buildBehaviour

With prepared bot:

val bot = telegramBot("TOKEN")

bot.buildBehaviourWithLongPolling {
    onCommand(
        "start",
        requireOnlyCommandInMessage = true // it is default, but you can overwrite it with `requireOnlyCommandInMessage = false`
    ) {
        // ...
    }
}

Without prepared bot:

telegramBotWithBehaviourAndLongPolling("TOKEN") {
    onCommand(
        "start",
        requireOnlyCommandInMessage = true // it is default, but you can overwrite it with `requireOnlyCommandInMessage = false`
    ) {
        // ...
    }
}

Long polling from the result of behaviour building

val bot = telegramBot("TOKEN")
val rootContext = bot.buildBehaviour {
    onCommand(
        "start",
        requireOnlyCommandInMessage = true // it is default, but you can overwrite it with `requireOnlyCommandInMessage = false`
    ) {
        // ...
    }
}

bot.longPolling(rootContext) // start listening updates