This commit is contained in:
InsanusMokrassar 2022-09-09 20:38:06 +06:00
parent 23be7cac81
commit f411b757fd
9 changed files with 40 additions and 23 deletions

View File

@ -34,7 +34,6 @@ android-test-junit = { module = "androidx.test.ext:junit", version.ref = "test_e
android-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso_core" } android-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso_core" }
tgbotapi = { module = "dev.inmo:tgbotapi", version.ref = "tgbotapi" } tgbotapi = { module = "dev.inmo:tgbotapi", version.ref = "tgbotapi" }
tgbotapi-admins = { module = "dev.inmo:tgbotapi.libraries.cache.admins.plagubot", version.ref = "tgbotapi-libraries" }
plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" } plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" }
plagubot-bot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" } plagubot-bot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" }
microutils-repos-common = { module = "dev.inmo:micro_utils.repos.common", version.ref = "microutils" } microutils-repos-common = { module = "dev.inmo:micro_utils.repos.common", version.ref = "microutils" }

View File

@ -11,7 +11,6 @@ kotlin {
commonMain { commonMain {
dependencies { dependencies {
api project(":plaguposter.common") api project(":plaguposter.common")
api libs.tgbotapi.admins
} }
} }
jvmMain { jvmMain {

View File

@ -4,14 +4,16 @@ import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.plagubot.Plugin import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.common.ChatConfig 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.models.OfferTemplate
import dev.inmo.plaguposter.inlines.repos.InlineTemplatesRepo import dev.inmo.plaguposter.inlines.repos.InlineTemplatesRepo
import dev.inmo.tgbotapi.bot.exceptions.RequestException import dev.inmo.tgbotapi.bot.exceptions.RequestException
import dev.inmo.tgbotapi.extensions.api.answers.answerInlineQuery 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.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onBaseInlineQuery import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onBaseInlineQuery
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.libraries.cache.admins.adminsPlugin import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
import dev.inmo.tgbotapi.types.inlineQueryAnswerResultsLimit import dev.inmo.tgbotapi.types.inlineQueryAnswerResultsLimit
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
@ -30,15 +32,10 @@ object Plugin : Plugin {
} }
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val adminsApi = koin.get<AdminsCacheAPI>()
val chatConfig = koin.get<ChatConfig>()
val templatesRepo = koin.get<InlineTemplatesRepo>() val templatesRepo = koin.get<InlineTemplatesRepo>()
onBaseInlineQuery { query -> onBaseInlineQuery { query ->
if (!adminsApi.isAdmin(chatConfig.sourceChatId, query.from.id)) {
answerInlineQuery(query, cachedTime = 0)
return@onBaseInlineQuery
}
val page = query.offset.toIntOrNull() ?: 0 val page = query.offset.toIntOrNull() ?: 0
val queryString = query.query.trim()
try { try {
answerInlineQuery( answerInlineQuery(
query, query,
@ -50,7 +47,7 @@ object Plugin : Plugin {
).results.mapIndexedNotNull { index, offerTemplate -> ).results.mapIndexedNotNull { index, offerTemplate ->
offerTemplate.createArticleResult( offerTemplate.createArticleResult(
index.toString(), index.toString(),
query.query queryString
) )
}, },
nextOffset = (page + 1).toString(), nextOffset = (page + 1).toString(),
@ -63,5 +60,22 @@ object Plugin : Plugin {
) )
} }
} }
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"))
)
)
}
} }
} }

View File

@ -141,7 +141,7 @@ object Plugin : Plugin {
addTemplate( addTemplate(
OfferTemplate( OfferTemplate(
"Start post", "Start post",
listOf(Format("start_post")) listOf(Format("/start_post"))
) )
) )
} }

View File

@ -109,7 +109,7 @@ data class RatingConfig(
} }
companion object { companion object {
val values = arrayOf(Max, Min, Random) val values: Array<Prefer> = arrayOf(Max, Min, Random)
} }
} }
} }

View File

@ -197,14 +197,14 @@ object Plugin : Plugin {
addTemplate( addTemplate(
OfferTemplate( OfferTemplate(
"Enable ratings for post", "Enable ratings for post",
listOf(Format("attach_ratings")), listOf(Format("/attach_ratings")),
"Require reply on post message" "Require reply on post message"
) )
) )
addTemplate( addTemplate(
OfferTemplate( OfferTemplate(
"Disable ratings for post", "Disable ratings for post",
listOf(Format("detach_ratings")), listOf(Format("/detach_ratings")),
"Require reply on post message" "Require reply on post message"
) )
) )

View File

@ -9,7 +9,6 @@ project.version = null
dependencies { dependencies {
implementation libs.kotlin implementation libs.kotlin
api libs.plagubot.bot api libs.plagubot.bot
api libs.tgbotapi.admins
api project(":plaguposter.posts") api project(":plaguposter.posts")
api project(":plaguposter.posts_registrar") api project(":plaguposter.posts_registrar")

View File

@ -14,8 +14,8 @@
"dev.inmo.plaguposter.ratings.selector.Plugin", "dev.inmo.plaguposter.ratings.selector.Plugin",
"dev.inmo.plaguposter.triggers.selector_with_timer.Plugin", "dev.inmo.plaguposter.triggers.selector_with_timer.Plugin",
"dev.inmo.plaguposter.ratings.gc.Plugin", "dev.inmo.plaguposter.ratings.gc.Plugin",
"dev.inmo.tgbotapi.libraries.cache.admins.AdminsPlugin", "dev.inmo.plaguposter.inlines.Plugin",
"dev.inmo.plaguposter.inlines.Plugin" "dev.inmo.plaguposter.triggers.command.Plugin"
], ],
"posts": { "posts": {
"targetChat": 12345678, "targetChat": 12345678,

View File

@ -45,12 +45,18 @@ object Plugin : Plugin {
val selector = koin.getOrNull<Selector>() val selector = koin.getOrNull<Selector>()
onCommand("publish_post") { onCommand("publish_post") {
val messageInReply = it.replyTo ?.contentMessageOrNull() ?: let { _ -> val messageInReply = it.replyTo ?.contentMessageOrNull() ?: run {
if (selector == null) {
reply(it, "You should reply some message of post to trigger it for posting") reply(it, "You should reply some message of post to trigger it for posting")
return@onCommand return@onCommand
} else {
null
} }
val postId = postsRepo.getIdByChatAndMessage(messageInReply.chat.id, messageInReply.messageId) ?: selector ?.take(1) ?.firstOrNull() }
val postId = messageInReply ?.let {
postsRepo.getIdByChatAndMessage(messageInReply.chat.id, messageInReply.messageId)
} ?: selector ?.take(1) ?.firstOrNull()
if (postId == null) { if (postId == null) {
reply( reply(
it, it,
@ -72,7 +78,7 @@ object Plugin : Plugin {
addTemplate( addTemplate(
OfferTemplate( OfferTemplate(
"Publish post", "Publish post",
listOf(Format("publish_post")), listOf(Format("/publish_post")),
if (selector == null) { if (selector == null) {
"Require reply on post message" "Require reply on post message"
} else { } else {