Skip to content

Actions

Jey edited this page Sep 3, 2024 · 19 revisions

All requests is Actions

All telegram api requests are various kinds of TgAction interfaces that implementing different methods such as SendMessageAction,
which have wrapped in the form of message() - type functions for the convenience of the library interface.

Actions diagram

Each Action may be able of having its own possible methods, depending on the available Feature.

Features

Different actions may have different Features depending on the Telegram Bot Api, such as: OptionsFeature, MarkupFeature EntitiesFeature CaptionFeature.

Let's take a closer look at them:

Options

For example, OptionsFeature is used to pass optional parameters.

Each action has its own type of options, the corresponding you can see in the Action itself in the options parameter, in properties section.
For example, sendMessage which contains a MessageOptions data class with different parameters as options.

Example usage:

message{ "*Test*" }.options {
    parseMode = ParseMode.Markdown
}.send(user, bot)

Markup

There is also a method for sending markups that supports all kind of keyboards:
ReplyKeyboardMarkup, InlineKeyboardMarkup, ForceReply, ReplyKeyboardRemove.

Inline Keyboard Markup

This builder allows you to construct inline buttons with any combination of parameters.

message{ "Test" }.inlineKeyboardMarkup {
    "name" callback "callbackData"         //
    "buttonName" url "https://google.com"  //--- these two buttons will be in the same row.
    newLine() // or br()
    "otherButton" webAppInfo "data"       // this will be in other row

    // you can also use a different style within the builder:
    callbackData("buttonName") { "callbackData" }
}.send(user, bot)

More details can be seen in the builder documentation.

Reply Keyboard Markup

This builder allows you to construct menu buttons.

message{ "Test" }.replyKeyboardMarkup {
  + "Menu button"     // you can add buttons by using unary plus operator
  + "Menu button 2"
  br() // go to second row
  "Send polls 👀" requestPoll true   // button with parameter

  options {
    resizeKeyboard = true
  }
}.send(user, bot)

Additional options applicable to the keyboard can be seen in ReplyKeyboardMarkupOptions.

See the builder documentation for more details about the methods.

It's mostly convenient to use dsl for collecting keyboard markup, but if needed, you can also add markup manually.

message{ "*Test*" }.markup {
    InlineKeyboardMarkup(
        InlineKeyboardButton("test", callbackData = "testCallback")
    )
}.send(user, bot)
message{ "*Test*" }.markup {
    ReplyKeyboardMarkup(
        KeyboardButton("Test menu button")
    )
}.send(user, bot)

Entities

There is also a method for sending MessageEntity.

Example usage:

message{ "Test \$hello" }.replyKeyboardMarkup {
    +"Test menu button"
}.entities {
    5 to 15 url "https://google.com" // add TextLink
    entity(EntityType.Bold, 0, 4)
    entity(EntityType.Cashtag, 5, 5) // backslash doesn't count (because it's used for compiler)
}.send(user, bot)

Contextual entities.

Entities can also be added through the context of some constructs, they are labeled with a specific EntitiesContextBuilder interface, it is also present in the caption feature.

Example usage:

message { "usual text " - bold { "this is bold text" } - " continue usual" }.send(user, bot)

All kinds of entity types are supported.

Caption

Also, the caption method can be used to add captions to media files.

Example usage:

photo { "FILE_ID" }.caption { "Test caption" }.send(user, bot)

See also