mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2024-12-22 14:07:14 +00:00
improvements
This commit is contained in:
parent
847b285ce3
commit
0a5ffee808
@ -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.utils.SimpleFilter
|
||||
import dev.inmo.tgbotapi.extensions.utils.contentMessageOrNull
|
||||
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.message.abstracts.*
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
|
||||
|
||||
val FirstSourceIsCommandsFilter = SimpleFilter<Message> {
|
||||
it is ContentMessage<*> && it.content.textContentOrNull() ?.textSources ?.firstOrNull {
|
||||
it is BotCommandTextSource
|
||||
} != null
|
||||
it.contentMessageOrNull() ?.withContentOrNull<TextContent>() ?.content ?.textSources ?.firstOrNull() is BotCommandTextSource
|
||||
}
|
||||
|
@ -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.strictlyOn
|
||||
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.sameMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
|
||||
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 kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.koin.core.Koin
|
||||
|
||||
@Serializable
|
||||
object Plugin : Plugin {
|
||||
@Serializable
|
||||
data class Config(
|
||||
val useInlineFinishingOpportunity: Boolean = true
|
||||
)
|
||||
|
||||
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
|
||||
val config = koin.get<ChatConfig>()
|
||||
val postsRepo = koin.get<PostsRepo>()
|
||||
@ -40,7 +42,8 @@ object Plugin : Plugin {
|
||||
strictlyOn { state: RegistrationState.InProcess ->
|
||||
val buttonUuid = "finish"
|
||||
|
||||
val messageToDelete = send(
|
||||
val suggestionMessageDeferred = async {
|
||||
send(
|
||||
state.context,
|
||||
dev.inmo.tgbotapi.utils.buildEntities {
|
||||
if (state.messages.isNotEmpty()) {
|
||||
@ -60,42 +63,50 @@ object Plugin : Plugin {
|
||||
null
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
val newMessagesInfo = firstOf {
|
||||
firstOf {
|
||||
add {
|
||||
listOf(
|
||||
waitAnyContentMessage().filter {
|
||||
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
|
||||
val receivedMessage = waitAnyContentMessage().filter {
|
||||
it.sameChat(state.context)
|
||||
}.first()
|
||||
emptyList<ContentMessage<MessageContent>>()
|
||||
}
|
||||
add {
|
||||
val finishPressed = waitTextMessage().filter {
|
||||
it.sameChat(messageToDelete) && it.content.text == "/finish_post"
|
||||
}.first()
|
||||
emptyList<ContentMessage<MessageContent>>()
|
||||
}
|
||||
}.ifEmpty {
|
||||
|
||||
when {
|
||||
receivedMessage.content.textContentOrNull() ?.text == "/finish_post" -> {
|
||||
val messageToDelete = suggestionMessageDeferred.await()
|
||||
edit(messageToDelete, "Ok, finishing your request")
|
||||
return@strictlyOn RegistrationState.Finish(
|
||||
RegistrationState.Finish(
|
||||
state.context,
|
||||
state.messages
|
||||
)
|
||||
}.flatMap {
|
||||
PostContentInfo.fromMessage(it)
|
||||
}
|
||||
|
||||
else -> {
|
||||
RegistrationState.InProcess(
|
||||
state.context,
|
||||
state.messages + newMessagesInfo
|
||||
state.messages + PostContentInfo.fromMessage(receivedMessage)
|
||||
).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
|
||||
}
|
||||
|
||||
onCommand("start_post", initialFilter = { it.chat.id == config.sourceChatId }) {
|
||||
onCommand("start_post", initialFilter = { it.sameChat(config.sourceChatId) }) {
|
||||
startChain(RegistrationState.InProcess(it.chat.id, emptyList()))
|
||||
}
|
||||
|
||||
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)))
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ function assert_success() {
|
||||
|
||||
app=plaguposter
|
||||
version="`grep ../gradle.properties -e "^version=" | sed -e "s/version=\(.*\)/\1/"`"
|
||||
server=docker.io/insanusmokrassar
|
||||
server=insanusmokrassar
|
||||
|
||||
assert_success ../gradlew build
|
||||
assert_success sudo docker build -t $app:"$version" .
|
||||
|
Loading…
Reference in New Issue
Block a user