Skip to content

g000sha256/tdl-coroutines

Repository files navigation

TDL Coroutines

Maven Central TDLib Version

Platform Platform

This library provides a Kotlin Coroutines client for the Telegram Database Library (TDLib).

Gradle setup

repositories {
    mavenCentral()
}
dependencies {
    implementation("dev.g000sha256:tdl-coroutines:1.4.0")
}

Usage

Caution

The TDLib frequently includes breaking changes in its minor versions. To reduce breaking changes when updating the TDL Coroutines library, use named arguments for constructors and methods, as new parameters may be added in future releases.

How to create a client

val client = TdlClient.create()

How to subscribe to updates

The TdlClient provides 159 update flows, as well as one that includes all updates.

coroutineScope.launch {
    client.authorizationStateUpdates.collect { updateAuthorizationState ->
        val authorizationState = updateAuthorizationState.authorizationState
        // TODO
    }
}
coroutineScope.launch {
    client.allUpdates.collect { update ->
        when (update) {
            is UpdateAuthorizationState -> {
                val authorizationState = update.authorizationState
                // TODO
            }
            is UpdateOption -> {
                val name = update.name
                val value = update.value
                // TODO
            }
            // TODO
        }
    }
}

How to send a request

The TdlClient provides 877 request methods.

coroutineScope.launch {
    val result = client.getAuthorizationState()
    when (result) {
        is TdlResult.Failure -> {
            val code = result.code
            val message = result.message
            // TODO
        }
        is TdlResult.Success -> {
            val authorizationState = result.result
            // TODO
        }
    }
}