1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-24 19:18:44 +00:00
Type-safe library for work with Telegram Bot API
Go to file
2023-02-06 11:36:00 +06:00
.github fix in gitea publishing name 2022-11-18 22:22:03 +06:00
docs fix dokka build 2022-08-05 22:03:10 +06:00
gradle Update libs.versions.toml 2023-02-05 18:34:05 +06:00
readmes add exceptions handling readme 2022-01-11 21:22:10 +06:00
resources Add files via upload 2022-07-18 13:43:06 +06:00
tgbotapi complete rewriting on libs.versions.toml 2022-06-29 14:23:45 +06:00
tgbotapi.api add support for independent chat permissions 2023-02-06 00:21:46 +06:00
tgbotapi.behaviour_builder support of user shared/chat shared 2023-02-05 20:46:25 +06:00
tgbotapi.behaviour_builder.fsm Update BehaviourContextWithFSM.kt 2022-12-31 15:17:21 +06:00
tgbotapi.core improve copying functions in chat permissions 2023-02-06 11:36:00 +06:00
tgbotapi.ksp fixes 2022-08-06 00:39:46 +06:00
tgbotapi.utils rights copying hotfix 2023-02-06 11:29:07 +06:00
tgbotapi.webapps add support of Bot API 6.5 web apps 2022-12-31 15:23:14 +06:00
_config.yml Set theme jekyll-theme-cayman 2020-01-06 22:44:23 +06:00
.gitignore update gitignore and dokka version 2021-12-30 11:55:12 +06:00
.travis.yml update travis config 2020-10-22 19:19:55 +06:00
build.gradle temporal include of internal microutils build 2022-12-08 09:35:22 +06:00
changelog_info_retriever update changelog parser 2020-10-04 17:43:25 +06:00
CHANGELOG.md Merge branch '5.1.0' into feature/replace_can_send_media_messages_field 2023-02-06 00:24:19 +06:00
CONTRIBUTING.md Create CONTRIBUTING.md 2020-10-02 13:10:01 +06:00
extensions.gradle start to use more unified way of scripts organization 2022-06-29 13:47:21 +06:00
gradle.properties start 5.1.0 2023-02-05 17:41:07 +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
mppJsProject.gradle start to use more unified way of scripts organization 2022-06-29 13:47:21 +06:00
mppProjectWithSerialization.gradle complete rewriting on libs.versions.toml 2022-06-29 14:23:45 +06:00
publish.gradle add gitea publication 2022-11-17 11:26:05 +06:00
publish.kpsb add gitea publication 2022-11-17 11:26:05 +06:00
README.md Update README.md 2023-02-06 00:59:33 +06:00
renovate.json Add renovate.json 2020-08-12 20:21:10 +00:00
settings.gradle fixes 2022-08-06 00:39:46 +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

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 bookstack and chat.