TelegramBotAPI-examples/FSMBot/src/main/kotlin/SimpleFSMBot.kt

51 lines
1.9 KiB
Kotlin
Raw Normal View History

2021-06-30 07:12:15 +00:00
import dev.inmo.micro_utils.fsm.common.State
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
2021-09-21 17:22:13 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
2021-06-30 07:12:15 +00:00
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
import dev.inmo.tgbotapi.extensions.utils.formatting.*
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.TextContent
2021-09-21 17:22:13 +00:00
import kotlinx.coroutines.*
2021-06-30 07:12:15 +00:00
2021-09-21 17:22:13 +00:00
sealed interface BotState : State
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
data class StopState(override val context: ChatId) : BotState
2021-06-30 07:12:15 +00:00
suspend fun main(args: Array<String>) {
val botToken = args.first()
2021-11-11 06:34:52 +00:00
telegramBotWithBehaviourAndFSMAndStartLongPolling<BotState>(botToken, CoroutineScope(Dispatchers.IO)) {
2021-10-18 11:14:40 +00:00
strictlyOn<ExpectContentOrStopState> {
sendMessage(
it.context,
buildEntities {
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
}
)
2022-04-16 18:26:04 +00:00
val contentMessage = waitContentMessage().first()
val content = contentMessage.content
2021-10-18 11:14:40 +00:00
when {
content is TextContent && content.parseCommandsWithParams().keys.contains("stop") -> StopState(it.context)
else -> {
execute(content.createResend(it.context))
it
}
2021-06-30 07:12:15 +00:00
}
2021-10-18 11:14:40 +00:00
}
strictlyOn<StopState> {
sendMessage(it.context, "You have stopped sending of content")
2021-06-30 07:12:15 +00:00
2021-10-18 11:14:40 +00:00
null
2021-06-30 07:12:15 +00:00
}
command("start") {
2021-10-18 11:14:40 +00:00
startChain(ExpectContentOrStopState(it.chat.id, it))
2021-06-30 07:12:15 +00:00
}
}.second.join()
}