1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-01 22:40:07 +00:00
Type-safe library for work with Telegram Bot API
Go to file
2024-11-28 00:51:33 +00:00
.github change task used for ksp generation 2024-10-30 18:57:47 +06:00
docs small optimization of extensions.gradle 2024-08-30 00:51:03 +06:00
gradle Update ksp to v2.1.0-1.0.29 2024-11-28 00:51:33 +00:00
readmes add exceptions handling readme 2022-01-11 21:22:10 +06:00
resources Add files via upload 2023-02-24 15:30:36 +06:00
tgbotapi small optimization of extensions.gradle 2024-08-30 00:51:03 +06:00
tgbotapi.api jvmApiDump 2024-11-01 14:45:39 +06:00
tgbotapi.behaviour_builder add copy_text button and allowPaidBroadcast field in requests 2024-11-01 02:04:22 +06:00
tgbotapi.behaviour_builder.fsm add paid media payload support 2024-09-06 20:11:08 +06:00
tgbotapi.core jvmApiDump 2024-11-11 13:38:09 +06:00
tgbotapi.ksp add api validator 2024-03-16 10:17:33 +06:00
tgbotapi.utils jvmApiDump 2024-11-11 13:38:09 +06:00
tgbotapi.webapps potentially complete implementation of Bot API 7.10 2024-09-07 00:19:33 +06:00
_config.yml Set theme jekyll-theme-cayman 2020-01-06 22:44:23 +06:00
.gitignore start updating 2024-08-02 16:53:59 +06:00
.travis.yml update travis config 2020-10-22 19:19:55 +06:00
build.gradle change dependencies repositories resolution order 2024-06-26 01:13:52 +06:00
changelog_info_retriever update changelog parser 2020-10-04 17:43:25 +06:00
CHANGELOG.md fill changelog 2024-11-11 13:52:31 +06:00
CONTRIBUTING.md Create CONTRIBUTING.md 2020-10-02 13:10:01 +06:00
extensions.gradle small optimization of extensions.gradle 2024-08-30 00:51:03 +06:00
gradle.properties start 20.0.1 2024-11-11 12:36:30 +06:00
gradlew init 2018-12-26 16:21:52 +08:00
gradlew.bat init 2018-12-26 16:21:52 +08:00
LICENSE Update LICENSE 2020-02-19 22:29:53 +06:00
README.md add notes about support of Telegram Bots API 7.11 2024-11-01 23:42:22 +06:00
renovate.json Add renovate.json 2020-08-12 20:21:10 +00:00
settings.gradle update kdocs 2023-03-04 21:18:50 +06:00
TelegramBotAPI.drawio packages update 2021-10-18 15:20:25 +06:00

TelegramBotAPI Maven Central Supported version

Docs KDocs Mini tutorial
Useful repos Create bot Examples
Misc Awesome Kotlin Badge Small survey
Platforms JVM Js
Experimental Platforms Linux x64 MinGW x64

Hello! This is a set of libraries for working with Telegram Bot API.

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

More including instructions available here. Other configuration examples:

Most common example

suspend fun main() {
  val bot = telegramBot(TOKEN)

  bot.buildBehaviourWithLongPolling {
    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.buildBehaviourWithLongPolling {
    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 docs and chat.