2024-01-12 02:44:30 +06:00
|
|
|
# TelegramBotAPI [](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi) [](https://core.telegram.org/bots/api-changelog#december-29-2023)
|
2018-12-26 16:07:24 +08:00
|
|
|
|
2023-06-10 18:16:01 +06:00
|
|
|
| Docs | [](https://tgbotapi.inmo.dev/index.html) [](https://docs.inmo.dev/tgbotapi/index.html) |
|
2023-04-18 12:54:11 +06:00
|
|
|
|:----------------------:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
|
|
|
|
| Useful repos | [](https://github.com/InsanusMokrassar/TelegramBotAPI-bot_template/generate) [](https://github.com/InsanusMokrassar/TelegramBotAPI-examples/) |
|
|
|
|
| Misc | [](https://github.com/KotlinBy/awesome-kotlin) [](https://docs.google.com/forms/d/e/1FAIpQLSctdJHT_aEniyYT0-IUAEfo1hsIlezX2owlkEAYX4KPl2V2_A/viewform?usp=sf_link) |
|
|
|
|
| Platforms |   |
|
2023-04-29 11:19:06 +06:00
|
|
|
| Experimental Platforms | [](https://kotlinlang.org/docs/native-target-support.html#tier-1) [](https://kotlinlang.org/docs/native-target-support.html#tier-1) |
|
2022-08-05 01:48:44 +06:00
|
|
|
|
2023-02-24 15:31:33 +06:00
|
|
|
<!--- [](https://t.me/ktgbotapi) --->
|
2022-08-05 01:48:44 +06:00
|
|
|
|
|
|
|
<p align="center">
|
2023-02-24 15:31:33 +06:00
|
|
|
<a href="https://t.me/ktgbotapi">
|
2022-08-05 01:48:44 +06:00
|
|
|
<img src="./resources/tg_channel_qr.jpg">
|
|
|
|
</a>
|
|
|
|
</p>
|
2021-08-22 10:58:32 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
Hello! This is a set of libraries for working with Telegram Bot API.
|
2020-11-16 21:59:25 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
## Examples
|
2020-04-17 15:43:43 +06:00
|
|
|
|
2021-02-13 01:38:31 +06:00
|
|
|
There are several things you need to do to launch examples below:
|
|
|
|
|
|
|
|
* Add `mavenCentral()` to your project repositories
|
|
|
|
* [Maven variant](https://github.com/InsanusMokrassar/TelegramBotAPI/wiki/Including-in-your-project#pomxml)
|
|
|
|
* Add dependency `implementation "dev.inmo:tgbotapi:$tgbotapi_version"`
|
2021-02-13 01:42:35 +06:00
|
|
|
* Replace `tgbotapi_version` with exact version (see last one in the table above) or put variable with this name in project
|
2021-02-13 01:38:31 +06:00
|
|
|
* Alternative variant for maven [here](https://github.com/InsanusMokrassar/TelegramBotAPI/wiki/Including-in-your-project#telegrambotapi)
|
|
|
|
|
2021-02-13 10:09:33 +06:00
|
|
|
More including instructions [available here](https://github.com/InsanusMokrassar/TelegramBotAPI/wiki/Including-in-your-project).
|
|
|
|
Other configuration examples:
|
|
|
|
|
|
|
|
* [For multiplatform](https://github.com/InsanusMokrassar/TelegramBotAPI-examples/tree/master/ResenderBot)
|
|
|
|
* [For JVM](https://github.com/InsanusMokrassar/TelegramBotAPI-examples/blob/master/GetMeBot/build.gradle)
|
2021-02-13 01:38:31 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
### Most common example
|
2020-04-17 15:43:43 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
```kotlin
|
|
|
|
suspend fun main() {
|
|
|
|
val bot = telegramBot(TOKEN)
|
2020-04-17 15:43:43 +06:00
|
|
|
|
2022-03-14 02:50:38 +06:00
|
|
|
bot.buildBehaviourWithLongPolling {
|
2021-02-11 12:06:34 +06:00
|
|
|
println(getMe())
|
|
|
|
|
|
|
|
onCommand("start") {
|
|
|
|
reply(it, "Hi:)")
|
2020-04-17 15:43:43 +06:00
|
|
|
}
|
2021-02-11 12:06:34 +06:00
|
|
|
}.join()
|
2020-04-17 15:43:43 +06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
In this example you will see information about this bot at the moment of starting and answer with `Hi:)` every time it
|
|
|
|
gets message `/start`
|
2020-05-14 14:07:23 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
### Handling only last messages
|
2020-04-08 15:28:03 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
```kotlin
|
|
|
|
suspend fun main() {
|
|
|
|
val bot = telegramBot(TOKEN)
|
2020-05-14 13:44:18 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
val flowsUpdatesFilter = FlowsUpdatesFilter()
|
|
|
|
bot.buildBehaviour(flowUpdatesFilter = flowsUpdatesFilter) {
|
|
|
|
println(getMe())
|
|
|
|
|
|
|
|
onCommand("start") {
|
|
|
|
reply(it, "Hi:)")
|
|
|
|
}
|
2020-05-14 13:44:18 +06:00
|
|
|
|
2021-02-11 13:37:24 +06:00
|
|
|
retrieveAccumulatedUpdates(this).join()
|
2021-02-11 13:35:06 +06:00
|
|
|
}
|
2021-02-11 12:06:34 +06:00
|
|
|
}
|
2020-05-14 13:44:18 +06:00
|
|
|
```
|
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
The main difference with the previous example is that bot will get only last updates (accumulated before bot launch
|
|
|
|
and maybe some updates it got after launch)
|
2020-05-14 13:44:18 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
### Build a little bit more complex behaviour
|
2020-05-14 13:44:18 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
```kotlin
|
|
|
|
suspend fun main() {
|
|
|
|
val bot = telegramBot(TOKEN)
|
2020-05-14 13:44:18 +06:00
|
|
|
|
2022-03-14 02:50:38 +06:00
|
|
|
bot.buildBehaviourWithLongPolling {
|
2021-02-11 12:06:34 +06:00
|
|
|
println(getMe())
|
2020-05-14 13:44:18 +06:00
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
val nameReplyMarkup = ReplyKeyboardMarkup(
|
|
|
|
matrix {
|
|
|
|
row {
|
|
|
|
+SimpleKeyboardButton("nope")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
onCommand("start") {
|
|
|
|
val photo = waitPhoto(
|
|
|
|
SendTextMessage(it.chat.id, "Send me your photo please")
|
|
|
|
).first()
|
|
|
|
|
|
|
|
val name = waitText(
|
|
|
|
SendTextMessage(
|
|
|
|
it.chat.id,
|
|
|
|
"Send me your name or choose \"nope\"",
|
2021-02-11 13:38:55 +06:00
|
|
|
replyMarkup = nameReplyMarkup
|
2021-02-11 12:06:34 +06:00
|
|
|
)
|
|
|
|
).first().text.takeIf { it != "nope" }
|
|
|
|
|
|
|
|
sendPhoto(
|
|
|
|
it.chat,
|
|
|
|
photo.mediaCollection,
|
|
|
|
entities = buildEntities {
|
|
|
|
if (name != null) regular(name) // may be collapsed up to name ?.let(::regular)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}.join()
|
2020-05-14 13:44:18 +06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-02-11 12:06:34 +06:00
|
|
|
### More examples
|
|
|
|
|
|
|
|
You may find examples in [this project](https://github.com/InsanusMokrassar/TelegramBotAPI-examples). Besides, you are
|
2024-01-08 18:29:59 +08:00
|
|
|
always welcome in our [docs](https://docs.inmo.dev/tgbotapi/index.html) and
|
2021-02-11 12:06:34 +06:00
|
|
|
[chat](https://t.me/InMoTelegramBotAPIChat).
|