1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-12-26 16:15:52 +00:00

Compare commits

..

3 Commits

Author SHA1 Message Date
ce1abb0ae2 Merge pull request #814 from InsanusMokrassar/10.0.0
10.0.0
2024-01-12 12:45:49 +06:00
76a2cfd1a0 Merge pull request #817 from mefilt/master
Fixed incorrect link at readme
2024-01-08 16:34:38 +06:00
Mefilt
edca5494d4 Fixed incorrect link at readme 2024-01-08 18:29:59 +08:00
2 changed files with 27 additions and 47 deletions

View File

@@ -118,5 +118,5 @@ suspend fun main() {
### More examples ### More examples
You may find examples in [this project](https://github.com/InsanusMokrassar/TelegramBotAPI-examples). Besides, you are You may find examples in [this project](https://github.com/InsanusMokrassar/TelegramBotAPI-examples). Besides, you are
always welcome in our [bookstack](https://bookstack.inmo.dev/books/telegrambotapi) and always welcome in our [docs](https://docs.inmo.dev/tgbotapi/index.html) and
[chat](https://t.me/InMoTelegramBotAPIChat). [chat](https://t.me/InMoTelegramBotAPIChat).

View File

@@ -17,41 +17,23 @@ private val formatter
sealed class ChatType { sealed class ChatType {
abstract val stringified: String abstract val stringified: String
@Serializable(ChatTypeSerializer::class) @Serializable(ChatTypeSerializer::class)
object Sender : ChatType() { override val stringified = "sender" } object PrivateChatType : ChatType() { override val stringified = "private" }
@Serializable(ChatTypeSerializer::class) @Serializable(ChatTypeSerializer::class)
object Private : ChatType() { override val stringified = "private" } object GroupChatType : ChatType() { override val stringified = "group" }
@Serializable(ChatTypeSerializer::class) @Serializable(ChatTypeSerializer::class)
object Group : ChatType() { override val stringified = "group" } object SupergroupChatType : ChatType() { override val stringified = "supergroup" }
@Serializable(ChatTypeSerializer::class) @Serializable(ChatTypeSerializer::class)
object Supergroup : ChatType() { override val stringified = "supergroup" } object ChannelChatType : ChatType() { override val stringified = "channel" }
@Serializable(ChatTypeSerializer::class) @Serializable(ChatTypeSerializer::class)
object Channel : ChatType() { override val stringified = "channel" } class UnknownChatType(override val stringified: String) : ChatType()
@Serializable(ChatTypeSerializer::class)
class Unknown(override val stringified: String) : ChatType()
companion object {
@Deprecated("Renamed", ReplaceWith("Private", "dev.inmo.tgbotapi.types.chat.ChatType.Private"))
val PrivateChatType = Private
@Deprecated("Renamed", ReplaceWith("Group", "dev.inmo.tgbotapi.types.chat.ChatType.Group"))
val GroupChatType = Group
@Deprecated("Renamed", ReplaceWith("Supergroup", "dev.inmo.tgbotapi.types.chat.ChatType.Supergroup"))
val SupergroupChatType = Supergroup
@Deprecated("Renamed", ReplaceWith("Channel", "dev.inmo.tgbotapi.types.chat.ChatType.Channel"))
val ChannelChatType = Channel
@Deprecated("Renamed", ReplaceWith("Unknown", "dev.inmo.tgbotapi.types.chat.ChatType.Unknown"))
val UnknownChatType = Unknown
@Deprecated("Renamed", ReplaceWith("Unknown(stringified)", "dev.inmo.tgbotapi.types.chat.ChatType.Unknown"))
fun UnknownChatType(stringified: String) = Unknown(stringified)
}
} }
val String.asChatType val String.asChatType
get() = when (this) { get() = when (this) {
ChatType.Sender.stringified -> ChatType.Sender ChatType.PrivateChatType.stringified -> ChatType.PrivateChatType
ChatType.Private.stringified -> ChatType.Private ChatType.GroupChatType.stringified -> ChatType.GroupChatType
ChatType.Group.stringified -> ChatType.Group ChatType.SupergroupChatType.stringified -> ChatType.SupergroupChatType
ChatType.Supergroup.stringified -> ChatType.Supergroup ChatType.ChannelChatType.stringified -> ChatType.ChannelChatType
ChatType.Channel.stringified -> ChatType.Channel else -> ChatType.UnknownChatType(this)
else -> ChatType.Unknown(this)
} }
@RiskFeature @RiskFeature
@@ -81,16 +63,15 @@ object ChatSerializer : KSerializer<Chat> {
val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true
when (type) { when (type) {
ChatType.Sender -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
ChatType.Private -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson)
ChatType.Group -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) ChatType.SupergroupChatType -> if (isForum) {
ChatType.Supergroup -> if (isForum) {
formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson) formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson)
} else { } else {
formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson)
} }
ChatType.Channel -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson)
is ChatType.Unknown -> 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(),
decodedJson decodedJson
@@ -120,16 +101,15 @@ object PreviewChatSerializer : KSerializer<PreviewChat> {
val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true
return when (type) { return when (type) {
ChatType.Sender -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
ChatType.Private -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson)
ChatType.Group -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) ChatType.SupergroupChatType -> if (isForum) {
ChatType.Supergroup -> if (isForum) {
formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson) formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson)
} else { } else {
formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson)
} }
ChatType.Channel -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson)
is ChatType.Unknown -> 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(),
decodedJson decodedJson
@@ -163,16 +143,16 @@ sealed class ExtendedChatSerializer : KSerializer<ExtendedChat> {
val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true
return when (type) { return when (type) {
ChatType.Sender -> formatter.decodeFromJsonElement(ExtendedPrivateChatImpl.serializer(), decodedJson) // else -> throw IllegalArgumentException("Unknown type of chat")
ChatType.Private -> formatter.decodeFromJsonElement(ExtendedPrivateChatImpl.serializer(), decodedJson) ChatType.PrivateChatType -> formatter.decodeFromJsonElement(ExtendedPrivateChatImpl.serializer(), decodedJson)
ChatType.Group -> formatter.decodeFromJsonElement(ExtendedGroupChatImpl.serializer(), decodedJson) ChatType.GroupChatType -> formatter.decodeFromJsonElement(ExtendedGroupChatImpl.serializer(), decodedJson)
ChatType.Supergroup -> if (isForum) { ChatType.SupergroupChatType -> if (isForum) {
formatter.decodeFromJsonElement(ExtendedForumChatImpl.serializer(), decodedJson) formatter.decodeFromJsonElement(ExtendedForumChatImpl.serializer(), decodedJson)
} else { } else {
formatter.decodeFromJsonElement(ExtendedSupergroupChatImpl.serializer(), decodedJson) formatter.decodeFromJsonElement(ExtendedSupergroupChatImpl.serializer(), decodedJson)
} }
ChatType.Channel -> formatter.decodeFromJsonElement(ExtendedChannelChatImpl.serializer(), decodedJson) ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ExtendedChannelChatImpl.serializer(), decodedJson)
is ChatType.Unknown -> UnknownExtendedChat( is ChatType.UnknownChatType -> UnknownExtendedChat(
formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(),
decodedJson.toString(), decodedJson.toString(),
decodedJson decodedJson