A Kotlin library for interacting with Telegram API using TDLib. This SDK provides a simple way to listen to messages from specified Telegram chats and handle authentication.
- 🔐 Secure authentication using TDLib
- 📱 Support for multiple platforms (Linux, macOS, Windows)
- 💬 Real-time message listening
- 🔄 Coroutine-based API with Kotlin Flow
- 🛡️ Error handling and state management
- 📦 Easy integration with existing projects
- Kotlin 1.8.0 or higher
- JDK 11 or higher
- Telegram API credentials (api_id and api_hash)
Add the following to your build.gradle.kts:
repositories {
mavenCentral()
// Add your repository here if the library is hosted privately
}
dependencies {
implementation("com.velkonost:telegram-listener-sdk:0.1.0") // Replace with actual version
}- Visit https://my.telegram.org/auth
- Log in with your phone number
- Go to 'API development tools'
- Create a new application
- Note down your
api_idandapi_hash
import velkonost.telegram.sdk.listener.TGListenerSDK
// Initialize the SDK with your credentials
TGListenerSDK.setup(
apiId = YOUR_API_ID, // Int
apiHash = "YOUR_API_HASH", // String
phoneNumber = "+1234567890", // String
databaseDirectory = "database", // Optional, defaults to "database"
filesDirectory = "files" // Optional, defaults to "files"
)import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.runBlocking
import velkonost.telegram.sdk.listener.TGListenerSDK
import velkonost.telegram.sdk.listener.model.NewMessage
// Start listening to messages from specific chats
val messageFlow = TGListenerSDK.startListenChats(
includeOutgoing = true, // Set to false to exclude outgoing messages
chats = listOf(123456789L) // List of chat IDs to listen to
)
// Collect messages
runBlocking {
messageFlow.collect { message ->
println("New message in chat ${message.chatId}: ${message.text}")
message.replyMessageText?.let { reply ->
println("Reply to: $reply")
}
}
}The SDK supports the following platforms:
- Linux (x64, arm64)
- macOS (Apple Silicon)
- Windows (x64)
The appropriate native library will be automatically loaded based on your system.
The SDK handles the authentication process automatically:
- When
setup()is called, the SDK initializes TDLib - If this is the first run, you'll need to enter the verification code sent to your Telegram account
- The authentication state is managed internally
- Once authenticated, the session is persisted in the database directory
The SDK uses a custom exception hierarchy for error handling:
try {
// SDK operations
} catch (e: TelegramException.Error) {
// Handle general Telegram API errors
println("Error: ${e.message}")
} catch (e: TelegramException.UnexpectedResult) {
// Handle unexpected API responses
println("Unexpected result: ${e.message}")
}Messages are represented by the NewMessage data class:
data class NewMessage(
val chatId: Long, // ID of the chat
val text: String, // Message text
val replyMessageText: String? = null // Text of the message being replied to, if any
)The SDK uses two main directories:
databaseDirectory: Stores TDLib database filesfilesDirectory: Stores downloaded files
Make sure these directories are writable by your application.
- Never commit your
api_idandapi_hashto version control - Store credentials securely (e.g., in environment variables or a secure configuration)
- The database directory may contain sensitive information, ensure it's properly secured
- Use appropriate file permissions for the database and files directories
Contributions are welcome! Please feel free to submit a Pull Request.
For support, please open an issue in the GitHub repository.