From 6eb7563c7036fdd87b453f18485628cfdfb6469c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Sep 2025 17:15:59 +0600 Subject: [PATCH] add support of is_paid_post field --- .../message/ChannelContentMessageImpl.kt | 2 +- .../types/message/ChannelPaidPostImpl.kt | 54 ++++++++++++++++++ .../inmo/tgbotapi/types/message/RawMessage.kt | 55 +++++++++++++------ .../message/abstracts/ChannelPaidPost.kt | 8 +++ .../tgbotapi/extensions/utils/WithContent.kt | 3 + 5 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelPaidPostImpl.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChannelPaidPost.kt diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelContentMessageImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelContentMessageImpl.kt index 0e77380bfb..89d42be394 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelContentMessageImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelContentMessageImpl.kt @@ -28,9 +28,9 @@ data class ChannelContentMessageImpl( override val fromOffline: Boolean, @SerialName(paidMessageStarCountField) override val cost: Int? = null, +) : ChannelContentMessage { @SerialName(isPaidPostField) override val isPaidPost: Boolean = false -) : ChannelContentMessage { constructor( messageId: MessageId, chat: PreviewChannelChat, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelPaidPostImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelPaidPostImpl.kt new file mode 100644 index 0000000000..8aa31930c6 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelPaidPostImpl.kt @@ -0,0 +1,54 @@ +package dev.inmo.tgbotapi.types.message + +import korlibs.time.DateTime +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup +import dev.inmo.tgbotapi.types.chat.CommonBot +import dev.inmo.tgbotapi.types.chat.PreviewChannelChat +import dev.inmo.tgbotapi.types.chat.PreviewChat +import dev.inmo.tgbotapi.types.chat.User +import dev.inmo.tgbotapi.types.message.abstracts.* +import dev.inmo.tgbotapi.types.message.content.MessageContent +import kotlinx.serialization.EncodeDefault +import kotlinx.serialization.SerialName + +data class ChannelPaidPostImpl( + override val messageId: MessageId, + override val chat: PreviewChannelChat, + override val senderChat: PreviewChat, + override val content: T, + override val date: DateTime, + override val editDate: DateTime?, + override val hasProtectedContent: Boolean, + override val forwardOrigin: MessageOrigin?, + override val replyInfo: ReplyInfo?, + override val replyMarkup: InlineKeyboardMarkup?, + override val senderBot: CommonBot?, + override val authorSignature: AuthorSignature?, + override val mediaGroupId: MediaGroupId?, + override val fromOffline: Boolean, + @SerialName(paidMessageStarCountField) + override val cost: Int? = null, +) : ChannelPaidPost { + @SerialName(isPaidPostField) + @EncodeDefault + override val isPaidPost: Boolean = true + constructor( + messageId: MessageId, + chat: PreviewChannelChat, + senderChat: PreviewChat, + content: T, + date: DateTime, + editDate: DateTime?, + hasProtectedContent: Boolean, + forwardInfo: ForwardInfo, + replyTo: AccessibleMessage?, + replyMarkup: InlineKeyboardMarkup?, + senderBot: CommonBot?, + authorSignature: AuthorSignature?, + mediaGroupId: MediaGroupId?, + fromOffline: Boolean, + ) : this( + messageId, chat, senderChat, content, date, editDate, hasProtectedContent, forwardInfo.messageOrigin(), replyTo ?.let { ReplyInfo.Internal(it) }, replyMarkup, senderBot, authorSignature, mediaGroupId, fromOffline + ) +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt index 0058480203..151add37eb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt @@ -400,24 +400,43 @@ internal data class RawMessage( } when (chat) { is PreviewPublicChat -> when (chat) { - is PreviewChannelChat -> ChannelContentMessageImpl( - messageId = messageId, - chat = chat, - senderChat = checkedFrom ?: sender_chat ?: chat, - content = content, - date = date.asDate, - editDate = edit_date?.asDate, - hasProtectedContent = has_protected_content == true, - forwardOrigin = forward_origin, - replyInfo = replyInfo, - replyMarkup = reply_markup, - senderBot = via_bot, - authorSignature = author_signature, - mediaGroupId = media_group_id, - fromOffline = is_from_offline, - cost = paid_star_count, - isPaidPost = is_paid_post, - ) + is PreviewChannelChat -> if (is_paid_post) { + ChannelContentMessageImpl( + messageId = messageId, + chat = chat, + senderChat = checkedFrom ?: sender_chat ?: chat, + content = content, + date = date.asDate, + editDate = edit_date?.asDate, + hasProtectedContent = has_protected_content == true, + forwardOrigin = forward_origin, + replyInfo = replyInfo, + replyMarkup = reply_markup, + senderBot = via_bot, + authorSignature = author_signature, + mediaGroupId = media_group_id, + fromOffline = is_from_offline, + cost = paid_star_count, + ) + } else { + ChannelPaidPostImpl( + messageId = messageId, + chat = chat, + senderChat = checkedFrom ?: sender_chat ?: chat, + content = content, + date = date.asDate, + editDate = edit_date?.asDate, + hasProtectedContent = has_protected_content == true, + forwardOrigin = forward_origin, + replyInfo = replyInfo, + replyMarkup = reply_markup, + senderBot = via_bot, + authorSignature = author_signature, + mediaGroupId = media_group_id, + fromOffline = is_from_offline, + cost = paid_star_count, + ) + } is PreviewForumChat -> when(chat) { is PreviewChannelDirectMessagesChat -> { if (messageThreadId != null) { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChannelPaidPost.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChannelPaidPost.kt new file mode 100644 index 0000000000..0434929220 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChannelPaidPost.kt @@ -0,0 +1,8 @@ +package dev.inmo.tgbotapi.types.message.abstracts + +import dev.inmo.tgbotapi.types.message.content.MessageContent + +interface ChannelPaidPost : ChannelContentMessage { + override val isPaidPost: Boolean + get() = true +} diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt index 702d80d396..58c5e26bc5 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt @@ -20,6 +20,9 @@ inline fun PossiblySentViaBotCommonMessage<*>.requi inline fun ChannelContentMessage<*>.withContent() = if (content is T) { this as ChannelContentMessage } else { null } inline fun ChannelContentMessage<*>.requireWithContent() = withContent()!! +inline fun ChannelPaidPost<*>.withContent() = if (content is T) { this as ChannelPaidPost } else { null } +inline fun ChannelPaidPost<*>.requireWithContent() = withContent()!! + inline fun PrivateContentMessage<*>.withContent() = if (content is T) { this as PrivateContentMessage } else { null } inline fun PrivateContentMessage<*>.requireWithContent() = withContent()!!