Coder Social home page Coder Social logo

whyoleg / ktd Goto Github PK

View Code? Open in Web Editor NEW
46.0 6.0 11.0 34.53 MB

Kotlin Multiplatform TDLib (Telegram Database library) client

License: Apache License 2.0

Kotlin 99.83% CMake 0.05% C++ 0.12%
tdlib telegram-api kotlin kotlin-multiplatform kotlin-android kotlin-jvm android multiplatform telegram java

ktd's Introduction

ktd

⚠️⚠️⚠️

This project isn't maintained anymore and will be reborned in some way in 2022

⚠️⚠️⚠️

Library provide wrapper over tdlib Telegram API using coroutines Flow and suspend functions. For every tdlib function exists easy to use extension function with parameters and return type.

suspend fun TelegramClient.getChat(chatId: Long): Chat

For listening updates you can use flow with all updates:

val TelegramClient.updates: Flow<TelegramUpdate>

Structure

Telegram - root factory object for creating TelegramClient. It will manage lifecycle of created clients. Telegram implements Job interface and can be closed by calling cancel() Optionally parent Job object can be passed as parameter to bind Telegram to existing lifecycle. To configure low-level parameters for tdlib you can pass TelegramClientConfiguration.

  • receiveTimeout - maximum duration allowed to wait for new data in one call to tdlib. By default 1 second. Sufficient for most use cases but can be changed in future.
  • maxEventsCount - maximum number of responses received from tdlib by one request. By default 1000. Sufficient for most use cases but can be changed in future.
val telegram = Telegram(
    configuration = TelegramClientConfiguration(
        maxEventsCount = 100,
        receiveTimeout = 30.seconds
    )
)

TelegramClient can be created from Telegram and closed or independent by calling cancel() or by canceling Telegram object. Using of client can be checked in examples. TelegramClient - main interface to interact with library.

All extensions can be found in api package. They are grouped by type and exists in 2 kinds:

  • Raw - just type binding for tdlib function:
suspend fun TelegramClient.chat(f: GetChat): Chat
  • Parameterized - functions with inline parameters of function object:
suspend fun TelegramClient.getChat(chatId: Long): Chat

Result of calling this 2 functions will be the same. Parameterized functions just more easy to use and find.

Also sync package exists which contains functions which can be called synchronously using TelegramClient companion object.

fun TelegramClient.Companion.setLogVerbosityLevel(newVerbosityLevel: Int): Ok

Which can be used without TelegramClient instance like this:

TelegramClient.setLogVerbosityLevel(5)

To obtain updates, f.e. for chat last message, updates property on TelegramClient can be used:

client.updates.filterIsInstance<UpdateChatLastMessage>()

Gradle build setup

repositories {
    maven { 
        url "https://dl.bintray.com/whyoleg/ktd"
    }
}

dependencies {
    //td api by version
    implementation "dev.whyoleg.ktd:ktd-api-coroutines-jvm:0.5.0-1.5.4"
    //or for android
    implementation "dev.whyoleg.ktd:ktd-api-coroutines-android:0.5.0-1.5.4"
    //or for common
    implementation "dev.whyoleg.ktd:ktd-api-coroutines:0.5.0-1.5.4"
    //or for another td api version
    implementation "dev.whyoleg.ktd:ktd-api-coroutines-jvm:0.5.0-1.5.0"
}
  • Available artifacts:
    • Client: dev.whyoleg.ktd:ktd-client-[type]-[platform]:[version] - tdlib bindings
    • API: dev.whyoleg.ktd:ktd-api-[type]-[platform]:[version]-[tdlib-version] - generated API accessors
  • Supported configurations:
    • version - ktd version, f.e. 0.5.0
    • type - level of abstraction
      • raw - simple client with low-level bindings
      • corotutines - standard client with suspend functions for execution and updates Flow for consuming updates
    • platform
      • jvm
      • android (only for api, for client jvm can be used instead)
      • metadata (common)
      • omit platfrom to use GRADLE_METADATA dependency
    • tdlib-version
      • 1.5.4
      • 1.5.3
      • 1.5.2
      • 1.5.1
      • 1.5.0

Now library published only to bintray, it will change in future.

Status

For now, library has experimental status, so public interface can be changed any time until 1.0 version.

ktd's People

Contributors

whyoleg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ktd's Issues

Can't find class [org/drinkless/tdlib/Client]

I've added the depency

implementation "dev.whyoleg.ktd:ktd-api-coroutines-jvm:0.5.0-1.5.4"

I've libtdjni.so in my /usr/local/bin. So I've passed -Djava.library.path=/usr/local/bin to VM options (IntelliJ IDEA Run Configuration)

But when I try to create TelegramClient, am getting below error

Try to load tdlib as library
FATAL ERROR in native method: Can't find class [org/drinkless/tdlib/Client]
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1946)
	- locked <0x00000007825868b8> (a java.util.Vector)
	- locked <0x00000007825dc1c8> (a java.util.Vector)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1861)
	at java.lang.Runtime.loadLibrary0(Runtime.java:871)
	at java.lang.System.loadLibrary(System.java:1124)
	at dev.whyoleg.ktd.LinkKt.loadTdlib(Link.kt:8)
	at dev.whyoleg.ktd.TelegramRawClient.<clinit>(TelegramRawClient.kt:8)
	at dev.whyoleg.ktd.NativeClient.<init>(NativeClient.kt:8)
	at dev.whyoleg.ktd.Telegram.client(Telegram.kt:11)
	at MainKt.main(Main.kt:13)
[ 1][t 0][1586099161.561222076][tl_jni_object.cpp:37]	Can't find class [org/drinkless/tdlib/Client]

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

java.lang.UnsatisfiedLinkError: no tdjni in java.library.path:

Hi,

when starting TelegramClient, I get the following exception:

Try to load tdlib as library
Try to load tdlib from artifact
java.lang.UnsatisfiedLinkError: no tdjni in java.library.path: [/usr/java/packages/lib, /usr/lib/x86_64-linux-gnu/jni, /lib/x86_64-linux-gnu, /usr/lib/x86_64-linux-gnu, /usr/lib/jni, /lib, /usr/lib]
	at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2670)
	at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
	at java.base/java.lang.System.loadLibrary(System.java:1873)
	at dev.whyoleg.ktd.LinkKt.loadTdlib(Link.kt:8)
	at dev.whyoleg.ktd.TelegramRawClient.<clinit>(TelegramRawClient.kt:8)
	at dev.whyoleg.ktd.NativeClient.<init>(NativeClient.kt:8)
	at dev.whyoleg.ktd.Telegram.client(Telegram.kt:11)
	at fe.iv.desktop.MainKt.main(Main.kt:30)
	at fe.iv.desktop.MainKt$$$main.invoke(Unknown Source)
	at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$1.invokeSuspend(IntrinsicsJvm.kt:205)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlin.coroutines.ContinuationKt.startCoroutine(Continuation.kt:115)
	at kotlin.coroutines.jvm.internal.RunSuspendKt.runSuspend(RunSuspend.kt:19)

I'm using Mint 19

Release of 0.6.0

Hi Oleg!
I was just wondering when you plan to release 0.6.0, which I see is in the development branch.
I'd like to try and port some bot's code to the JVM, but it seems 0.6.0 includes many differences with 0.5.0, and I don't want to start writing and then "fix" the code again.
Don't worry btw, I don't want to put pressure on you, since you probably have other things to do.

Thanks!

Exception in thread "main" java.lang.UnsatisfiedLinkError: dev.whyoleg.ktd.TelegramRawClient.create()J

When I try to

import dev.whyoleg.ktd.Telegram
import dev.whyoleg.ktd.TelegramClientConfiguration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

@ExperimentalTime
suspend fun main() {

    val telegram = Telegram(
        configuration = TelegramClientConfiguration(
            maxEventsCount = 1000,
            receiveTimeout = 1.seconds //ms
        )
    )

    val client = telegram.client()
}

I am getting below error

Try to load tdlib as library
Exception in thread "main" java.lang.UnsatisfiedLinkError: dev.whyoleg.ktd.TelegramRawClient.create()J
	at dev.whyoleg.ktd.TelegramRawClient.create(Native Method)
	at dev.whyoleg.ktd.NativeClient.<init>(NativeClient.kt:8)
	at dev.whyoleg.ktd.Telegram.client(Telegram.kt:11)
	at MainKt.main(Main.kt:19)
	at MainKt$$$main.invoke(Unknown Source)
	at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$1.invokeSuspend(IntrinsicsJvm.kt:199)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlin.coroutines.ContinuationKt.startCoroutine(Continuation.kt:114)
	at kotlin.coroutines.jvm.internal.RunSuspendKt.runSuspend(RunSuspend.kt:19)
	at MainKt.main(Main.kt)

Remove prints from loadTdlib

Function loadTdlib has noisy prints and I can't disable it. Eg:

println("Try to load tdlib as library")
...
it.printStackTrace()
println("Try to load tdlib from artifact")
...
it.printStackTrace()
println("Try to load tdlib from local path: $path")
...
println("Can't load tdlib")

Maybe remove this lines?

bintray.com is closed

I'm trying to build your app with travis/github actions and getting this error

It looks like bintray is closed.

> Could not resolve all artifacts for configuration ':app:debugCompileClasspath'.
   > Could not resolve dev.whyoleg.ktd:ktd-api-coroutines:0.5.0-1.5.4.
     Required by:
         project :app
      > Could not resolve dev.whyoleg.ktd:ktd-api-coroutines:0.5.0-1.5.4.
         > Could not get resource 'https://dl.bintray.com/whyoleg/ktd/dev/whyoleg/ktd/ktd-api-coroutines/0.5.0-1.5.4/ktd-api-coroutines-0.5.0-1.5.4.pom'.
            > Could not GET 'https://dl.bintray.com/whyoleg/ktd/dev/whyoleg/ktd/ktd-api-coroutines/0.5.0-1.5.4/ktd-api-coroutines-0.5.0-1.5.4.pom'. Received status code 403 from server: Forbidden```

How add JNI part

After build binary for mac i add dylib from example/java/build/ in root project folder.
But after launch i catch error:

FATAL ERROR in native method: Can't find class [org/drinkless/tdlib/Client]
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1934)
	- locked <0x00000007955868b0> (a java.util.Vector)
	- locked <0x00000007955f5490> (a java.util.Vector)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1850)
	at java.lang.Runtime.loadLibrary0(Runtime.java:871)
	- locked <0x000000079559e488> (a java.lang.Runtime)
	at java.lang.System.loadLibrary(System.java:1124)
	at dev.whyoleg.ktd.LinkKt.loadTdlib(Link.kt:8)
	at dev.whyoleg.ktd.TelegramRawClient.<clinit>(TelegramRawClient.kt:8)
	at dev.whyoleg.ktd.NativeClient.<init>(NativeClient.kt:8)
	at dev.whyoleg.ktd.Telegram.client(Telegram.kt:11)
	at MainKt.main(Main.kt:17)
	at MainKt$$$main.invoke(Unknown Source)
	at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$1.invokeSuspend(IntrinsicsJvm.kt:205)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlin.coroutines.ContinuationKt.startCoroutine(Continuation.kt:115)
	at kotlin.coroutines.jvm.internal.RunSuspendKt.runSuspend(RunSuspend.kt:19)
	at MainKt.main(Main.kt)
[ 1][t 0][1601808549.902974128][tl_jni_object.cpp:37]	Can't find class [org/drinkless/tdlib/Client]

What i should do for fix this? Any guide for this?

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.