mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2025-09-15 21:19:30 +00:00
deprecate uncommon inline plugin
This commit is contained in:
@@ -15,6 +15,7 @@ kotlin {
|
||||
}
|
||||
jvmMain {
|
||||
dependencies {
|
||||
api libs.plagubot.plugins.inline.queries
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1 +1 @@
|
||||
package dev.inmo.plaguposter.inlines
|
||||
package dev.inmo.plagubot.plugins.inline.queries
|
||||
|
@@ -1,81 +1,21 @@
|
||||
package dev.inmo.plaguposter.inlines
|
||||
|
||||
import dev.inmo.micro_utils.pagination.Pagination
|
||||
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||
import dev.inmo.kslog.common.TagLogger
|
||||
import dev.inmo.kslog.common.w
|
||||
import dev.inmo.plagubot.Plugin
|
||||
import dev.inmo.plaguposter.common.ChatConfig
|
||||
import dev.inmo.plaguposter.inlines.models.Format
|
||||
import dev.inmo.plaguposter.inlines.models.OfferTemplate
|
||||
import dev.inmo.plaguposter.inlines.repos.InlineTemplatesRepo
|
||||
import dev.inmo.tgbotapi.bot.exceptions.RequestException
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answerInlineQuery
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onBaseInlineQuery
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||
import dev.inmo.tgbotapi.types.inlineQueryAnswerResultsLimit
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.*
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object Plugin : Plugin {
|
||||
@Serializable
|
||||
internal data class Config(
|
||||
val preset: List<OfferTemplate>
|
||||
)
|
||||
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["inlines"] ?: return@single Config(emptyList())) }
|
||||
single { InlineTemplatesRepo(getOrNull<Config>() ?.preset ?.toMutableSet() ?: mutableSetOf()) }
|
||||
}
|
||||
private val actualPlugin = dev.inmo.plagubot.plugins.inline.queries.Plugin
|
||||
|
||||
object Plugin : Plugin by actualPlugin {
|
||||
private val log = TagLogger("InlinePlugin")
|
||||
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||
val templatesRepo = koin.get<InlineTemplatesRepo>()
|
||||
onBaseInlineQuery { query ->
|
||||
val page = query.offset.toIntOrNull() ?: 0
|
||||
val queryString = query.query.trim()
|
||||
try {
|
||||
answerInlineQuery(
|
||||
query,
|
||||
templatesRepo.templates.paginate(
|
||||
Pagination(
|
||||
page,
|
||||
inlineQueryAnswerResultsLimit.last + 1
|
||||
)
|
||||
).results.mapIndexedNotNull { index, offerTemplate ->
|
||||
offerTemplate.createArticleResult(
|
||||
index.toString(),
|
||||
queryString
|
||||
)
|
||||
},
|
||||
nextOffset = (page + 1).toString(),
|
||||
cachedTime = 0
|
||||
)
|
||||
} catch (e: RequestException) {
|
||||
bot.answerInlineQuery(
|
||||
query,
|
||||
cachedTime = 0
|
||||
)
|
||||
}
|
||||
log.w {
|
||||
"Built-in inline plugin has been deprecated. Use \"${actualPlugin::class.qualifiedName}\" instead"
|
||||
}
|
||||
onCommand("help", requireOnlyCommandInMessage = true) {
|
||||
reply(
|
||||
it,
|
||||
"Push the button above to see available commands",
|
||||
replyMarkup = flatInlineKeyboard {
|
||||
inlineQueryInCurrentChatButton("Toggle commands", "")
|
||||
}
|
||||
)
|
||||
}
|
||||
koin.getOrNull<InlineTemplatesRepo>() ?.apply {
|
||||
addTemplate(
|
||||
OfferTemplate(
|
||||
"Trigger help button",
|
||||
listOf(Format("/help"))
|
||||
)
|
||||
)
|
||||
with(actualPlugin) {
|
||||
setupBotPlugin(koin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,40 +0,0 @@
|
||||
package dev.inmo.plaguposter.inlines.models
|
||||
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
||||
import dev.inmo.tgbotapi.types.message.MarkdownV2
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
|
||||
@Serializable
|
||||
data class Format(
|
||||
val template: String,
|
||||
val regexTemplate: String = "^$",
|
||||
val splitBy: String? = null,
|
||||
val enableMarkdownSupport: Boolean = false
|
||||
) {
|
||||
@Transient
|
||||
val queryRegex = Regex(regexTemplate, RegexOption.DOT_MATCHES_ALL)
|
||||
|
||||
init {
|
||||
println(queryRegex)
|
||||
}
|
||||
|
||||
fun formatByRegex(with: String): String? {
|
||||
return if (queryRegex.matches(with)) {
|
||||
template.format(*(splitBy ?.let { with.split(it).toTypedArray() } ?: arrayOf(with)))
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun createContent(with: String): InputTextMessageContent? {
|
||||
return if (queryRegex.matches(with)) {
|
||||
InputTextMessageContent(
|
||||
template.format(*(splitBy ?.let { with.split(it).toTypedArray() } ?: arrayOf(with))),
|
||||
if (enableMarkdownSupport) MarkdownV2 else null
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,22 +0,0 @@
|
||||
package dev.inmo.plaguposter.inlines.models
|
||||
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class OfferTemplate(
|
||||
val title: String,
|
||||
val formats: List<Format> = emptyList(),
|
||||
val description: String? = null
|
||||
) {
|
||||
fun createArticleResult(id: String, query: String): InlineQueryResultArticle? = formats.firstOrNull {
|
||||
it.queryRegex.matches(query)
|
||||
} ?.createContent(query) ?.let { content ->
|
||||
InlineQueryResultArticle(
|
||||
id,
|
||||
title,
|
||||
content,
|
||||
description = description
|
||||
)
|
||||
}
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
package dev.inmo.plaguposter.inlines.repos
|
||||
|
||||
import dev.inmo.plaguposter.inlines.models.OfferTemplate
|
||||
|
||||
class InlineTemplatesRepo(
|
||||
private val _templates: MutableSet<OfferTemplate>
|
||||
) {
|
||||
internal val templates
|
||||
get() = _templates.toList()
|
||||
suspend fun addTemplate(offerTemplate: OfferTemplate): Boolean {
|
||||
return _templates.add(offerTemplate)
|
||||
}
|
||||
suspend fun dropTemplate(offerTemplate: OfferTemplate): Boolean {
|
||||
return _templates.remove(offerTemplate)
|
||||
}
|
||||
}
|
@@ -1 +1 @@
|
||||
<manifest package="dev.inmo.plaguposter.inlines"/>
|
||||
<manifest package="dev.inmo.plagubot.plugins.inline.queries"/>
|
||||
|
Reference in New Issue
Block a user