Type-safe library for work with Telegram Bot API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
InsanusMokrassar c9f3d99cd7
Merge pull request #708 from InsanusMokrassar/5.0.1
1 week ago
.github fix in gitea publishing name 2 months ago
docs fix dokka build 6 months ago
gradle update microutils and fill changelog 1 week ago
readmes add exceptions handling readme 1 year ago
resources Add files via upload 6 months ago
tgbotapi complete rewriting on libs.versions.toml 7 months ago
tgbotapi.api fixes 4 weeks ago
tgbotapi.behaviour_builder add support of forum_topic_edited, general_forum_topic_hidden, general_forum_topic_unhidden, and write_access_allowed 4 weeks ago
tgbotapi.behaviour_builder.fsm Update BehaviourContextWithFSM.kt 4 weeks ago
tgbotapi.core fix of return type in SendMediaGroup and fill changelog 2 weeks ago
tgbotapi.ksp fixes 6 months ago
tgbotapi.utils add opportunity in long polling to automatically delete webhook 4 weeks ago
tgbotapi.webapps add support of Bot API 6.5 web apps 4 weeks ago
.gitignore update gitignore and dokka version 1 year ago
.travis.yml update travis config 2 years ago
CHANGELOG.md update microutils and fill changelog 1 week ago
CONTRIBUTING.md Create CONTRIBUTING.md 2 years ago
LICENSE Update LICENSE 3 years ago
README.md Update README.md 4 weeks ago
TelegramBotAPI.drawio packages update 1 year ago
_config.yml Set theme jekyll-theme-cayman 3 years ago
build.gradle temporal include of internal microutils build 2 months ago
changelog_info_retriever update changelog parser 2 years ago
extensions.gradle start to use more unified way of scripts organization 7 months ago
gradle.properties start 5.0.1 2 weeks ago
gradlew init 4 years ago
gradlew.bat init 4 years ago
mppJsProject.gradle start to use more unified way of scripts organization 7 months ago
mppProjectWithSerialization.gradle complete rewriting on libs.versions.toml 7 months ago
publish.gradle add gitea publication 2 months ago
publish.kpsb add gitea publication 2 months ago
renovate.json Add renovate.json 2 years ago
settings.gradle fixes 6 months ago

README.md

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.