Git Product home page Git Product logo

tg-telegram-bot2's Introduction

Telegram bot api library logo

Telegram Bot

Maven Central Supported version
KDocs codecov
Chat in Telegram Chat in Telegram

Telegram Bot Api wrapper with a user-friendly interface.

Installation

Add the ksp plugin and library to the dependencies.

build.gradle.kts example:

plugins {
    // ...
    id("com.google.devtools.ksp") version "2.0.0-1.0.23"
}

dependencies {
    // ...
    implementation("eu.vendeli:telegram-bot:6.3.1")
    ksp("eu.vendeli:ksp:6.3.1")
}

Samples

Usage

suspend fun main() {
    val bot = TelegramBot("BOT_TOKEN")

    bot.handleUpdates()
    // start long-polling listener
}

@CommandHandler(["/start"])
suspend fun start(user: User, bot: TelegramBot) {
    message { "Hello, what's your name?" }.send(user, bot)
    bot.inputListener[user] = "conversation"
}

@InputHandler(["conversation"], guard = UserPresentGuard::class)
suspend fun startConversation(update: ProcessedUpdate, user: User, bot: TelegramBot) {
    message { "Nice to meet you, ${update.text}" }.send(user, bot)
    message { "What is your favorite food?" }.send(user, bot)
    bot.inputListener.set(user) { "conversation-2step" } // another way to set input
}

@CommonHandler.Regex("blue colo?r")
suspend fun color(user: User, bot: TelegramBot) {
    message { "Oh you also like blue color?" }.send(user, bot)
}
//..

a little more detailed about handlers you can see in handlers article.

It is also possible to process updates functionally:

fun main() = runBlocking {
    val bot = TelegramBot("BOT_TOKEN")

    bot.handleUpdates { update ->
        onCommand("/start") {
            message { "Hello, what's your name?" }.send(user, bot)
            bot.inputListener[user] = "conversation"
        }
        inputChain("conversation") {
            message { "Nice to meet you, ${message.text}" }.send(update.getUser(), bot)
            message { "What is your favorite food?" }.send(update.getUser(), bot)
        }.breakIf({ message.text == "peanut butter" }) { // chain break condition
            message { "Oh, too bad, I'm allergic to it." }.send(update.getUser(), bot)
            // action that will be applied when match
        }.andThen {
            // next input point if break condition doesn't match
        }
    }
}

Configuration

The library has very flexible customization options,
and the ability to configure from the environment variables out of the box is also provided.

You can read more in a Bot configuration article.

Processing responses

All requests are async but to operate with response and have more control over flow you need use sendAsync() instead of send() method, which returns Response:

message { "test" }.sendAsync(user, bot).onFailure {
    println("code: ${it.errorCode} description: ${it.description}")
}

Any sendAsync method returns a Response on which you can also use methods getOrNull() , isSuccess() , onFailure()

Additional resources

There is a wiki section that have helpful information.

Questions

You're always welcome in our chat, feel free to ask.

Acknowledgements

A big thank you to everyone who has contributed to this project. Your support and feedback are invaluable.

If you find this library useful, please consider giving it a star. Your support helps us continue to improve and maintain this project.

tg-telegram-bot2's People

Contributors

vendelieu avatar renovate[bot] avatar nmago avatar adi-itgg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.