1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2026-06-30 15:15:11 +00:00
InsanusMokrassar 23578d25ef Add rich block markdown and html source builders
Add List<RichBlock>.toRichMarkdown() / toRichHtml() (and per-block RichBlock
.markdown / .html plus RichTextInfo.markdown / .html convenience) that render the
Rich Markdown style and Rich HTML style source strings for a whole rich message,
ready to feed into InputRichMessageMarkdown / InputRichMessageHTML.

All 21 block types are covered, including lists (bullet/ordered/task), tables,
block/pull quotations, details, collages, slideshows, maps and media. Media
blocks use the Telegram file_id as the source (documented), since Telegram only
accepts HTTP(S) URLs for rich media and the parsed model carries no public URL.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 17:21:19 +06:00
2026-05-19 23:25:15 +06:00
2024-12-26 09:05:11 +06:00
2026-04-19 12:48:40 +06:00
2022-01-11 21:22:10 +06:00
2023-02-24 15:30:36 +06:00
2026-06-29 23:37:14 +06:00
2025-11-23 16:55:54 +06:00
2020-01-06 22:44:23 +06:00
2026-05-19 23:25:15 +06:00
2020-10-22 19:19:55 +06:00
2020-10-02 13:10:01 +06:00
2025-05-31 17:04:51 +06:00
2026-06-19 16:27:54 +06:00
2018-12-26 16:21:52 +08:00
2018-12-26 16:21:52 +08:00
2020-02-19 22:29:53 +06:00
2020-08-12 20:21:10 +00:00
2023-03-04 21:18:50 +06:00
2021-10-18 15:20:25 +06:00

TelegramBotAPI Maven Central Version 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.

Bot API Server Notice

Under the hood, default bots realizations will try to use links (PathedFile#filePath) as files each time you are trying to download file from telegram in any way - via saving to file, use stream or download as byte array. To let bot correctly download files from bot api server, you must:

  • Run bot api server locally to proxy requests for files to the server where bot api server has been hosted
    • In case of local bot api server (shared one host machine) you must ensure that access to bot api server has been granted for your bot. For example, aiogram/telegram-bot-api image use 101 UID/GID in linux for user and group as owners. So, your bot must run under user included in 101 group (like systemd-journal) or be 101 UID user (like systemd-resolve)
  • OR Use some reverse proxy (like nginx). It will allow you to broadcast your bots files without linux rights problems
    • Set TelegramAPIUrlsKeeper#fileLinkUrlMapper to map urls to let bot execute requests to your nginx proxy
Languages
Kotlin 100%