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

93 lines
3.4 KiB
Kotlin
Raw Permalink Normal View History

2022-05-10 20:47:37 +00:00
import dev.inmo.micro_utils.coroutines.AccumulatorFlow
2024-04-18 14:06:59 +00:00
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
2021-06-30 07:12:15 +00:00
import dev.inmo.micro_utils.fsm.common.State
2022-06-26 07:03:52 +00:00
import dev.inmo.tgbotapi.extensions.api.send.send
2021-06-30 07:12:15 +00:00
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.*
2024-03-18 07:46:15 +00:00
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgs
2022-11-08 11:49:41 +00:00
import dev.inmo.tgbotapi.extensions.utils.extensions.sameThread
2021-06-30 07:12:15 +00:00
import dev.inmo.tgbotapi.extensions.utils.formatting.*
2024-04-18 14:06:59 +00:00
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
2022-11-10 18:35:16 +00:00
import dev.inmo.tgbotapi.types.IdChatIdentifier
2022-11-08 11:49:41 +00:00
import dev.inmo.tgbotapi.types.MessageThreadId
2021-06-30 07:12:15 +00:00
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.TextContent
2022-09-19 08:37:01 +00:00
import dev.inmo.tgbotapi.utils.botCommand
2022-11-08 11:49:41 +00:00
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
2021-09-21 17:22:13 +00:00
import kotlinx.coroutines.*
2022-11-08 11:49:41 +00:00
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
2021-06-30 07:12:15 +00:00
2021-09-21 17:22:13 +00:00
sealed interface BotState : State
2022-11-10 18:35:16 +00:00
data class ExpectContentOrStopState(override val context: IdChatIdentifier, val sourceMessage: CommonMessage<TextContent>) : BotState
data class StopState(override val context: IdChatIdentifier) : BotState
2021-06-30 07:12:15 +00:00
suspend fun main(args: Array<String>) {
val botToken = args.first()
telegramBotWithBehaviourAndFSMAndStartLongPolling<BotState>(
botToken,
CoroutineScope(Dispatchers.IO),
onStateHandlingErrorHandler = { state, e ->
when (state) {
is ExpectContentOrStopState -> {
println("Thrown error on ExpectContentOrStopState")
}
is StopState -> {
println("Thrown error on StopState")
}
}
e.printStackTrace()
state
}
) {
2021-10-18 11:14:40 +00:00
strictlyOn<ExpectContentOrStopState> {
2022-09-19 08:58:34 +00:00
send(
2022-11-10 18:35:16 +00:00
it.context,
2022-09-19 08:58:34 +00:00
) {
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
}
2021-10-18 11:14:40 +00:00
val contentMessage = waitAnyContentMessage().filter { message ->
2022-11-08 11:49:41 +00:00
message.sameThread(it.sourceMessage)
}.first()
2022-04-16 18:26:04 +00:00
val content = contentMessage.content
2021-10-18 11:14:40 +00:00
when {
2024-04-18 14:06:59 +00:00
content is TextContent && content.text == "/stop"
|| content is TextContent && content.parseCommandsWithArgs().keys.contains("stop") -> StopState(it.context)
2021-10-18 11:14:40 +00:00
else -> {
2022-11-10 18:35:16 +00:00
execute(content.createResend(it.context))
2021-10-18 11:14:40 +00:00
it
}
2021-06-30 07:12:15 +00:00
}
2021-10-18 11:14:40 +00:00
}
strictlyOn<StopState> {
2022-11-10 18:35:16 +00:00
send(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
}
2022-11-08 11:49:41 +00:00
command(
"start"
) {
2022-11-10 18:35:16 +00:00
startChain(ExpectContentOrStopState(it.chat.id, it))
2021-06-30 07:12:15 +00:00
}
2024-04-18 14:06:59 +00:00
onContentMessage(
{
it.content.textContentOrNull() ?.text == "/start"
}
) {
startChain(ExpectContentOrStopState(it.chat.id, it.withContentOrNull() ?: return@onContentMessage))
}
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
println(it)
}
2021-06-30 07:12:15 +00:00
}.second.join()
}