Popularity
5.5
Growing
Activity
9.1
-
189
3
15

Programming language: Kotlin
License: Apache License 2.0
Tags: Tools     Kotlin     KotlinJVM     Telegram-bot-api     Telegram     Bot    
Latest version: v2.3.2

Kotlin Telegram Bot alternatives and similar libraries

Based on the "Tools" category.
Alternatively, view telegram-bot alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of Kotlin Telegram Bot or a related project?

Add another 'Tools' Library

README

Telegram bot api library logo

Telegram Bot

Maven Central Supported version

KDocs Awesome Kotlin Badge Chat in Telegram

Kotlin based wrapper over Telegram API.

Installation

Add the MavenCentral repository to your root build.gradle.kts file:

repositories {
    mavenCentral()
}

Now add the library itself to the dependencies' module that you need it.

dependencies {
    implementation("eu.vendeli:telegram-bot:2.3.2")
}

Samples

You can see the samples in the telegram-bot_template repository. In the basic branch itself there is an empty draft that can be used to create any bot you want.

there you can find in the appropriate branches:

Usage

fun main() = runBlocking {
    val bot = TelegramBot("BOT_TOKEN", "com.example.controllers")
    /**
     * Second parameter is the package in which commands/inputs will be searched.
     */

    bot.handleUpdates()
    // start long-polling listener
}

@CommandHandler(["/start"])
suspend fun start(user: User, bot: TelegramBot) {
    message { "Hello, what's your name?" }.send(user, bot)
    bot.inputListener.set(user.id, "conversation")
}

@InputHandler(["conversation"])
suspend fun startConversation(user: User, bot: TelegramBot) {
    message { "Nice to meet you, ${message.text}" }.send(user, bot)
    message { "What is your favorite food?" }.send(user, bot)
    bot.inputListener.set(user.id, "conversation-2step")
}
//..

It is also possible to process updates manually:

fun main() = runBlocking {
    val bot = TelegramBot("BOT_TOKEN")

    bot.handleUpdates { update ->
        onCommand("/start") {
            message { "Hello, what's your name?" }.send(user, bot)
            bot.inputListener.set(user.id, "conversation")
        }
        inputChain("conversation") {
            message { "Nice to meet you, ${message.text}" }.send(user, bot)
            message { "What is your favorite food?" }.send(user, bot)
        }.breakIf({ message.text == "peanut butter" }) { // chain break condition
            message { "Oh, too bad, I'm allergic to it." }.send(user, bot)
            // action that will be applied when match
        }.andThen {
            // ...
        }
    }
}

You can also change additional parameters of the bot:

// ...
val bot = TelegramBot("BOT_TOKEN") {
    inputListener = RedisInputListenerImpl()
    classManager = KoinClassManagerImpl()
    logging {
        botLogLevel = Level.DEBUG
    }
}
// ...

A more complete list of settings can be found in BotConfiguration class.

It is also possible to do more advanced processing with a manual listener setting with bot.update.setListener {} function.

for webhook handling you can use any server and bot.update.handle() function (or use this function if you're directly setting listener), \ and for set webhook you can use this method:

setWebhook("https://site.com").send(bot)

if you want to operate with response you can use sendAsync() instead of send() method, which returns Response:

message { "test" }.sendAsync(user, bot).await().onFailure {
    println("code: ${it.errorCode} description: ${it.description}")
}

Any async request returns a Response on which you can also use methods getOrNull() , isSuccess() , onFailure() .

More about

You can also read more information in the wiki, and you're always welcome in chat.