PlaguPoster/posts_registrar/src/jvmMain/kotlin/Plugin.kt

149 lines
5.9 KiB
Kotlin
Raw Normal View History

2022-08-19 18:24:00 +00:00
package dev.inmo.plaguposter.posts.registrar
2022-09-04 09:46:45 +00:00
import dev.inmo.micro_utils.coroutines.*
2022-08-19 18:24:00 +00:00
import dev.inmo.micro_utils.fsm.common.State
import dev.inmo.micro_utils.repos.create
import dev.inmo.plagubot.Plugin
2022-09-04 09:46:45 +00:00
import dev.inmo.plaguposter.common.*
2022-10-11 05:38:50 +00:00
import dev.inmo.plagubot.plugins.inline.queries.models.Format
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
2022-09-04 07:27:35 +00:00
import dev.inmo.plaguposter.posts.models.*
2022-08-19 18:24:00 +00:00
import dev.inmo.plaguposter.posts.registrar.state.RegistrationState
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.tgbotapi.extensions.api.delete
import dev.inmo.tgbotapi.extensions.api.edit.edit
import dev.inmo.tgbotapi.extensions.api.send.send
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.sameChat
2022-08-19 18:24:00 +00:00
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
2022-10-03 05:16:09 +00:00
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
2022-08-19 18:24:00 +00:00
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
2022-11-17 09:09:10 +00:00
import dev.inmo.tgbotapi.utils.regular
2023-04-06 09:52:44 +00:00
import kotlinx.coroutines.async
2022-08-19 18:24:00 +00:00
import kotlinx.coroutines.flow.*
import kotlinx.serialization.Serializable
import org.koin.core.Koin
@Serializable
object Plugin : Plugin {
2023-04-06 09:52:44 +00:00
@Serializable
data class Config(
val useInlineFinishingOpportunity: Boolean = true
)
2022-08-19 18:24:00 +00:00
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
2022-09-04 07:27:35 +00:00
val config = koin.get<ChatConfig>()
2022-08-19 18:24:00 +00:00
val postsRepo = koin.get<PostsRepo>()
2023-04-06 09:52:44 +00:00
strictlyOn { state: RegistrationState.InProcess ->
2022-08-19 18:24:00 +00:00
val buttonUuid = "finish"
2023-04-06 09:52:44 +00:00
val suggestionMessageDeferred = async {
send(
state.context,
dev.inmo.tgbotapi.utils.buildEntities {
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
)
}
2022-08-19 18:24:00 +00:00
} else {
2023-04-06 09:52:44 +00:00
null
2022-08-19 18:24:00 +00:00
}
2023-04-06 09:52:44 +00:00
)
}
2022-08-19 18:24:00 +00:00
2023-04-06 09:52:44 +00:00
firstOf {
2022-08-19 18:24:00 +00:00
add {
2023-04-06 09:52:44 +00:00
val receivedMessage = waitAnyContentMessage().filter {
it.sameChat(state.context)
}.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())
}
}
}
}
2022-08-19 18:24:00 +00:00
}
add {
2023-04-06 09:52:44 +00:00
val messageToDelete = suggestionMessageDeferred.await()
2022-08-19 18:24:00 +00:00
val finishPressed = waitMessageDataCallbackQuery().filter {
it.message.sameMessage(messageToDelete) && it.data == buttonUuid
}.first()
2023-04-06 09:52:44 +00:00
edit(messageToDelete, "Ok, finishing your request")
RegistrationState.Finish(
state.context,
state.messages
)
}
2022-08-19 18:24:00 +00:00
}
}
strictlyOn { state: RegistrationState.Finish ->
postsRepo.create(
NewPost(
state.messages
)
)
null
}
onCommand("start_post", initialFilter = { config.allSourceChatIds.any { chatId -> it.sameChat(chatId) } }) {
2022-08-19 18:24:00 +00:00
startChain(RegistrationState.InProcess(it.chat.id, emptyList()))
}
onContentMessage(
initialFilter = { config.allSourceChatIds.any { chatId -> it.sameChat(chatId) } && !FirstSourceIsCommandsFilter(it) }
2022-08-19 18:24:00 +00:00
) {
2022-11-17 09:09:10 +00:00
startChain(RegistrationState.Finish(it.chat.id, PostContentInfo.fromMessage(it)))
2022-08-19 18:24:00 +00:00
}
2022-09-09 13:46:32 +00:00
koin.getOrNull<InlineTemplatesRepo>() ?.apply {
addTemplate(
OfferTemplate(
"Start post creating",
listOf(Format("/start_post")),
"Use this command to start creating of complex post with several messages"
)
)
addTemplate(
OfferTemplate(
"Finish post creating",
listOf(Format("/finish_post")),
"Finish creating of complex post"
2022-09-09 13:46:32 +00:00
)
)
}
2022-08-19 18:24:00 +00:00
}
}