mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-09-07 09:09:31 +00:00
@@ -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" }
|
||||
|
@@ -6,8 +6,8 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
||||
|
||||
|
||||
kotlin_version=2.2.0
|
||||
telegram_bot_api_version=27.1.2
|
||||
micro_utils_version=0.26.1
|
||||
telegram_bot_api_version=28.0.0
|
||||
micro_utils_version=0.26.2
|
||||
serialization_version=1.9.0
|
||||
ktor_version=3.2.3
|
||||
compose_version=1.8.2
|
||||
|
Reference in New Issue
Block a user