Skip to content

Update parsing

Jey edited this page Jan 12, 2024 · 18 revisions

Text payload

Certain updates may have text payload that can be parsed for further processing. Let's take a look at them:

TextReference

From the listed updates, a certain parameter is selected and taken as TextReference, for further parsing.

List of chosen parameters is below:

  • MessageUpdate -> message.text
  • EditedMessageUpdate -> editedMessage.text
  • ChannelPostUpdate -> channelPost.text
  • EditedChannelPostUpdate -> editedChannelPost.text
  • InlineQueryUpdate -> inlineQuery.query
  • ChosenInlineResultUpdate -> chosenInlineResult.query
  • CallbackQueryUpdate -> callbackQuery.data
  • ShippingQueryUpdate -> shippingQuery.invoicePayload
  • PreCheckoutQueryUpdate -> preCheckoutQuery.invoicePayload
  • PollUpdate -> poll.question

Parsing

The selected parameters are parsed with the appropriate configured delimiters into the command and parameters to it.

See configuration commandParsing block.

You can see in the diagram below which components are mapped to which parts of the target function.

Text parsing diagram

@ParamMapping

There is also an annotation called @ParamMapping for convenience or for some special case.

It allows you to map the name of the parameter from the input text to any parameter.

This is also convenient when your input data is limited, for example, CallbackData (64 characters).

See example of usage: greeting?name=Adam

@CommandHandler(["greeting"])
suspend fun greeting(@ParamMapping("name") anyParameterName: String, user: User, bot: TelegramBot) {
    message { "Hello, $anyParameterName" }.send(to = user, via = bot)
}

And also it can be used for catching unnamed parameters, in cases where the parser is set up such that parameter names are skipped or even they absent, which passes by 'param_n' pattern, where n is its ordinal.

So you can abbreviate the variable names in the callback itself and use clear readable names in the code.

Deeplink

Also, as a special case, if a deepLink is present, it will be passed as a parameter named deepLink.

Therefore, if you expect it in the /start command, you should declare it and if it is present, it will be passed.

@CommandHandler(["/start"])
suspend fun start(deepLink: String?, user: User, bot: TelegramBot) {
    message { "deeplink is $deepLink" }.send(to = user, via = bot)
}

See also

Clone this wiki locally