vendelieu / telegram-bot

Telegram Bot API wrapper with handy Kotlin DSL.
https://vendelieu.github.io/telegram-bot/
Apache License 2.0
191 stars 15 forks source link

Idea for a new way to handle conversations #42

Closed Kotov584 closed 10 months ago

Kotov584 commented 11 months ago

Is your feature request related to a problem? Please describe. I'm always frustrated when I have to define conversations using input handlers. When you are using input handlers, it's pretty hard to write and manage complex conversations.

Describe the solution you'd like Instead of input handlers, we could use classes for defining conversations. So we are able to write more well-architected code, even when it is complex.

Example:

package com.example.blank.conversation

import eu.vendeli.tgbot.TelegramBot
import eu.vendeli.tgbot.api.message
import eu.vendeli.tgbot.types.User
import eu.vendeli.tgbot.types.internal.ProcessedUpdate

sealed class GreetingsConversation {
    object Start : GreetingsConversation() {
        fun handle(update: ProcessedUpdate, user: User, bot: TelegramBot) {
            message("Hello, let's move to second step!").send(user, bot)
            //set secondStep state
           //maybe something like 
           setData(user, "hello" to update.text)
           setState(user, GreetingsConversation::start)
        }
    }

    object SecondStep : GreetingsConversation() {
        fun handle(update: ProcessedUpdate, user: User, bot: TelegramBot) {
            // retrieve data from start step and proceed
            message("Nice job! You completed this.").send(user, bot)
            deleteState(user, GreetingsConversation)
        }
    }
}

Additional context Not sure, if we can do this way. We discussed about it in Telegram group.

Kotov584 commented 11 months ago

or also this

package com.example.blank.conversation

import eu.vendeli.tgbot.TelegramBot
import eu.vendeli.tgbot.api.message
import eu.vendeli.tgbot.types.User

class GreetingsConversation {
    fun start(user: User, bot: TelegramBot) {
        message("Hello, let's move to second step!").send(user, bot)
    }

    fun secondStep(user: User, bot: TelegramBot) {
        message("Nice job! You completed this.").send(user, bot)
    }
}
vendelieu commented 11 months ago

Experimental implementation is released in #61

vendelieu commented 10 months ago

implemented in #75