Td Api Text Entity Type - OTR/Kotlin-Telegram-Client GitHub Wiki

Class TdApi.TextEntityType

Description

Represents entities contained in the text. Entities can be nested, but must not mutually intersect with each other. Pre, Code and PreCode entities can't contain other entities. Bold, Italic, Underline and Strikethrough entities can contain and to be contained in all other entities. All other entities can't contain each other.

Only *MentionName, *PreCode and *TextUrl has public properties, other subclasses are just empty classes.

Class TdApi.TextEntityTypeMentionName

A text shows instead of a raw mention of the user (e.g., when the user has no username).

Property

  • userId : Long - Identifier of the mentioned user.

Reference

TdApi.TextEntityTypeMentionName

Class TdApi.TextEntityTypePreCode

Text that must be formatted as if inside pre, and code HTML tags.

Property

  • language : String - Programming language of the code; as defined by the sender.

Reference

TdApi.TextEntityTypePreCode

Class TdApi.TextEntityTypeTextUrl

A text description shown instead of a raw URL.

Property

  • url : String - HTTP or tg:// URL to be opened when the link is clicked.

Reference

TdApi.TextEntityTypeTextUrl

Other subclasses are:

Example


/**
 * Entities contained in the text.
 * Entities can be nested, but must not mutually intersect with each other.
 * Pre, Code and PreCode entities can't contain other entities.
 * Bold, Italic, Underline and Strikethrough entities can contain
 * and to be contained in all other entities.
 * All other entities can't contain each other.
 */
fun someFunThatGetsFormattedText(formattedText: TdApi.FormattedText) {
    val entities: Array<TdApi.TextEntity> = formattedText.entities

    for (entity in entities) {
        // Length of the entity, in UTF-16 code units.
        val entityLength: Int = entity.length
        // Offset of the entity in UTF-16 code units.
        val entityOffset: Int = entity.offset
        // Type of the entity.
        val entityType: TdApi.TextEntityType = entity.type
        when (entityType) {

            is TdApi.TextEntityTypeMentionName -> {
                val userId: Long = entityType.userId
            }
            is TdApi.TextEntityTypePreCode -> {
                val language: String = entityType.language
            }
            is TdApi.TextEntityTypeTextUrl -> {
                val url: String = entityType.url
            }
            // Other types such as Italic, Bold, Underline, Strikethrough
            // has no public properties, so at least you could get their class name
            is TdApi.TextEntityTypeBold -> {
                val typeName: String = entityType.javaClass.simpleName
                    .removePrefix("TextEntityType")
                println("This text entity has $typeName style")
            }
        }
    }
}

Reference

TdApi.TextEntityType