1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

add support of InlineQueryResultsButton

This commit is contained in:
InsanusMokrassar 2023-04-21 22:26:11 +06:00
parent 1c66d1e2fc
commit f1aa67ceda
4 changed files with 166 additions and 12 deletions

View File

@ -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<InlineQueryResult> = 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<InlineQueryResult> = 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<InlineQueryResult> = 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)

View File

@ -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<Boolean> {
constructor(
inlineQueryID: InlineQueryIdentifier,
results: List<InlineQueryResult> = 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<Boolean>
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<InlineQueryResult> = emptyList(),
cachedTime: Int? = null,
isPersonal: Boolean? = null,
nextOffset: String? = null,
switchPmText: String?,
switchPmParameter: String?
) = AnswerInlineQuery(
id,
results,

View File

@ -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<InlineQueryResultsButton> {
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,
)
)
}
}

View File

@ -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"