Giter VIP home page Giter VIP logo

kotlin-telegram-framework's Introduction

Kotlin telegram bot framework

Kotlin telegram framework for fast developing stateful bots.

Framework is in active developing currently.

I will be grateful for any helpful pull requests

Examples

  1. @MemeBuilderBot / Source code
  2. @MemeGeneBot / Source code
  3. @MarksManageBot / Source code

Usage

You can see samples at https://github.com/asundukov/kotlin-telegram-bot/tree/master/samples

As telegram bot api library

fun main(args: Array<String>) {
    val api = TelegramApi()
    val updates = api.getUpdates(token, offset, limit, timeout)
    api.sendMessage(token, data)
}

Text bot

Sources

fun main(args: Array<String>) {
    val botToken = args[0]
    BotRunner().run(TextBot(botToken))
}

class TextBot(
        private val token: String
): SimpleTextBot() {
    private val counter = AtomicInteger(0)

    override fun handleText(text: String, from: TgUser, chat: TgChat): String {
        return "hello!\n this is message number " + counter.incrementAndGet().toString()
    }

    override fun getToken(): String {
        return token
    }
}

Stateful bot

Sample source

val bot = StatefulBotExample(botToken, StartBlock())
BotRunner().run(bot)
class StatefulBotExample(
        private val token: String,
        currentBlock: BotBlock
): StatefulBot(currentBlock) {
    override fun getToken(): String {
        return token
    }
}
class StartBlock(): BotTextBlock {
    override fun getAnswer(): ChatAnswer {
        return ChatAnswer.text("This is start block")
    }

    override fun handleText(message: TextMessage): BotBlock {
        val text = message.text
        //handle message
        return SecondBlock()
    }
}
class StartBlock(): BotTextBlock {
    override fun getAnswer(): ChatAnswer {
        return ChatAnswer.text("This is second block")
    }

    override fun handleText(message: TextMessage): BotBlock {
        val text = message.text
        //handle message
        return StartBlock()
    }
}

Bot commands + stateful blocks

class CommandsStatefulBotExample(
        private val token: String,
        currentBlock: BotBlock,
        commands: List<Command>
): CommandsStatefulBot(currentBlock, commands) {
    override fun getToken(): String {
        return token
    }
}
class StartCommand: Command {
    override fun handleCommand(query: String, message: RawMessage): BotBlock {
        return StartBlock
    }

    override fun getCommand(): String {
        return "/start"
    }

    override fun getCommandDescription(): String {
        return "Restart bot"
    }

    override fun isSystemCommand(): Boolean {
        return true
    }
}

Maven dependency

<dependency>
    <groupId>com.github.asundukov.kotlin-telegram-framework</groupId>
    <artifactId>kotlin-telegram-framework</artifactId>
    <version>1.0.1-beta-5</version>
</dependency>

kotlin-telegram-framework's People

Contributors

asundukov avatar

Stargazers

 avatar

kotlin-telegram-framework's Issues

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.