A wrapper for the Telegram Bot API written in Kotlin.
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()
}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
- 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'
}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.
Core
- Getting updates β polling, webhooks, allowed update types.
- Webhooks β
webhook { }DSL,secretToken, manualprocessUpdate. - Error handling β
TelegramBotResultandtelegramError { }. - Logging.
- File uploads β
TelegramFile.ByFile/ByByteArray/ByInputStream/ByUrl/ByFileId.
Chat and messages
- Chat and ChatFullInfo β the 7.3 split.
- Reply parameters β quoting, cross-chat replies.
- Link preview options β custom URL, size, position.
- Forum topics β create / edit / close / delete; General topic.
- Reactions β set, delete, paid reactions.
Built-in content
Telegram Stars and monetisation
- Telegram Stars β invoices, refunds, transactions.
- Paid media β
sendPaidMedia, purchases. - Gifts β sending and managing.
Business bots
- Business connection β connecting, sending on behalf, account management.
- Stories β
postStory,editStory,deleteStory, clickable areas.
- Fork and clone the repo
- Run
./gradlew ktlintFormat - Run
./gradlew buildto see if tests, ktlint and abi checks pass. - Commit and push your changes
- Submit a pull request to get your changes reviewed
- 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.
Kotlin Telegram Bot is under the Apache 2.0 license. See the LICENSE for more information.