Giter Club home page Giter Club logo

td-ktx's Introduction

Telegram Flow

Kotlin Coroutines extensions for Telegram API TDLib (Telegram Database library)

Using 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

Start using

  1. Create instance of TelegramFlow
  2. You can collect flow of TdApi.Objects from the TelegramFlow instance and its flow extensions.
  3. Call attachClient function of the TelegramFlow instance to connect it to the Telegram Client.
  4. Now you can use numerous extension functions to send data to the Telegram API and collect data from flow extensions

Using Flows

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
}

Using Functions

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
}

Using extensions interfaces

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

td-ktx's People

Contributors

alexey-kravtsov-tc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.