improvements

This commit is contained in:
InsanusMokrassar 2023-04-06 15:52:44 +06:00
parent 847b285ce3
commit 0a5ffee808
3 changed files with 69 additions and 57 deletions

View File

@ -2,13 +2,14 @@ package dev.inmo.plaguposter.common
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.contentMessageOrNull
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.types.message.abstracts.* import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
val FirstSourceIsCommandsFilter = SimpleFilter<Message> { val FirstSourceIsCommandsFilter = SimpleFilter<Message> {
it is ContentMessage<*> && it.content.textContentOrNull() ?.textSources ?.firstOrNull { it.contentMessageOrNull() ?.withContentOrNull<TextContent>() ?.content ?.textSources ?.firstOrNull() is BotCommandTextSource
it is BotCommandTextSource
} != null
} }

View File

@ -18,21 +18,23 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextWithFSM
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.* import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.strictlyOn import dev.inmo.tgbotapi.extensions.behaviour_builder.strictlyOn
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.text
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
import dev.inmo.tgbotapi.extensions.utils.types.buttons.* import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.MessageContent
import dev.inmo.tgbotapi.utils.regular import dev.inmo.tgbotapi.utils.regular
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import org.koin.core.Koin import org.koin.core.Koin
@Serializable @Serializable
object Plugin : Plugin { object Plugin : Plugin {
@Serializable
data class Config(
val useInlineFinishingOpportunity: Boolean = true
)
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) { override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
val config = koin.get<ChatConfig>() val config = koin.get<ChatConfig>()
val postsRepo = koin.get<PostsRepo>() val postsRepo = koin.get<PostsRepo>()
@ -40,7 +42,8 @@ object Plugin : Plugin {
strictlyOn { state: RegistrationState.InProcess -> strictlyOn { state: RegistrationState.InProcess ->
val buttonUuid = "finish" val buttonUuid = "finish"
val messageToDelete = send( val suggestionMessageDeferred = async {
send(
state.context, state.context,
dev.inmo.tgbotapi.utils.buildEntities { dev.inmo.tgbotapi.utils.buildEntities {
if (state.messages.isNotEmpty()) { if (state.messages.isNotEmpty()) {
@ -60,42 +63,50 @@ object Plugin : Plugin {
null null
} }
) )
}
val newMessagesInfo = firstOf { firstOf {
add { add {
listOf( val receivedMessage = waitAnyContentMessage().filter {
waitAnyContentMessage().filter { it.sameChat(state.context)
it.chat.id == state.context && it.content.textContentOrNull() ?.text != "/finish_post"
}.take(1).first()
)
}
add {
val finishPressed = waitMessageDataCallbackQuery().filter {
it.message.sameMessage(messageToDelete) && it.data == buttonUuid
}.first() }.first()
emptyList<ContentMessage<MessageContent>>()
} when {
add { receivedMessage.content.textContentOrNull() ?.text == "/finish_post" -> {
val finishPressed = waitTextMessage().filter { val messageToDelete = suggestionMessageDeferred.await()
it.sameChat(messageToDelete) && it.content.text == "/finish_post"
}.first()
emptyList<ContentMessage<MessageContent>>()
}
}.ifEmpty {
edit(messageToDelete, "Ok, finishing your request") edit(messageToDelete, "Ok, finishing your request")
return@strictlyOn RegistrationState.Finish( RegistrationState.Finish(
state.context, state.context,
state.messages state.messages
) )
}.flatMap {
PostContentInfo.fromMessage(it)
} }
else -> {
RegistrationState.InProcess( RegistrationState.InProcess(
state.context, state.context,
state.messages + newMessagesInfo state.messages + PostContentInfo.fromMessage(receivedMessage)
).also { ).also {
delete(messageToDelete) runCatchingSafely {
suggestionMessageDeferred.cancel()
}
runCatchingSafely {
delete(suggestionMessageDeferred.await())
}
}
}
}
}
add {
val messageToDelete = suggestionMessageDeferred.await()
val finishPressed = waitMessageDataCallbackQuery().filter {
it.message.sameMessage(messageToDelete) && it.data == buttonUuid
}.first()
edit(messageToDelete, "Ok, finishing your request")
RegistrationState.Finish(
state.context,
state.messages
)
}
} }
} }
@ -108,12 +119,12 @@ object Plugin : Plugin {
null null
} }
onCommand("start_post", initialFilter = { it.chat.id == config.sourceChatId }) { onCommand("start_post", initialFilter = { it.sameChat(config.sourceChatId) }) {
startChain(RegistrationState.InProcess(it.chat.id, emptyList())) startChain(RegistrationState.InProcess(it.chat.id, emptyList()))
} }
onContentMessage( onContentMessage(
initialFilter = { it.chat.id == config.sourceChatId && !FirstSourceIsCommandsFilter(it) } initialFilter = { it.sameChat(config.sourceChatId) && !FirstSourceIsCommandsFilter(it) }
) { ) {
startChain(RegistrationState.Finish(it.chat.id, PostContentInfo.fromMessage(it))) startChain(RegistrationState.Finish(it.chat.id, PostContentInfo.fromMessage(it)))
} }

View File

@ -15,7 +15,7 @@ function assert_success() {
app=plaguposter app=plaguposter
version="`grep ../gradle.properties -e "^version=" | sed -e "s/version=\(.*\)/\1/"`" version="`grep ../gradle.properties -e "^version=" | sed -e "s/version=\(.*\)/\1/"`"
server=docker.io/insanusmokrassar server=insanusmokrassar
assert_success ../gradlew build assert_success ../gradlew build
assert_success sudo docker build -t $app:"$version" . assert_success sudo docker build -t $app:"$version" .