mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
UnknownUpdate -> UnknownUpdateType, unknown types for messages and chats
This commit is contained in:
parent
a8ca45a4bd
commit
83e5d40443
@ -80,6 +80,10 @@ while they can work incorrectly
|
||||
### 0.22.2 CashTag and independent updates handling
|
||||
|
||||
* `cashtag` entity type was added
|
||||
* Several `Unknown*` realizations was added:
|
||||
* `UnknownUpdateType`
|
||||
* `UnknownMessageType`
|
||||
* `UnknownChatType`
|
||||
* New type of updates was added: `UnknownUpdate`. It will be used in cases when type of update is unknown in system
|
||||
|
||||
## 0.21.0 TelegramBotAPI 4.5
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.UnknownChatType
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.typeField
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.LongSerializer
|
||||
import kotlinx.serialization.internal.StringDescriptor
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObjectSerializer
|
||||
@ -24,7 +26,10 @@ internal object PreviewChatSerializer : KSerializer<Chat> {
|
||||
"group" -> formatter.fromJson(GroupChatImpl.serializer(), decodedJson)
|
||||
"supergroup" -> formatter.fromJson(SupergroupChatImpl.serializer(), decodedJson)
|
||||
"channel" -> formatter.fromJson(ChannelChatImpl.serializer(), decodedJson)
|
||||
else -> throw IllegalArgumentException("Unknown type of chat")
|
||||
else -> UnknownChatType(
|
||||
formatter.fromJson(LongSerializer, decodedJson.getPrimitive(chatIdField)).toChatId(),
|
||||
decodedJson.toString()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,4 +7,9 @@ import kotlinx.serialization.Serializable
|
||||
@Serializable(PreviewChatSerializer::class)
|
||||
interface Chat {
|
||||
val id: ChatId
|
||||
}
|
||||
}
|
||||
|
||||
data class UnknownChatType(
|
||||
override val id: ChatId,
|
||||
val raw: String
|
||||
) : Chat
|
||||
|
@ -10,6 +10,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.games.RawGame
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.abstracts.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.UnknownMessageType
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.*
|
||||
@ -184,91 +185,99 @@ internal data class RawMessage(
|
||||
|
||||
|
||||
val asMessage: Message by lazy {
|
||||
chatEvent ?.let {
|
||||
chatEvent ->
|
||||
when (chat) {
|
||||
is SupergroupChat -> SupergroupEventMessage(
|
||||
messageId,
|
||||
chat,
|
||||
chatEvent as? SupergroupEvent ?: throwWrongChatEvent(SupergroupEvent::class, chatEvent),
|
||||
date.asDate
|
||||
)
|
||||
is GroupChat -> GroupEventMessage(
|
||||
messageId,
|
||||
chat,
|
||||
chatEvent as? GroupEvent ?: throwWrongChatEvent(GroupChat::class, chatEvent),
|
||||
date.asDate
|
||||
)
|
||||
is ChannelChat -> ChannelEventMessage(
|
||||
messageId,
|
||||
chat,
|
||||
chatEvent as? ChannelEvent ?: throwWrongChatEvent(ChannelEvent::class, chatEvent),
|
||||
date.asDate
|
||||
)
|
||||
else -> throw IllegalStateException("Expected one of the public chats, but was $chat (in extracting of chat event message)")
|
||||
}
|
||||
} ?: content ?.let {
|
||||
content ->
|
||||
media_group_id ?.let {
|
||||
when (from) {
|
||||
null -> ChannelMediaGroupMessage(
|
||||
try {
|
||||
chatEvent?.let { chatEvent ->
|
||||
when (chat) {
|
||||
is SupergroupChat -> SupergroupEventMessage(
|
||||
messageId,
|
||||
chat,
|
||||
date.asDate,
|
||||
it,
|
||||
when (content) {
|
||||
is PhotoContent -> content
|
||||
is VideoContent -> content
|
||||
else -> throw IllegalStateException("Unsupported content for media group")
|
||||
},
|
||||
edit_date ?.asDate,
|
||||
forwarded,
|
||||
reply_to_message ?.asMessage,
|
||||
reply_markup
|
||||
chatEvent as? SupergroupEvent ?: throwWrongChatEvent(SupergroupEvent::class, chatEvent),
|
||||
date.asDate
|
||||
)
|
||||
else -> CommonMediaGroupMessage(
|
||||
is GroupChat -> GroupEventMessage(
|
||||
messageId,
|
||||
from,
|
||||
chat,
|
||||
chatEvent as? GroupEvent ?: throwWrongChatEvent(GroupChat::class, chatEvent),
|
||||
date.asDate
|
||||
)
|
||||
is ChannelChat -> ChannelEventMessage(
|
||||
messageId,
|
||||
chat,
|
||||
chatEvent as? ChannelEvent ?: throwWrongChatEvent(ChannelEvent::class, chatEvent),
|
||||
date.asDate
|
||||
)
|
||||
else -> throw IllegalStateException("Expected one of the public chats, but was $chat (in extracting of chat event message)")
|
||||
}
|
||||
} ?: content?.let { content ->
|
||||
media_group_id?.let {
|
||||
when (from) {
|
||||
null -> ChannelMediaGroupMessage(
|
||||
messageId,
|
||||
chat,
|
||||
date.asDate,
|
||||
it,
|
||||
when (content) {
|
||||
is PhotoContent -> content
|
||||
is VideoContent -> content
|
||||
else -> throw IllegalStateException("Unsupported content for media group")
|
||||
},
|
||||
edit_date?.asDate,
|
||||
forwarded,
|
||||
reply_to_message?.asMessage,
|
||||
reply_markup
|
||||
)
|
||||
else -> CommonMediaGroupMessage(
|
||||
messageId,
|
||||
from,
|
||||
chat,
|
||||
date.asDate,
|
||||
it,
|
||||
when (content) {
|
||||
is PhotoContent -> content
|
||||
is VideoContent -> content
|
||||
else -> throw IllegalStateException("Unsupported content for media group")
|
||||
},
|
||||
edit_date?.asDate,
|
||||
forwarded,
|
||||
reply_to_message?.asMessage,
|
||||
reply_markup
|
||||
)
|
||||
}
|
||||
} ?: when (chat) {
|
||||
is ChannelChat -> ChannelMessage(
|
||||
messageId,
|
||||
chat,
|
||||
content,
|
||||
date.asDate,
|
||||
it,
|
||||
when (content) {
|
||||
is PhotoContent -> content
|
||||
is VideoContent -> content
|
||||
else -> throw IllegalStateException("Unsupported content for media group")
|
||||
},
|
||||
edit_date ?.asDate,
|
||||
edit_date?.asDate,
|
||||
forwarded,
|
||||
reply_to_message ?.asMessage,
|
||||
reply_markup
|
||||
reply_to_message?.asMessage,
|
||||
reply_markup,
|
||||
author_signature
|
||||
)
|
||||
else -> CommonMessageImpl(
|
||||
messageId,
|
||||
from
|
||||
?: throw IllegalStateException("Was detected common message, but owner (sender) of the message was not found"),
|
||||
chat,
|
||||
content,
|
||||
date.asDate,
|
||||
edit_date?.asDate,
|
||||
forwarded,
|
||||
reply_to_message?.asMessage,
|
||||
reply_markup,
|
||||
paymentInfo
|
||||
)
|
||||
}
|
||||
} ?: when (chat) {
|
||||
is ChannelChat -> ChannelMessage(
|
||||
messageId,
|
||||
chat,
|
||||
content,
|
||||
date.asDate,
|
||||
edit_date ?.asDate,
|
||||
forwarded,
|
||||
reply_to_message ?.asMessage,
|
||||
reply_markup,
|
||||
author_signature
|
||||
)
|
||||
else -> CommonMessageImpl(
|
||||
messageId,
|
||||
from ?: throw IllegalStateException("Was detected common message, but owner (sender) of the message was not found"),
|
||||
chat,
|
||||
content,
|
||||
date.asDate,
|
||||
edit_date ?.asDate,
|
||||
forwarded,
|
||||
reply_to_message ?.asMessage,
|
||||
reply_markup,
|
||||
paymentInfo
|
||||
)
|
||||
}
|
||||
} ?: throw IllegalStateException("Was not found supported type of data")
|
||||
} ?: throw IllegalStateException("Was not found supported type of data")
|
||||
} catch (e: Exception) {
|
||||
UnknownMessageType(
|
||||
messageId,
|
||||
chat,
|
||||
date.asDate,
|
||||
e
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun throwWrongChatEvent(expected: KClass<*>, but: ChatEvent): CommonEvent {
|
||||
|
@ -13,6 +13,13 @@ interface Message {
|
||||
val date: DateTime
|
||||
}
|
||||
|
||||
data class UnknownMessageType(
|
||||
override val messageId: MessageIdentifier,
|
||||
override val chat: Chat,
|
||||
override val date: DateTime,
|
||||
val insideException: Exception
|
||||
) : Message
|
||||
|
||||
internal class TelegramBotAPIMessageDeserializationStrategyClass<T> : DeserializationStrategy<T> {
|
||||
override val descriptor: SerialDescriptor = StringDescriptor.withName("TelegramBotAPIMessageSerializer")
|
||||
|
||||
|
@ -9,7 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Telegr
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField
|
||||
import kotlinx.serialization.SerialName
|
||||
@ -51,7 +51,7 @@ internal data class RawUpdate constructor(
|
||||
shipping_query != null -> ShippingQueryUpdate(updateId, shipping_query)
|
||||
pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query)
|
||||
poll != null -> PollUpdate(updateId, poll)
|
||||
else -> UnknownUpdate(
|
||||
else -> UnknownUpdateType(
|
||||
updateId,
|
||||
raw
|
||||
)
|
||||
|
@ -12,7 +12,7 @@ interface Update {
|
||||
val data: Any
|
||||
}
|
||||
|
||||
data class UnknownUpdate(
|
||||
data class UnknownUpdateType(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: String
|
||||
) : Update
|
||||
|
Loading…
Reference in New Issue
Block a user