All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ubot.plugins.inline.queries.0.23.0.source-code.Plugin.kt Maven / Gradle / Ivy

There is a newer version: 0.24.1
Show newest version
package dev.inmo.plagubot.plugins.inline.queries

import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.plagubot.Plugin
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
import dev.inmo.plagubot.registerConfig
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.flatInlineKeyboard
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineQueryInCurrentChatButton
import dev.inmo.tgbotapi.types.inlineQueryAnswerResultsLimit
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module

object Plugin : Plugin {
    @Serializable
    internal data class Config(
        val preset: List
    )
    override fun Module.setupDI(config: JsonObject) {
        registerConfig("inlines") { Config(emptyList()) }
        single { InlineTemplatesRepo(getOrNull() ?.preset ?.toMutableSet() ?: mutableSetOf()) }
    }

    override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
        val templatesRepo = koin.get()
        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
                )
            }
        }
        onCommand("help", requireOnlyCommandInMessage = true) {
            reply(
                it,
                "Push the button above to see available commands",
                replyMarkup = flatInlineKeyboard {
                    inlineQueryInCurrentChatButton("Toggle commands", "")
                }
            )
        }
        koin.getOrNull() ?.apply {
            addTemplate(
                OfferTemplate(
                    "Trigger help button",
                    listOf(Format("/help"))
                )
            )
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy