From f1aa67cedad1a91278fbc021b8294bd6065c986c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 21 Apr 2023 22:26:11 +0600 Subject: [PATCH] add support of InlineQueryResultsButton --- .../api/answers/AnswerInlineQuery.kt | 42 +++++++-- .../requests/answers/AnswerInlineQuery.kt | 46 ++++++++-- .../answers/InlineQueryResultsButton.kt | 89 +++++++++++++++++++ .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 1 + 4 files changed, 166 insertions(+), 12 deletions(-) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/InlineQueryResultsButton.kt diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/answers/AnswerInlineQuery.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/answers/AnswerInlineQuery.kt index b40ff79f44..3e6a3da5e2 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/answers/AnswerInlineQuery.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/answers/AnswerInlineQuery.kt @@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.extensions.api.answers import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.requests.answers.AnswerInlineQuery +import dev.inmo.tgbotapi.requests.answers.InlineQueryResultsButton import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts.InlineQueryResult import dev.inmo.tgbotapi.types.InlineQueries.query.InlineQuery import dev.inmo.tgbotapi.types.InlineQueryIdentifier @@ -12,8 +13,37 @@ suspend fun TelegramBot.answerInlineQuery( cachedTime: Int? = null, isPersonal: Boolean? = null, nextOffset: String? = null, - switchPmText: String? = null, - switchPmParameter: String? = null + button: InlineQueryResultsButton? = null +) = execute( + AnswerInlineQuery(inlineQueryID, results, cachedTime, isPersonal, nextOffset, button) +) + +suspend fun TelegramBot.answerInlineQuery( + inlineQuery: InlineQuery, + results: List = emptyList(), + cachedTime: Int? = null, + isPersonal: Boolean? = null, + nextOffset: String? = null, + button: InlineQueryResultsButton? = null +) = answerInlineQuery(inlineQuery.id, results, cachedTime, isPersonal, nextOffset, button) + +suspend fun TelegramBot.answer( + inlineQuery: InlineQuery, + results: List = emptyList(), + cachedTime: Int? = null, + isPersonal: Boolean? = null, + nextOffset: String? = null, + button: InlineQueryResultsButton? = null +) = answerInlineQuery(inlineQuery.id, results, cachedTime, isPersonal, nextOffset, button) + +suspend fun TelegramBot.answerInlineQuery( + inlineQueryID: InlineQueryIdentifier, + results: List = emptyList(), + cachedTime: Int? = null, + isPersonal: Boolean? = null, + nextOffset: String? = null, + switchPmText: String?, + switchPmParameter: String? ) = execute( AnswerInlineQuery(inlineQueryID, results, cachedTime, isPersonal, nextOffset, switchPmText, switchPmParameter) ) @@ -24,8 +54,8 @@ suspend fun TelegramBot.answerInlineQuery( cachedTime: Int? = null, isPersonal: Boolean? = null, nextOffset: String? = null, - switchPmText: String? = null, - switchPmParameter: String? = null + switchPmText: String?, + switchPmParameter: String? ) = answerInlineQuery(inlineQuery.id, results, cachedTime, isPersonal, nextOffset, switchPmText, switchPmParameter) suspend fun TelegramBot.answer( @@ -34,6 +64,6 @@ suspend fun TelegramBot.answer( cachedTime: Int? = null, isPersonal: Boolean? = null, nextOffset: String? = null, - switchPmText: String? = null, - switchPmParameter: String? = null + switchPmText: String?, + switchPmParameter: String? ) = answerInlineQuery(inlineQuery.id, results, cachedTime, isPersonal, nextOffset, switchPmText, switchPmParameter) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/AnswerInlineQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/AnswerInlineQuery.kt index e721e2389b..7058ae061a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/AnswerInlineQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/AnswerInlineQuery.kt @@ -23,11 +23,30 @@ data class AnswerInlineQuery( val isPersonal: Boolean? = null, @SerialName(nextOffsetField) val nextOffset: String? = null, - @SerialName(switchPmTextField) - val switchPmText: String? = null, - @SerialName(switchPmParameterField) - val switchPmParameter: String? = null + @SerialName(buttonField) + val button: InlineQueryResultsButton? = null, ) : SimpleRequest { + constructor( + inlineQueryID: InlineQueryIdentifier, + results: List = emptyList(), + cachedTime: Int? = null, + isPersonal: Boolean? = null, + nextOffset: String? = null, + switchPmText: String?, + switchPmParameter: String? + ) : this( + inlineQueryID, + results, + cachedTime, + isPersonal, + nextOffset, + switchPmText ?.let { + switchPmParameter ?.let { + InlineQueryResultsButton.Start(switchPmText, switchPmParameter) + } + } + ) + override fun method(): String = "answerInlineQuery" override val resultDeserializer: DeserializationStrategy get() = Boolean.serializer() @@ -40,8 +59,23 @@ fun InlineQuery.createAnswer( cachedTime: Int? = null, isPersonal: Boolean? = null, nextOffset: String? = null, - switchPmText: String? = null, - switchPmParameter: String? = null + button: InlineQueryResultsButton? = null, +) = AnswerInlineQuery( + id, + results, + cachedTime, + isPersonal, + nextOffset, + button +) + +fun InlineQuery.createAnswer( + results: List = emptyList(), + cachedTime: Int? = null, + isPersonal: Boolean? = null, + nextOffset: String? = null, + switchPmText: String?, + switchPmParameter: String? ) = AnswerInlineQuery( id, results, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/InlineQueryResultsButton.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/InlineQueryResultsButton.kt new file mode 100644 index 0000000000..4cc8bf71b0 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/answers/InlineQueryResultsButton.kt @@ -0,0 +1,89 @@ +package dev.inmo.tgbotapi.requests.answers + +import dev.inmo.micro_utils.common.Warning +import dev.inmo.tgbotapi.types.StartParameter +import dev.inmo.tgbotapi.types.startParameterField +import dev.inmo.tgbotapi.types.textField +import dev.inmo.tgbotapi.types.webAppField +import dev.inmo.tgbotapi.types.webapps.WebAppInfo +import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded +import kotlinx.serialization.KSerializer +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +@Serializable(InlineQueryResultsButtonSerializer::class) +@ClassCastsIncluded +sealed interface InlineQueryResultsButton { + val text: String + + @Serializable + class Raw internal constructor( + @SerialName(textField) + val text: String, + @SerialName(webAppField) + val webAppInfo: WebAppInfo? = null, + @SerialName(startParameterField) + val deepLinkParameter: String? = null + ) + + @Serializable(InlineQueryResultsButtonSerializer::class) + data class WebApp( + @SerialName(textField) + override val text: String, + @SerialName(webAppField) + val webAppInfo: WebAppInfo + ) : InlineQueryResultsButton + + @Serializable(InlineQueryResultsButtonSerializer::class) + data class Start( + @SerialName(textField) + override val text: String, + @SerialName(startParameterField) + val deepLinkParameter: String + ) : InlineQueryResultsButton + + @Serializable(InlineQueryResultsButtonSerializer::class) + data class Unknown internal constructor ( + @SerialName(textField) + override val text: String + ) : InlineQueryResultsButton + + companion object { + operator fun invoke( + text: String, + deepLinkParameter: String + ) = Start(text, deepLinkParameter) + operator fun invoke( + text: String, + webAppInfo: WebAppInfo + ) = WebApp(text, webAppInfo) + } +} + +object InlineQueryResultsButtonSerializer : KSerializer { + override val descriptor: SerialDescriptor = InlineQueryResultsButton.Raw.serializer().descriptor + + override fun deserialize(decoder: Decoder): InlineQueryResultsButton { + val raw = InlineQueryResultsButton.Raw.serializer().deserialize(decoder) + + return when { + raw.webAppInfo != null -> InlineQueryResultsButton.WebApp(raw.text, raw.webAppInfo) + raw.deepLinkParameter != null -> InlineQueryResultsButton.Start(raw.text, raw.deepLinkParameter) + else -> InlineQueryResultsButton.Unknown(raw.text) + } + } + + override fun serialize(encoder: Encoder, value: InlineQueryResultsButton) { + InlineQueryResultsButton.Raw.serializer().serialize( + encoder, + InlineQueryResultsButton.Raw( + value.text, + (value as? InlineQueryResultsButton.WebApp)?.webAppInfo, + (value as? InlineQueryResultsButton.Start)?.deepLinkParameter, + ) + ) + } +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 2e69e85555..9eb1e50bde 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -250,6 +250,7 @@ const val errorMessageField = "error_message" const val messageTextField = "message_text" const val isPersonalField = "is_personal" const val nextOffsetField = "next_offset" +const val buttonField = "buttonField" const val switchPmTextField = "switch_pm_text" const val switchPmParameterField = "switch_pm_parameter" const val maxAllowedConnectionsField = "max_connections"