2021-02-07 06:44:38 +00:00
|
|
|
[Participate in our common survey ☺](https://forms.gle/q6Xf8K3fD1pPsYUw9)
|
2021-02-04 19:18:38 +00:00
|
|
|
|
2018-12-26 08:07:24 +00:00
|
|
|
# TelegramBotAPI
|
|
|
|
|
2021-11-09 07:40:38 +00:00
|
|
|
| [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Build Status](https://github.com/InsanusMokrassar/TelegramBotAPI/workflows/Build/badge.svg)](https://github.com/InsanusMokrassar/TelegramBotAPI/actions) [![Small survey](https://img.shields.io/static/v1?label=Google&message=Survey&color=blue)](https://forms.gle/2Hex2ynbHWHhi1KY7) [![Chat in Telegram](https://img.shields.io/static/v1?label=Telegram&message=Chat&color=blue)](https://t.me/InMoTelegramBotAPI) |
|
|
|
|
|:---:|
|
|
|
|
| [![Create bot](https://img.shields.io/static/v1?label=Github&message=Template&color=blue)](https://github.com/InsanusMokrassar/TelegramBotAPI-bot_template/generate) [![Examples](https://img.shields.io/static/v1?label=Github&message=Examples&color=blue)](https://github.com/InsanusMokrassar/TelegramBotAPI-examples/) [![KDocs](https://img.shields.io/static/v1?label=Dokka&message=KDocs&color=blue)](https://tgbotapi.inmo.dev/index.html) [![Mini tutorial](https://img.shields.io/static/v1?label=Bookstack&message=Tutorial&color=blue)](https://bookstack.inmo.dev/books/telegrambotapi/chapter/introduction-tutorial) |
|
|
|
|
| [![Supported version](https://img.shields.io/badge/Telegram%20Bot%20API-5.4-blue)](https://core.telegram.org/bots/api-changelog#november-5-2021) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi) |
|
2021-08-22 04:58:32 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
Hello! This is a set of libraries for working with Telegram Bot API.
|
2020-11-16 15:59:25 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
## Examples
|
2020-04-17 09:43:43 +00:00
|
|
|
|
2021-02-12 19:38:31 +00: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-12 19:42:35 +00:00
|
|
|
* Replace `tgbotapi_version` with exact version (see last one in the table above) or put variable with this name in project
|
2021-02-12 19:38:31 +00:00
|
|
|
* Alternative variant for maven [here](https://github.com/InsanusMokrassar/TelegramBotAPI/wiki/Including-in-your-project#telegrambotapi)
|
|
|
|
|
2021-02-13 04:09:33 +00: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-12 19:38:31 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
### Most common example
|
2020-04-17 09:43:43 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
```kotlin
|
|
|
|
suspend fun main() {
|
|
|
|
val bot = telegramBot(TOKEN)
|
2020-04-17 09:43:43 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
bot.buildBehaviour {
|
|
|
|
println(getMe())
|
|
|
|
|
|
|
|
onCommand("start") {
|
|
|
|
reply(it, "Hi:)")
|
2020-04-17 09:43:43 +00:00
|
|
|
}
|
2021-02-11 06:06:34 +00:00
|
|
|
}.join()
|
2020-04-17 09:43:43 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-02-11 06:06:34 +00: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 08:07:23 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
### Handling only last messages
|
2020-04-08 09:28:03 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
```kotlin
|
|
|
|
suspend fun main() {
|
|
|
|
val bot = telegramBot(TOKEN)
|
2020-05-14 07:44:18 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
val flowsUpdatesFilter = FlowsUpdatesFilter()
|
|
|
|
bot.buildBehaviour(flowUpdatesFilter = flowsUpdatesFilter) {
|
|
|
|
println(getMe())
|
|
|
|
|
|
|
|
onCommand("start") {
|
|
|
|
reply(it, "Hi:)")
|
|
|
|
}
|
2020-05-14 07:44:18 +00:00
|
|
|
|
2021-02-11 07:37:24 +00:00
|
|
|
retrieveAccumulatedUpdates(this).join()
|
2021-02-11 07:35:06 +00:00
|
|
|
}
|
2021-02-11 06:06:34 +00:00
|
|
|
}
|
2020-05-14 07:44:18 +00:00
|
|
|
```
|
|
|
|
|
2021-02-11 06:06:34 +00: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 07:44:18 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
### Build a little bit more complex behaviour
|
2020-05-14 07:44:18 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
```kotlin
|
|
|
|
suspend fun main() {
|
|
|
|
val bot = telegramBot(TOKEN)
|
2020-05-14 07:44:18 +00:00
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
bot.buildBehaviour {
|
|
|
|
println(getMe())
|
2020-05-14 07:44:18 +00:00
|
|
|
|
2021-02-11 06:06:34 +00: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 07:38:55 +00:00
|
|
|
replyMarkup = nameReplyMarkup
|
2021-02-11 06:06:34 +00: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 07:44:18 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-02-11 06:06:34 +00:00
|
|
|
### More examples
|
|
|
|
|
|
|
|
You may find examples in [this project](https://github.com/InsanusMokrassar/TelegramBotAPI-examples). Besides, you are
|
|
|
|
always welcome in our [wiki](https://github.com/InsanusMokrassar/TelegramBotAPI/wiki/About-this-project) and
|
|
|
|
[chat](https://t.me/InMoTelegramBotAPIChat).
|