Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

322 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Kotlin Telegram Bot

Build Status Release ktlint

A wrapper for the Telegram Bot API written in Kotlin.

Usage

Creating a bot instance is really simple:

fun main() {
    val bot = bot {
        token = "YOUR_API_KEY"
    }
}

Now lets poll telegram API and route all text updates:

fun main() {
    val bot = bot {
        token = "YOUR_API_KEY"
        dispatch {
            text {
                bot.sendMessage(ChatId.fromId(message.chat.id), text = text)
            }
        }
    }
    bot.startPolling()
}

Want to route commands?:

fun main() {
    val bot = bot {
        token = "YOUR_API_KEY"
        dispatch {
            command("start") {
                val result = bot.sendMessage(chatId = ChatId.fromId(message.chat.id), text = "Hi there!")
                result.fold({
                    // do something here with the response
                },{
                    // do something with the error 
                })
            }
        }
    }
    bot.startPolling()
}

Examples

Take a look at the examples folder.

There are several samples:

  • A simple echo bot
  • A more complex sample with commands, filter, reply markup keyboard and more
  • A sample getting updates through Telegram's webhook using a Netty server
  • An example bot sending polls and listening to poll answers

Download

  • Add the JitPack repository to your root build.gradle file:
repositories {
    maven { url "https://jitpack.io" }
}
  • Add the code to your module's build.gradle file:
dependencies {
    implementation 'io.github.kotlin-telegram-bot.kotlin-telegram-bot:telegram:x.y.z'
}

Upgrading from 6.x

The current line (10.x) is up to date with Bot API 10.0 and includes the breaking changes Telegram introduced between 6.3 and 10.0. If you're coming from 6.2.0 or earlier, read docs/MIGRATION_10.md before bumping β€” most code will keep compiling, but getChat now returns ChatFullInfo (not Chat), the legacy reply / forward / preview parameters have richer replacements, and the duplicate entities.SuccessfulPayment was removed.

Detailed documentation

Core

  • Getting updates β€” polling, webhooks, allowed update types.
  • Webhooks β€” webhook { } DSL, secretToken, manual processUpdate.
  • Error handling β€” TelegramBotResult and telegramError { }.
  • Logging.
  • File uploads β€” TelegramFile.ByFile / ByByteArray / ByInputStream / ByUrl / ByFileId.

Chat and messages

Built-in content

Telegram Stars and monetisation

Business bots

  • Business connection β€” connecting, sending on behalf, account management.
  • Stories β€” postStory, editStory, deleteStory, clickable areas.

Contributing

  1. Fork and clone the repo
  2. Run ./gradlew ktlintFormat
  3. Run ./gradlew build to see if tests, ktlint and abi checks pass.
  4. Commit and push your changes
  5. Submit a pull request to get your changes reviewed

Thanks

  • Big part of the architecture of this project is inspired by python-telegram-bot, check it out!
  • Some awesome kotlin ninja techniques were grabbed from Fuel.

License

Kotlin Telegram Bot is under the Apache 2.0 license. See the LICENSE for more information.

About

πŸ€– A wrapper for the Telegram Bot API written in Kotlin

Topics

Resources

Stars

974 stars

Watchers

16 watching

Forks

Releases

Packages

Used by

Contributors

Languages