1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 12:08:43 +00:00

include chat_type

This commit is contained in:
InsanusMokrassar 2021-04-26 20:42:26 +06:00
parent 6a3588bb8d
commit 694bec22a2
7 changed files with 78 additions and 20 deletions

View File

@ -319,6 +319,7 @@ const val scaleField = "scale"
const val maxTipAmountField = "max_tip_amount" const val maxTipAmountField = "max_tip_amount"
const val suggestedTipAmountsField = "suggested_tip_amounts" const val suggestedTipAmountsField = "suggested_tip_amounts"
const val chatTypeField = "chat_type"
const val explanationEntitiesField = "explanation_entities" const val explanationEntitiesField = "explanation_entities"
const val explanationParseModeField = "explanation_parse_mode" const val explanationParseModeField = "explanation_parse_mode"

View File

@ -2,10 +2,12 @@ package dev.inmo.tgbotapi.types.InlineQueries.abstracts
import dev.inmo.tgbotapi.types.InlineQueryIdentifier import dev.inmo.tgbotapi.types.InlineQueryIdentifier
import dev.inmo.tgbotapi.types.User import dev.inmo.tgbotapi.types.User
import dev.inmo.tgbotapi.types.chat.ChatType
interface InlineQuery { interface InlineQuery {
val id: InlineQueryIdentifier val id: InlineQueryIdentifier
val from: User val from: User
val query: String val query: String
val offset: String val offset: String
val chatType: ChatType?
} }

View File

@ -3,10 +3,12 @@ package dev.inmo.tgbotapi.types.InlineQueries.query
import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery
import dev.inmo.tgbotapi.types.InlineQueryIdentifier import dev.inmo.tgbotapi.types.InlineQueryIdentifier
import dev.inmo.tgbotapi.types.User import dev.inmo.tgbotapi.types.User
import dev.inmo.tgbotapi.types.chat.ChatType
data class BaseInlineQuery( data class BaseInlineQuery(
override val id: InlineQueryIdentifier, override val id: InlineQueryIdentifier,
override val from: User, override val from: User,
override val query: String, override val query: String,
override val offset: String override val offset: String,
override val chatType: ChatType?
) : InlineQuery ) : InlineQuery

View File

@ -3,12 +3,15 @@ package dev.inmo.tgbotapi.types.InlineQueries.query
import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery
import dev.inmo.tgbotapi.types.InlineQueryIdentifier import dev.inmo.tgbotapi.types.InlineQueryIdentifier
import dev.inmo.tgbotapi.types.User import dev.inmo.tgbotapi.types.User
import dev.inmo.tgbotapi.types.chat.ChatType
import dev.inmo.tgbotapi.types.location.Location import dev.inmo.tgbotapi.types.location.Location
import kotlinx.serialization.Serializable
data class LocationInlineQuery( data class LocationInlineQuery(
override val id: InlineQueryIdentifier, override val id: InlineQueryIdentifier,
override val from: User, override val from: User,
override val query: String, override val query: String,
override val offset: String, override val offset: String,
override val chatType: ChatType?,
val location: Location val location: Location
) : InlineQuery ) : InlineQuery

View File

@ -1,9 +1,10 @@
package dev.inmo.tgbotapi.types.InlineQueries.query package dev.inmo.tgbotapi.types.InlineQueries.query
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.ChatType
import dev.inmo.tgbotapi.types.chat.ChatTypeSerializer
import dev.inmo.tgbotapi.types.location.Location import dev.inmo.tgbotapi.types.location.Location
import kotlinx.serialization.SerialName import kotlinx.serialization.*
import kotlinx.serialization.Serializable
@Serializable @Serializable
internal data class RawInlineQuery( internal data class RawInlineQuery(
@ -15,12 +16,15 @@ internal data class RawInlineQuery(
val query: String, val query: String,
@SerialName(offsetField) @SerialName(offsetField)
val offset: String, val offset: String,
@SerialName(chatTypeField)
@Serializable(ChatTypeSerializer::class)
val chatType: ChatType? = null,
@SerialName(locationField) @SerialName(locationField)
val location: Location? = null val location: Location? = null
) { ) {
val asInlineQuery by lazy { val asInlineQuery by lazy {
location ?.let { location ?.let {
LocationInlineQuery(id, from, query, offset, location) LocationInlineQuery(id, from, query, offset, chatType, location)
} ?: BaseInlineQuery(id, from, query, offset) } ?: BaseInlineQuery(id, from, query, offset, chatType)
} }
} }

View File

@ -4,10 +4,10 @@ import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.abstracts.Chat import dev.inmo.tgbotapi.types.chat.abstracts.Chat
import dev.inmo.tgbotapi.types.chat.abstracts.UnknownChatType import dev.inmo.tgbotapi.types.chat.abstracts.UnknownChatType
import dev.inmo.tgbotapi.types.chat.abstracts.extended.ExtendedChat import dev.inmo.tgbotapi.types.chat.abstracts.extended.ExtendedChat
import dev.inmo.tgbotapi.types.chat.abstracts.extended.UnknownExtendedChat
import dev.inmo.tgbotapi.types.chat.extended.* import dev.inmo.tgbotapi.types.chat.extended.*
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.* import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
@ -17,6 +17,40 @@ import kotlinx.serialization.json.*
private val formatter private val formatter
get() = nonstrictJsonFormat get() = nonstrictJsonFormat
@Serializable(ChatTypeSerializer::class)
sealed class ChatType {
abstract val stringified: String
@Serializable(ChatTypeSerializer::class)
object PrivateChatType : ChatType() { override val stringified = "private" }
@Serializable(ChatTypeSerializer::class)
object GroupChatType : ChatType() { override val stringified = "group" }
@Serializable(ChatTypeSerializer::class)
object SupergroupChatType : ChatType() { override val stringified = "supergroup" }
@Serializable(ChatTypeSerializer::class)
object ChannelChatType : ChatType() { override val stringified = "channel" }
@Serializable(ChatTypeSerializer::class)
class UnknownChatType(override val stringified: String) : ChatType()
}
val String.asChatType
get() = when (this) {
ChatType.PrivateChatType.stringified -> ChatType.PrivateChatType
ChatType.GroupChatType.stringified -> ChatType.GroupChatType
ChatType.SupergroupChatType.stringified -> ChatType.SupergroupChatType
ChatType.ChannelChatType.stringified -> ChatType.ChannelChatType
else -> ChatType.UnknownChatType(this)
}
@Serializer(ChatType::class)
object ChatTypeSerializer : KSerializer<ChatType> {
override val descriptor: SerialDescriptor = String.serializer().descriptor
override fun deserialize(decoder: Decoder): ChatType {
return decoder.decodeString().asChatType
}
override fun serialize(encoder: Encoder, value: ChatType) {
encoder.encodeString(value.stringified)
}
}
internal object PreviewChatSerializer : KSerializer<Chat> { internal object PreviewChatSerializer : KSerializer<Chat> {
@InternalSerializationApi @InternalSerializationApi
override val descriptor: SerialDescriptor = buildSerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN) override val descriptor: SerialDescriptor = buildSerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN)
@ -24,14 +58,14 @@ internal object PreviewChatSerializer : KSerializer<Chat> {
override fun deserialize(decoder: Decoder): Chat { override fun deserialize(decoder: Decoder): Chat {
val decodedJson = JsonObject.serializer().deserialize(decoder) val decodedJson = JsonObject.serializer().deserialize(decoder)
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?: error("Field $typeField must be presented, but absent in $decodedJson") val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson")
return when (type) { return when (type) {
"private" -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
"group" -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson)
"supergroup" -> formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) ChatType.SupergroupChatType -> formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson)
"channel" -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson)
else -> UnknownChatType( is ChatType.UnknownChatType -> UnknownChatType(
formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(),
decodedJson.toString() decodedJson.toString()
) )
@ -56,14 +90,18 @@ internal object ExtendedChatSerializer : KSerializer<ExtendedChat> {
override fun deserialize(decoder: Decoder): ExtendedChat { override fun deserialize(decoder: Decoder): ExtendedChat {
val decodedJson = JsonObject.serializer().deserialize(decoder) val decodedJson = JsonObject.serializer().deserialize(decoder)
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?: error("Field $typeField must be presented, but absent in $decodedJson") val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson")
return when (type) { return when (type) {
"private" -> formatter.decodeFromJsonElement(ExtendedPrivateChatImpl.serializer(), decodedJson) // else -> throw IllegalArgumentException("Unknown type of chat")
"group" -> formatter.decodeFromJsonElement(ExtendedGroupChatImpl.serializer(), decodedJson) ChatType.PrivateChatType -> formatter.decodeFromJsonElement(ExtendedPrivateChatImpl.serializer(), decodedJson)
"supergroup" -> formatter.decodeFromJsonElement(ExtendedSupergroupChatImpl.serializer(), decodedJson) ChatType.GroupChatType -> formatter.decodeFromJsonElement(ExtendedGroupChatImpl.serializer(), decodedJson)
"channel" -> formatter.decodeFromJsonElement(ExtendedChannelChatImpl.serializer(), decodedJson) ChatType.SupergroupChatType -> formatter.decodeFromJsonElement(ExtendedSupergroupChatImpl.serializer(), decodedJson)
else -> throw IllegalArgumentException("Unknown type of chat") ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ExtendedChannelChatImpl.serializer(), decodedJson)
is ChatType.UnknownChatType -> UnknownExtendedChat(
formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(),
decodedJson.toString()
)
} }
} }

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.chat.abstracts.extended package dev.inmo.tgbotapi.types.chat.abstracts.extended
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.ChatPhoto import dev.inmo.tgbotapi.types.ChatPhoto
import dev.inmo.tgbotapi.types.chat.ExtendedChatSerializer import dev.inmo.tgbotapi.types.chat.ExtendedChatSerializer
import dev.inmo.tgbotapi.types.chat.abstracts.Chat import dev.inmo.tgbotapi.types.chat.abstracts.Chat
@ -8,4 +9,11 @@ import kotlinx.serialization.Serializable
@Serializable(ExtendedChatSerializer::class) @Serializable(ExtendedChatSerializer::class)
interface ExtendedChat : Chat { interface ExtendedChat : Chat {
val chatPhoto: ChatPhoto? val chatPhoto: ChatPhoto?
} }
data class UnknownExtendedChat(
override val id: ChatId,
val raw: String
) : ExtendedChat {
override val chatPhoto: ChatPhoto? = null
}