1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-23 09:07:12 +00:00
Type-safe library for work with Telegram Bot API
Go to file
2021-03-26 07:03:33 +06:00
.github Update kdocs.yml 2021-03-17 21:14:05 +06:00
badges add link to bot template 2020-11-08 18:58:52 +06:00
docs update dokka 2021-03-16 23:01:26 +06:00
gradle/wrapper Update gradle-wrapper.properties 2021-02-23 09:53:45 +06:00
resources fixes in readme 2021-01-08 16:37:03 +06:00
tgbotapi Update mpp_publish_template.kpsb 2021-03-03 01:39:18 +06:00
tgbotapi.core update condition for ReplyMessageNotFoundException 2021-03-26 07:03:33 +06:00
tgbotapi.extensions.api optimize imports 2021-03-11 21:39:34 +06:00
tgbotapi.extensions.behaviour_builder fix build 2021-03-12 15:03:11 +06:00
tgbotapi.extensions.utils remove caption based messages extension for parsing of commands 2021-03-24 13:56:51 +06:00
_config.yml Set theme jekyll-theme-cayman 2020-01-06 22:44:23 +06:00
.gitignore add publication of github release via changelog 2020-09-22 18:04:10 +06:00
.travis.yml update travis config 2020-10-22 19:19:55 +06:00
build.gradle exprimentally update publishing scripts 2020-11-18 17:18:51 +06:00
changelog_info_retriever update changelog parser 2020-10-04 17:43:25 +06:00
CHANGELOG.md add parseCommandsWithParams for text inputs 2021-03-24 13:43:32 +06:00
CONTRIBUTING.md Create CONTRIBUTING.md 2020-10-02 13:10:01 +06:00
gradle.properties update kotlin version 2021-03-24 13:14:09 +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 Update README.md 2021-02-13 10:09:33 +06:00
renovate.json Add renovate.json 2020-08-12 20:21:10 +00:00
settings.gradle fix dokka config 2021-03-16 22:33:53 +06:00
TelegramBotAPI.minder fixes in readme 2021-01-08 16:37:03 +06:00

Participate in our common survey ☺

TelegramBotAPI

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

Common info Awesome Kotlin Badge Build Status Small survey
Useful links Chat in Telegram Create bot KDocs Examples, Mini tutorial
TelegramBotAPI Core status Maven Central
TelegramBotAPI API Extensions status Maven Central
TelegramBotAPI Util Extensions status Maven Central
TelegramBotAPI Behaviour Builder Extensions status Maven Central
TelegramBotAPI All status Maven Central

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.buildBehaviour {
    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.buildBehaviour {
    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.