1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-11-27 01:35:47 +00:00

potentially finally add support of business chats input

This commit is contained in:
2024-04-16 22:04:24 +06:00
parent c1f40c1030
commit 64ef0fa160
10 changed files with 251 additions and 33 deletions

View File

@@ -34,13 +34,13 @@ sealed interface IdChatIdentifier : ChatIdentifier {
get() = null
companion object {
operator fun invoke(chatId: RawChatId) = ChatId(chatId)
operator fun invoke(chatId: RawChatId, threadId: MessageThreadId?) = threadId ?.let {
operator fun invoke(chatId: RawChatId, threadId: MessageThreadId? = null, businessConnectionId: BusinessConnectionId? = null) = threadId ?.let {
ChatIdWithThreadId(chatId, threadId)
} ?: ChatId(chatId)
operator fun invoke(chatId: RawChatId, businessConnectionId: BusinessConnectionId?) = businessConnectionId ?.let {
} ?: businessConnectionId ?.let {
BusinessChatId(chatId, businessConnectionId)
} ?: ChatId(chatId)
operator fun invoke(chatId: RawChatId, threadId: MessageThreadId) = ChatIdWithThreadId(chatId, threadId)
operator fun invoke(chatId: RawChatId, businessConnectionId: BusinessConnectionId) = BusinessChatId(chatId, businessConnectionId)
}
}

View File

@@ -63,8 +63,9 @@ object ChatSerializer : KSerializer<Chat> {
return try {
formatter.decodeFromJsonElement(ExtendedChatSerializer, decodedJson)
} catch (e: SerializationException) {
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson")
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType
val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true
val original = decodedJson[originField]
when (type) {
ChatType.Sender -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
@@ -81,6 +82,12 @@ object ChatSerializer : KSerializer<Chat> {
decodedJson.toString(),
decodedJson
)
null -> {
when {
original != null -> formatter.decodeFromJsonElement(BusinessChatImpl.serializer(), decodedJson)
else -> error("Field $typeField must be presented for common types (excluding Business chats), but absent in $decodedJson")
}
}
}
}
}
@@ -102,19 +109,13 @@ object PreviewChatSerializer : KSerializer<PreviewChat> {
override fun deserialize(decoder: Decoder): PreviewChat {
val decodedJson = JsonObject.serializer().deserialize(decoder)
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson")
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType
val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true
val isBusiness = decodedJson[chatIdField] ?.jsonPrimitive ?.contentOrNull ?.contains("//") == true
val original = decodedJson[originField]
return when (type) {
ChatType.Sender -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
ChatType.Private -> {
if (isBusiness) {
formatter.decodeFromJsonElement(BusinessChatImpl.serializer(), decodedJson)
} else {
formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
}
}
ChatType.Private -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
ChatType.Group -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson)
ChatType.Supergroup -> if (isForum) {
formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson)
@@ -127,6 +128,12 @@ object PreviewChatSerializer : KSerializer<PreviewChat> {
decodedJson.toString(),
decodedJson
)
null -> {
when {
original != null -> formatter.decodeFromJsonElement(PreviewBusinessChat.serializer(), decodedJson)
else -> error("Field $typeField must be presented for common types (excluding Business chats), but absent in $decodedJson")
}
}
}
}
@@ -153,8 +160,9 @@ sealed class ExtendedChatSerializer : KSerializer<ExtendedChat> {
override fun deserialize(decoder: Decoder): ExtendedChat {
val decodedJson = JsonObject.serializer().deserialize(decoder)
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson")
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType
val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true
val original = decodedJson[originField]
return when (type) {
ChatType.Sender -> formatter.decodeFromJsonElement(ExtendedPrivateChatImpl.serializer(), decodedJson)
@@ -171,11 +179,18 @@ sealed class ExtendedChatSerializer : KSerializer<ExtendedChat> {
decodedJson.toString(),
decodedJson
)
null -> {
when {
original != null -> formatter.decodeFromJsonElement(ExtendedBusinessChatImpl.serializer(), decodedJson)
else -> error("Field $typeField must be presented for common types (excluding Business chats), but absent in $decodedJson")
}
}
}
}
override fun serialize(encoder: Encoder, value: ExtendedChat) {
when (value) {
is ExtendedBusinessChatImpl -> ExtendedBusinessChatImpl.serializer().serialize(encoder, value)
is ExtendedPrivateChatImpl -> ExtendedPrivateChatImpl.serializer().serialize(encoder, value)
is ExtendedGroupChatImpl -> ExtendedGroupChatImpl.serializer().serialize(encoder, value)
is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, value)
@@ -203,8 +218,9 @@ sealed class ExtendedChatSerializer : KSerializer<ExtendedChat> {
override fun deserialize(decoder: Decoder): ExtendedChat {
return super.deserialize(decoder).let {
if (it is ExtendedPrivateChatImpl) {
it.copy(
id = (it.id as? BusinessChatId) ?: BusinessChatId(it.id.chatId, businessConnectionId)
ExtendedBusinessChatImpl(
BusinessChatId(it.id.chatId, businessConnectionId),
it
)
} else {
it

View File

@@ -263,6 +263,7 @@ data class ExtendedForumChatImpl(
@Serializable
data class ExtendedBot(
@SerialName(idField)
override val id: UserId,
@SerialName(firstNameField)
override val firstName: String,
@@ -291,6 +292,14 @@ data class ExtendedBot(
private val isBot = true
}
@Serializable
data class ExtendedBusinessChatImpl(
@SerialName(idField)
override val id: BusinessChatId,
@SerialName(originField)
override val original: ExtendedPrivateChat
) : ExtendedBusinessChat, ExtendedChat by original
data class UnknownExtendedChat(
override val id: IdChatIdentifier,
val raw: String,

View File

@@ -35,7 +35,7 @@ data class PrivateChatImpl(
data class BusinessChatImpl(
@SerialName(idField)
override val id: BusinessChatId,
@SerialName(firstNameField)
@SerialName(originField)
override val original: PreviewPrivateChat
) : PreviewBusinessChat

View File

@@ -15,7 +15,7 @@ import dev.inmo.tgbotapi.types.message.content.MessageContent
data class BusinessContentMessageImpl<T: MessageContent>(
override val messageId: MessageId,
override val from: User,
override val chat: PreviewPrivateChat,
override val chat: PreviewBusinessChat,
override val businessConnectionId: BusinessConnectionId,
override val content: T,
override val date: DateTime,
@@ -31,7 +31,7 @@ data class BusinessContentMessageImpl<T: MessageContent>(
constructor(
messageId: MessageId,
from: User,
chat: PreviewPrivateChat,
chat: PreviewBusinessChat,
businessConnectionId: BusinessConnectionId,
content: T,
date: DateTime,

View File

@@ -533,7 +533,10 @@ internal data class RawMessage(
BusinessContentMessageImpl(
messageId,
from ?: error("Was detected common message, but owner (sender) of the message was not found"),
chat,
BusinessChatImpl(
chat.id.toBusinessChatId(business_connection_id),
chat
),
business_connection_id,
content,
date.asDate,

View File

@@ -3,12 +3,13 @@ package dev.inmo.tgbotapi.types.message.abstracts
import dev.inmo.tgbotapi.abstracts.types.WithBusinessConnectionId
import dev.inmo.tgbotapi.types.business_connection.BusinessConnectionId
import dev.inmo.tgbotapi.types.chat.PreviewBot
import dev.inmo.tgbotapi.types.chat.PreviewBusinessChat
import dev.inmo.tgbotapi.types.chat.PreviewPrivateChat
import dev.inmo.tgbotapi.types.message.content.MessageContent
interface BusinessContentMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T>, FromUserMessage,
WithBusinessConnectionId {
override val chat: PreviewPrivateChat
override val chat: PreviewBusinessChat
override val businessConnectionId: BusinessConnectionId
val senderBusinessBot: PreviewBot?
}