mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-23 00:57:13 +00:00
Type-safe library for work with Telegram Bot API
dev-inmo-tgbotapihacktoberfesthacktoberfest-2021hacktoberfest2020hacktoberfest2021kotlinkotlin-jskotlin-jvmkotlin-librarykotlin-multiplatformtelegramtelegram-bot-apitelegrambotapitgbotapi
.github | ||
badges | ||
docs | ||
gradle/wrapper | ||
resources | ||
tgbotapi | ||
tgbotapi.core | ||
tgbotapi.extensions.api | ||
tgbotapi.extensions.behaviour_builder | ||
tgbotapi.extensions.behaviour_builder.fsm | ||
tgbotapi.extensions.utils | ||
_config.yml | ||
.gitignore | ||
.travis.yml | ||
build.gradle | ||
changelog_info_retriever | ||
CHANGELOG.md | ||
CONTRIBUTING.md | ||
gradle.properties | ||
gradlew | ||
gradlew.bat | ||
LICENSE | ||
README.md | ||
renovate.json | ||
settings.gradle | ||
TelegramBotAPI.drawio |
Participate in our common survey ☺
TelegramBotAPI
Table of contents generated with markdown-toc
Hello! This is a set of libraries for working with Telegram Bot API.
Common info | Small survey |
---|---|
Useful links | Examples, Mini tutorial |
TelegramBotAPI Core status | |
TelegramBotAPI API Extensions status | |
TelegramBotAPI Util Extensions status | |
TelegramBotAPI Behaviour Builder Extensions status | |
TelegramBotAPI All status |
Examples
There are several things you need to do to launch examples below:
- Add
mavenCentral()
to your project repositories - Add dependency
implementation "dev.inmo:tgbotapi:$tgbotapi_version"
- Replace
tgbotapi_version
with exact version (see last one in the table above) or put variable with this name in project - Alternative variant for maven here
- Replace
More including instructions available here. Other configuration examples:
Most common example
suspend fun main() {
val bot = telegramBot(TOKEN)
bot.buildBehaviour {
println(getMe())
onCommand("start") {
reply(it, "Hi:)")
}
}.join()
}
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
Handling only last messages
suspend fun main() {
val bot = telegramBot(TOKEN)
val flowsUpdatesFilter = FlowsUpdatesFilter()
bot.buildBehaviour(flowUpdatesFilter = flowsUpdatesFilter) {
println(getMe())
onCommand("start") {
reply(it, "Hi:)")
}
retrieveAccumulatedUpdates(this).join()
}
}
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)
Build a little bit more complex behaviour
suspend fun main() {
val bot = telegramBot(TOKEN)
bot.buildBehaviour {
println(getMe())
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\"",
replyMarkup = nameReplyMarkup
)
).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()
}
More examples
You may find examples in this project. Besides, you are always welcome in our wiki and chat.