Skip to content

Telegram framework for fast developing stateful telegram bots written on Kotlin. Includes Kotlin-friendly Telegram API Client.

Notifications You must be signed in to change notification settings

asundukov/kotlin-telegram-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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>

About

Telegram framework for fast developing stateful telegram bots written on Kotlin. Includes Kotlin-friendly Telegram API Client.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages