Type-safe library for work with Telegram Bot API
Go to file
InsanusMokrassar 02cedc6626 start 2.1.2 2022-06-29 13:27:35 +06:00
.github update github building yml 2022-03-24 16:49:08 +06:00
docs fix dokka 2022-04-23 12:33:34 +06:00
gradle/wrapper update gradle 2022-04-06 11:45:08 +06:00
readmes add exceptions handling readme 2022-01-11 21:22:10 +06:00
resources packages update 2021-10-18 15:20:25 +06:00
tgbotapi publication scripts update 2022-03-22 16:26:18 +06:00
tgbotapi.api replace all the edits into one file 2022-06-26 13:18:22 +06:00
tgbotapi.behaviour_builder fixes for build 2022-05-22 01:46:11 +06:00
tgbotapi.behaviour_builder.fsm remove deprecations 2022-05-22 01:40:03 +06:00
tgbotapi.core rename PremiumChat -> PossiblyPremiumChat 2022-06-21 23:06:36 +06:00
tgbotapi.utils rename PremiumChat -> PossiblyPremiumChat 2022-06-21 23:06:36 +06:00
tgbotapi.webapps update webapps support 2022-06-21 16:35:48 +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
CHANGELOG.md start 2.1.2 2022-06-29 13:27:35 +06:00
CONTRIBUTING.md Create CONTRIBUTING.md 2020-10-02 13:10:01 +06:00
LICENSE Update LICENSE 2020-02-19 22:29:53 +06:00
README.md add mention about 6.1 telegram bot api support 2022-06-21 18:58:01 +06:00
TelegramBotAPI.drawio packages update 2021-10-18 15:20:25 +06:00
_config.yml Set theme jekyll-theme-cayman 2020-01-06 22:44:23 +06:00
build.gradle BehaviourBuilder FSM extension 2021-10-13 14:22:01 +06:00
changelog_info_retriever update changelog parser 2020-10-04 17:43:25 +06:00
gradle.properties start 2.1.2 2022-06-29 13:27:35 +06:00
gradlew init 2018-12-26 16:21:52 +08:00
gradlew.bat init 2018-12-26 16:21:52 +08:00
publish.gradle publication scripts update 2022-03-22 16:26:18 +06:00
publish.kpsb publication scripts update 2022-03-22 16:26:18 +06:00
renovate.json Add renovate.json 2020-08-12 20:21:10 +00:00
settings.gradle webapps 2022-04-23 12:32:39 +06:00

README.md

TelegramBotAPI Maven Central Supported version

Awesome Kotlin Badge Build Status Small survey Chat in Telegram
Create bot Examples KDocs Mini tutorial

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