1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

separated User object and additional fields for Bot representation

This commit is contained in:
InsanusMokrassar 2020-01-23 19:08:02 +06:00
parent 8687a2ba6b
commit a8ae0a296a
4 changed files with 64 additions and 9 deletions

View File

@ -26,6 +26,10 @@
* `UnknownKeyboardButtonPollType`
* `RegularKeyboardButtonPollType`
* `QuizKeyboardButtonPollType`
* `User` now is sealed class
* `CommonUser` was added as representation of default `User`
* `Bot` was added as representation of bot user
* `GetMe` now return `Bot` object
## 0.22.0

View File

@ -1,14 +1,15 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.Bot
import com.github.insanusmokrassar.TelegramBotAPI.types.User
import kotlinx.serialization.*
@Serializable
class GetMe : SimpleRequest<User> {
class GetMe : SimpleRequest<Bot> {
override fun method(): String = "getMe"
override val resultDeserializer: DeserializationStrategy<User>
get() = User.serializer()
override val resultDeserializer: DeserializationStrategy<Bot>
get() = Bot.serializer()
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@ -57,6 +57,9 @@ const val isBotField = "is_bot"
const val firstNameField = "first_name"
const val lastNameField = "last_name"
const val languageCodeField = "language_code"
const val canJoinGroupsField = "can_join_groups"
const val canReadAllGroupMessagesField = "can_read_all_group_messages"
const val supportInlineQueriesField = "supports_inline_queries"
const val textEntitiesField = "text_entities"
const val stickerSetNameField = "set_name"
const val stickerSetNameFullField = "sticker_set_name"

View File

@ -1,14 +1,16 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObjectSerializer
@Serializable
data class User(
sealed class User : PrivateChat
@Serializable
data class CommonUser(
override val id: ChatId,
@SerialName(isBotField)
val isBot: Boolean = false,
@SerialName(firstNameField)
override val firstName: String,
@SerialName(lastNameField)
@ -17,4 +19,49 @@ data class User(
override val username: Username? = null,
@SerialName(languageCodeField)
val languageCode: String? = null
) : PrivateChat
) : User()
@Serializable
data class Bot(
override val id: ChatId,
@SerialName(firstNameField)
override val firstName: String,
@SerialName(lastNameField)
override val lastName: String = "",
@SerialName(usernameField)
override val username: Username? = null,
@SerialName(canJoinGroupsField)
val canJoinGroups: Boolean = false,
@SerialName(canReadAllGroupMessagesField)
val canReadAllGroupMessages: Boolean = false,
@SerialName(supportInlineQueriesField)
val supportsInlineQueries: Boolean = false
) : User() {
@SerialName(isBotField)
private val isBot = true
}
@Serializer(User::class)
internal object UserSerializer : KSerializer<User> {
override fun deserialize(decoder: Decoder): User {
val asJson = JsonObjectSerializer.deserialize(decoder)
return when {
asJson.getPrimitiveOrNull(isBotField) ?.booleanOrNull != true -> Json.nonstrict.fromJson(
CommonUser.serializer(),
asJson
)
else -> Json.nonstrict.fromJson(
Bot.serializer(),
asJson
)
}
}
override fun serialize(encoder: Encoder, obj: User) {
when (obj) {
is CommonUser -> CommonUser.serializer().serialize(encoder, obj)
is Bot -> Bot.serializer().serialize(encoder, obj)
}
}
}