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.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
|
|
||||||
}
|
}
|
||||||
|
@ -18,84 +18,95 @@ 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>()
|
||||||
|
|
||||||
strictlyOn {state: RegistrationState.InProcess ->
|
strictlyOn { state: RegistrationState.InProcess ->
|
||||||
val buttonUuid = "finish"
|
val buttonUuid = "finish"
|
||||||
|
|
||||||
val messageToDelete = send(
|
val suggestionMessageDeferred = async {
|
||||||
state.context,
|
send(
|
||||||
dev.inmo.tgbotapi.utils.buildEntities {
|
state.context,
|
||||||
if (state.messages.isNotEmpty()) {
|
dev.inmo.tgbotapi.utils.buildEntities {
|
||||||
regular("Your message(s) has been registered. You may send new ones or push \"Finish\" to finalize your post")
|
if (state.messages.isNotEmpty()) {
|
||||||
|
regular("Your message(s) has been registered. You may send new ones or push \"Finish\" to finalize your post")
|
||||||
|
} else {
|
||||||
|
regular("Ok, send me your messages for new post")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
replyMarkup = if (state.messages.isNotEmpty()) {
|
||||||
|
flatInlineKeyboard {
|
||||||
|
dataButton(
|
||||||
|
"Finish",
|
||||||
|
buttonUuid
|
||||||
|
)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
regular("Ok, send me your messages for new post")
|
null
|
||||||
}
|
}
|
||||||
},
|
)
|
||||||
replyMarkup = if (state.messages.isNotEmpty()) {
|
}
|
||||||
flatInlineKeyboard {
|
|
||||||
dataButton(
|
|
||||||
"Finish",
|
|
||||||
buttonUuid
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
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"
|
}.first()
|
||||||
}.take(1).first()
|
|
||||||
)
|
when {
|
||||||
|
receivedMessage.content.textContentOrNull() ?.text == "/finish_post" -> {
|
||||||
|
val messageToDelete = suggestionMessageDeferred.await()
|
||||||
|
edit(messageToDelete, "Ok, finishing your request")
|
||||||
|
RegistrationState.Finish(
|
||||||
|
state.context,
|
||||||
|
state.messages
|
||||||
|
)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
RegistrationState.InProcess(
|
||||||
|
state.context,
|
||||||
|
state.messages + PostContentInfo.fromMessage(receivedMessage)
|
||||||
|
).also {
|
||||||
|
runCatchingSafely {
|
||||||
|
suggestionMessageDeferred.cancel()
|
||||||
|
}
|
||||||
|
runCatchingSafely {
|
||||||
|
delete(suggestionMessageDeferred.await())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
add {
|
add {
|
||||||
|
val messageToDelete = suggestionMessageDeferred.await()
|
||||||
val finishPressed = waitMessageDataCallbackQuery().filter {
|
val finishPressed = waitMessageDataCallbackQuery().filter {
|
||||||
it.message.sameMessage(messageToDelete) && it.data == buttonUuid
|
it.message.sameMessage(messageToDelete) && it.data == buttonUuid
|
||||||
}.first()
|
}.first()
|
||||||
emptyList<ContentMessage<MessageContent>>()
|
|
||||||
}
|
|
||||||
add {
|
|
||||||
val finishPressed = waitTextMessage().filter {
|
|
||||||
it.sameChat(messageToDelete) && it.content.text == "/finish_post"
|
|
||||||
}.first()
|
|
||||||
emptyList<ContentMessage<MessageContent>>()
|
|
||||||
}
|
|
||||||
}.ifEmpty {
|
|
||||||
edit(messageToDelete, "Ok, finishing your request")
|
|
||||||
return@strictlyOn RegistrationState.Finish(
|
|
||||||
state.context,
|
|
||||||
state.messages
|
|
||||||
)
|
|
||||||
}.flatMap {
|
|
||||||
PostContentInfo.fromMessage(it)
|
|
||||||
}
|
|
||||||
|
|
||||||
RegistrationState.InProcess(
|
edit(messageToDelete, "Ok, finishing your request")
|
||||||
state.context,
|
RegistrationState.Finish(
|
||||||
state.messages + newMessagesInfo
|
state.context,
|
||||||
).also {
|
state.messages
|
||||||
delete(messageToDelete)
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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)))
|
||||||
}
|
}
|
||||||
|
@ -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" .
|
||||||
|
Loading…
Reference in New Issue
Block a user