elbekD/kt-telegram-bot


Telegram Bot Library for Kotlin language

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.elbekd:kt-telegram-bot:2.2.0'
	}
	dependencies {
		implementation("com.github.elbekd:kt-telegram-bot:2.2.0")
	}
	<dependency>
	    <groupId>com.github.elbekd</groupId>
	    <artifactId>kt-telegram-bot</artifactId>
	    <version>2.2.0</version>
	</dependency>

                            
    libraryDependencies += "com.github.elbekd" % "kt-telegram-bot" % "2.2.0"
        
        

                            
    :dependencies [[com.github.elbekd/kt-telegram-bot "2.2.0"]]
        
        

Readme


kt-telegram-bot

Convenient way to build Telegram bots using powerful Kotlin language. Support for Telegram Bot API 6.5. Method names are the same as in API.

Changelog

Version 2.2.0

  • Update Bot API to 6.5

Version 2.1.8

  • fix issue #56

Version 2.1.7

  • Update Bot API to 6.2
  • fix InputMedia(#54) and BotCommandScope (de)serialization

Changelog history

Getting started

Prerequisites

  • JDK 8 or higher
  • Kotlin 1.3 or higher
  • Gradle
  • IDE (optionally)

Installation

Gradle

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation "com.elbekD:kt-telegram-bot:$version"
}

Or Gradle Kotlin DSL

repositories {
    maven("https://jitpack.io")
}

dependencies {
    implementation("com.github.elbekD:kt-telegram-bot:${version}")
}

Maven

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>

<dependency>
    <groupId>com.github.elbekD</groupId>
    <artifactId>kt-telegram-bot</artifactId>
    <version>{version}</version>
</dependency>

Quick start

fun main(args: Array<String>) {
    val token = "<TOKEN>"
    val bot = Bot.createPolling(token)
    bot.onCommand("/start") { msg, _ ->
        bot.sendMessage(msg.chat.id, "Hello World!")
    }
    bot.start()
}

Chain

It is common case when you need to ask the user several questions sequentially and process user errors. Now you can create such chains easily. Sea the example below. Do not forget to call build() method at the end.

fun main() {
    val token = "<TOKEN>"
    val username = "<BOT USERNAME>"
    val bot = Bot.createPolling(username, token)

    bot.chain("/start") { msg -> bot.sendMessage(msg.chat.id.toChatId(), "Hi! What is your name?") }
        .then { msg -> bot.sendMessage(msg.chat.id.toChatId(), "Nice to meet you, ${msg.text}! Send something to me") }
        .then { msg -> bot.sendMessage(msg.chat.id.toChatId(), "Fine! See you soon") }
        .build()

    bot.chain(
        label = "location_chain",
        predicate = { msg -> msg.location != null },
        action = { msg ->
            bot.sendMessage(
                msg.chat.id.toChatId(),
                "Fine, you've sent me a location. Confirm the order?(yes|no)"
            )
        })
        .then("answer_choice") { msg ->
            when (msg.text) {
                "yes" -> bot.jumpToAndFire("order_taxi", msg)
                "no" -> bot.jumpToAndFire("cancel_ordering", msg)
                else -> {
                    bot.sendMessage(msg.chat.id.toChatId(), "Oops, I don't understand you. Just answer yes or no?")
                    bot.jumpTo("answer_choice", msg)
                }
            }
        }
        .then("order_taxi", isTerminal = true) { msg ->
            bot.sendMessage(msg.chat.id.toChatId(), "Fine! Taxi is coming")
        }
        .then("cancel_ordering", isTerminal = true) { msg ->
            bot.sendMessage(msg.chat.id.toChatId(), "Ok! See you next time")
        }
        .build()

    bot.start()
}

Deployment

Use ShadowJar plugin or any other way you like.

Examples

Bot methods

See details in source code.

License

MIT License