Kotlin Coroutines extensions for Telegram API TDLib (Telegram Database library)
The main class of the library is TelegramFlow. It converts Telegram Updates handlers to the Kotlin Coroutine Flows and Telegram callback-style Functions to Kotlin Coroutine suspend functions
- Create instance of TelegramFlow
- You can collect flow of TdApi.Objects from the TelegramFlow instance and its flow extensions.
- Call attachClient function of the TelegramFlow instance to connect it to the Telegram Client.
- Now you can use numerous extension functions to send data to the Telegram API and collect data from flow extensions
Any update listed in TdApi can be collected by corresponding flow extension of the TelegramFlow.
telegramFlow.userStatusFlow().collect { status: TdApi.UpdateUserStatus ->
// collect UpdateUserStatus from Telegram
}
For the Updates where there is the only field available inside, Update class is suppress by the flow extension and return data itself, for example:
telegramFlow.authorizationStateFlow().collect { state: TdApi.AuthorizationState ->
// collect AuthorizationState instead of TdApi.UpdateAuthorizationState since there is no other data inside
}
Any funcrion listed in TdApi can be called via corresponding coroutine extension
suspend fun sendCode(code: String) {
api.checkAuthenticationCode(code) // send TdApi.CheckAuthenticationCode(code) to the Client
}
Library provides extension interfaces to access specific Telegram Object's extensions. This allows you to use the library full potential
class YourTelegramClass : UserKtx {
// Instance of the TelegramFlow connecting extensions to the API
override val api = TelegramFlow()
// Flow that returns updates of the user as a full UserInfo
val fullInfoFlow: Flow<TdApi.UserFullInfo> = api.userFlow().map { user ->
user.getFullInfo() // call TdApi.GetUserFullInfo(userId) with id of the user instance
}
}
All possible extensions for Telegram entities can be accessed via TelegramKtx. List and description of available extension interfaces can be found here