|
|
|
|
@@ -1,10 +1,13 @@
|
|
|
|
|
import dev.inmo.micro_utils.coroutines.awaitFirst
|
|
|
|
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
|
|
|
|
import dev.inmo.micro_utils.fsm.common.State
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.send
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitAnyContentMessage
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitCommandMessage
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndFSMAndStartLongPolling
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.command
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.containsCommand
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgs
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.extensions.sameThread
|
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
|
|
|
|
|
@@ -13,10 +16,12 @@ import dev.inmo.tgbotapi.types.IdChatIdentifier
|
|
|
|
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.TextContent
|
|
|
|
|
import dev.inmo.tgbotapi.utils.botCommand
|
|
|
|
|
import dev.inmo.tgbotapi.utils.firstOf
|
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.flow.filter
|
|
|
|
|
import kotlinx.coroutines.flow.first
|
|
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
|
|
|
|
|
|
sealed interface BotState : State
|
|
|
|
|
data class ExpectContentOrStopState(override val context: IdChatIdentifier, val sourceMessage: CommonMessage<TextContent>) : BotState
|
|
|
|
|
@@ -48,19 +53,29 @@ suspend fun main(args: Array<String>) {
|
|
|
|
|
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val contentMessage = waitAnyContentMessage().filter { message ->
|
|
|
|
|
message.sameThread(it.sourceMessage)
|
|
|
|
|
}.first()
|
|
|
|
|
val contentMessage = firstOf(
|
|
|
|
|
{
|
|
|
|
|
waitCommandMessage("stop").filter { message ->
|
|
|
|
|
message.sameThread(it.sourceMessage)
|
|
|
|
|
}.first()
|
|
|
|
|
null
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
waitAnyContentMessage().filter { message ->
|
|
|
|
|
message.sameThread(it.sourceMessage)
|
|
|
|
|
}.filter {
|
|
|
|
|
containsCommand(
|
|
|
|
|
"stop",
|
|
|
|
|
it.withContentOrNull<TextContent>() ?.content ?.textSources ?: return@filter false
|
|
|
|
|
) == false
|
|
|
|
|
}.first()
|
|
|
|
|
}
|
|
|
|
|
) ?: return@strictlyOn StopState(it.context)
|
|
|
|
|
|
|
|
|
|
val content = contentMessage.content
|
|
|
|
|
|
|
|
|
|
when {
|
|
|
|
|
content is TextContent && content.text == "/stop"
|
|
|
|
|
|| content is TextContent && content.parseCommandsWithArgs().keys.contains("stop") -> StopState(it.context)
|
|
|
|
|
else -> {
|
|
|
|
|
execute(content.createResend(it.context))
|
|
|
|
|
it
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
execute(content.createResend(it.context))
|
|
|
|
|
it
|
|
|
|
|
}
|
|
|
|
|
strictlyOn<StopState> {
|
|
|
|
|
send(it.context) { +"You have stopped sending of content" }
|
|
|
|
|
|