1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

add TextQuote

This commit is contained in:
InsanusMokrassar 2024-01-07 20:51:29 +06:00
parent 955e9ca871
commit f637b480b1
14 changed files with 100 additions and 32 deletions

View File

@ -665,3 +665,4 @@ const val winnersField = "winners"
const val additionalChatCountField = "additional_chat_count" const val additionalChatCountField = "additional_chat_count"
const val unclaimedPrizeCountField = "unclaimed_prize_count" const val unclaimedPrizeCountField = "unclaimed_prize_count"
const val wasRefundedField = "was_refunded" const val wasRefundedField = "was_refunded"
const val isManualField = "is_manual"

View File

@ -0,0 +1,38 @@
package dev.inmo.tgbotapi.types
import dev.inmo.tgbotapi.abstracts.TextedInput
import dev.inmo.tgbotapi.types.message.RawMessageEntities
import dev.inmo.tgbotapi.types.message.asTextSources
import dev.inmo.tgbotapi.types.message.textsources.TextSource
import dev.inmo.tgbotapi.types.message.toRawMessageEntities
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmName
@Serializable
data class TextQuote private constructor(
@SerialName(textField)
override val text: String,
@SerialName(positionField)
val position: Int,
@SerialName(entitiesField)
private val entities: RawMessageEntities? = null,
@SerialName(isManualField)
val isManual: Boolean = false
) : TextedInput {
override val textSources: List<TextSource> by lazy {
entities ?.asTextSources(text) ?: emptyList()
}
companion object {
@JvmName("PublicConstructor")
operator fun invoke(
text: String,
position: Int,
textSources: List<TextSource> = emptyList(),
isManual: Boolean = false
) = TextQuote(
text, position, textSources.toRawMessageEntities(position), isManual
)
}
}

View File

@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.types.abstracts
import dev.inmo.tgbotapi.types.TextQuote
interface WithOptionalQuoteInfo {
val quote: TextQuote?
}

View File

@ -55,6 +55,7 @@ internal data class RawMessage(
private val is_automatic_forward: Boolean? = null, private val is_automatic_forward: Boolean? = null,
private val reply_to_message: RawMessage? = null, private val reply_to_message: RawMessage? = null,
private val external_reply: ReplyInfo.External? = null, private val external_reply: ReplyInfo.External? = null,
private val quote: TextQuote? = null,
private val via_bot: CommonBot? = null, private val via_bot: CommonBot? = null,
private val edit_date: TelegramDate? = null, private val edit_date: TelegramDate? = null,
private val has_protected_content: Boolean? = null, private val has_protected_content: Boolean? = null,
@ -148,40 +149,46 @@ internal data class RawMessage(
messageId, messageId,
story story
) )
text != null -> TextContent(text, (entities ?: emptyList()).asTextSources(text), link_preview_options) text != null -> TextContent(text, (entities ?: emptyList()).asTextSources(text), link_preview_options, quote)
audio != null -> AudioContent( audio != null -> AudioContent(
audio, audio,
caption, caption,
adaptedCaptionEntities adaptedCaptionEntities,
quote
) )
video != null -> VideoContent( video != null -> VideoContent(
video, video,
caption, caption,
adaptedCaptionEntities, adaptedCaptionEntities,
has_media_spoiler ?: false has_media_spoiler ?: false,
quote
) )
animation != null -> AnimationContent( animation != null -> AnimationContent(
animation, animation,
document, document,
caption, caption,
adaptedCaptionEntities, adaptedCaptionEntities,
has_media_spoiler ?: false has_media_spoiler ?: false,
quote
) )
document != null -> DocumentContent( document != null -> DocumentContent(
document, document,
caption, caption,
adaptedCaptionEntities adaptedCaptionEntities,
quote
) )
voice != null -> VoiceContent( voice != null -> VoiceContent(
voice, voice,
caption, caption,
adaptedCaptionEntities adaptedCaptionEntities,
quote
) )
photo != null -> PhotoContent( photo != null -> PhotoContent(
photo, photo,
caption, caption,
adaptedCaptionEntities, adaptedCaptionEntities,
has_media_spoiler ?: false has_media_spoiler ?: false,
quote
) )
sticker != null -> StickerContent(sticker) sticker != null -> StickerContent(sticker)
dice != null -> DiceContent(dice) dice != null -> DiceContent(dice)

View File

@ -1,9 +1,12 @@
package dev.inmo.tgbotapi.types.message package dev.inmo.tgbotapi.types.message
import dev.inmo.micro_utils.serialization.mapper.MapperSerializer
import dev.inmo.tgbotapi.types.CustomEmojiId import dev.inmo.tgbotapi.types.CustomEmojiId
import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.chat.User
import dev.inmo.tgbotapi.types.message.textsources.* import dev.inmo.tgbotapi.types.message.textsources.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
@Serializable @Serializable
internal data class RawMessageEntity( internal data class RawMessageEntity(

View File

@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.types.message.content
import dev.inmo.tgbotapi.abstracts.TextedInput import dev.inmo.tgbotapi.abstracts.TextedInput
import dev.inmo.tgbotapi.types.MediaGroupIdentifier import dev.inmo.tgbotapi.types.MediaGroupIdentifier
import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.abstracts.WithOptionalQuoteInfo
import dev.inmo.tgbotapi.types.files.AudioFile import dev.inmo.tgbotapi.types.files.AudioFile
import dev.inmo.tgbotapi.types.files.DocumentFile import dev.inmo.tgbotapi.types.files.DocumentFile
import dev.inmo.tgbotapi.types.media.* import dev.inmo.tgbotapi.types.media.*
@ -22,7 +23,7 @@ sealed interface DocumentMediaGroupPartContent : MediaGroupPartContent {
override fun toMediaGroupMemberTelegramMedia(): DocumentMediaGroupMemberTelegramMedia override fun toMediaGroupMemberTelegramMedia(): DocumentMediaGroupMemberTelegramMedia
} }
sealed interface TextedMediaContent : TextedContent, MediaContent sealed interface TextedMediaContent : TextedContent, MediaContent, WithOptionalQuoteInfo
sealed interface MediaGroupCollectionContent<T : MediaGroupPartContent> : TextedMediaContent { sealed interface MediaGroupCollectionContent<T : MediaGroupPartContent> : TextedMediaContent {
@Serializable @Serializable

View File

@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.types.media.TelegramMediaAnimation
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.TextQuote
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.AnimationFile import dev.inmo.tgbotapi.types.files.AnimationFile
import dev.inmo.tgbotapi.types.files.DocumentFile import dev.inmo.tgbotapi.types.files.DocumentFile
@ -19,7 +20,8 @@ data class AnimationContent(
val includedDocument: DocumentFile?, val includedDocument: DocumentFile?,
override val text: String?, override val text: String?,
override val textSources: TextSourcesList = emptyList(), override val textSources: TextSourcesList = emptyList(),
override val spoilered: Boolean = false override val spoilered: Boolean = false,
override val quote: TextQuote? = null
) : TextedMediaContent, SpoilerableMediaContent { ) : TextedMediaContent, SpoilerableMediaContent {
override fun createResend( override fun createResend(
chatId: ChatIdentifier, chatId: ChatIdentifier,

View File

@ -8,6 +8,7 @@ import dev.inmo.tgbotapi.types.media.toTelegramMediaAudio
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.TextQuote
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.AudioFile import dev.inmo.tgbotapi.types.files.AudioFile
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
@ -17,7 +18,8 @@ import kotlinx.serialization.Serializable
data class AudioContent( data class AudioContent(
override val media: AudioFile, override val media: AudioFile,
override val text: String? = null, override val text: String? = null,
override val textSources: TextSourcesList = emptyList() override val textSources: TextSourcesList = emptyList(),
override val quote: TextQuote? = null
) : AudioMediaGroupPartContent { ) : AudioMediaGroupPartContent {
override fun createResend( override fun createResend(
chatId: ChatIdentifier, chatId: ChatIdentifier,

View File

@ -9,6 +9,8 @@ import dev.inmo.tgbotapi.types.media.toTelegramMediaDocument
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.TextQuote
import dev.inmo.tgbotapi.types.abstracts.WithOptionalQuoteInfo
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.DocumentFile import dev.inmo.tgbotapi.types.files.DocumentFile
import dev.inmo.tgbotapi.types.files.asDocumentFile import dev.inmo.tgbotapi.types.files.asDocumentFile
@ -19,7 +21,8 @@ import kotlinx.serialization.Serializable
data class DocumentContent( data class DocumentContent(
override val media: DocumentFile, override val media: DocumentFile,
override val text: String? = null, override val text: String? = null,
override val textSources: TextSourcesList = emptyList() override val textSources: TextSourcesList = emptyList(),
override val quote: TextQuote? = null
) : DocumentMediaGroupPartContent { ) : DocumentMediaGroupPartContent {
override fun createResend( override fun createResend(
chatId: ChatIdentifier, chatId: ChatIdentifier,
@ -52,7 +55,8 @@ inline fun MediaContent.asDocumentContent() = when (this) {
is TextedInput -> DocumentContent( is TextedInput -> DocumentContent(
media.asDocumentFile(), media.asDocumentFile(),
text, text,
textSources textSources,
(this as? WithOptionalQuoteInfo) ?.quote
) )
else -> DocumentContent( else -> DocumentContent(
media.asDocumentFile() media.asDocumentFile()

View File

@ -2,10 +2,8 @@ package dev.inmo.tgbotapi.types.message.content
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.send.media.SendMediaGroup import dev.inmo.tgbotapi.requests.send.media.SendMediaGroup
import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.MediaGroupIdentifier import dev.inmo.tgbotapi.types.abstracts.WithOptionalQuoteInfo
import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.TelegramMediaFile import dev.inmo.tgbotapi.types.files.TelegramMediaFile
import dev.inmo.tgbotapi.types.media.TelegramMedia import dev.inmo.tgbotapi.types.media.TelegramMedia
@ -16,10 +14,12 @@ import kotlinx.serialization.Serializable
@Serializable @Serializable
data class MediaGroupContent<T : MediaGroupPartContent>( data class MediaGroupContent<T : MediaGroupPartContent>(
override val group: List<MediaGroupCollectionContent.PartWrapper<T>>, override val group: List<MediaGroupCollectionContent.PartWrapper<T>>,
override val mediaGroupId: MediaGroupIdentifier override val mediaGroupId: MediaGroupIdentifier,
) : MediaGroupCollectionContent<T> { ) : MediaGroupCollectionContent<T>, WithOptionalQuoteInfo {
val mainContent: MediaGroupPartContent val mainContent: MediaGroupPartContent
get() = group.first().content get() = group.first().content
override val quote: TextQuote?
get() = mainContent.quote
override val media: TelegramMediaFile override val media: TelegramMediaFile
get() = mainContent.media get() = mainContent.media

View File

@ -8,6 +8,8 @@ import dev.inmo.tgbotapi.types.media.toTelegramMediaPhoto
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.TextQuote
import dev.inmo.tgbotapi.types.abstracts.WithOptionalQuoteInfo
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.* import dev.inmo.tgbotapi.types.files.*
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
@ -18,8 +20,9 @@ data class PhotoContent(
override val mediaCollection: Photo, override val mediaCollection: Photo,
override val text: String? = null, override val text: String? = null,
override val textSources: TextSourcesList = emptyList(), override val textSources: TextSourcesList = emptyList(),
override val spoilered: Boolean = false override val spoilered: Boolean = false,
) : MediaCollectionContent<PhotoSize>, VisualMediaGroupPartContent { override val quote: TextQuote? = null
) : MediaCollectionContent<PhotoSize>, VisualMediaGroupPartContent, WithOptionalQuoteInfo {
override val media: PhotoSize = mediaCollection.biggest() ?: throw IllegalStateException("Can't locate any photo size for this content") override val media: PhotoSize = mediaCollection.biggest() ?: throw IllegalStateException("Can't locate any photo size for this content")
override fun createResend( override fun createResend(

View File

@ -3,11 +3,9 @@ package dev.inmo.tgbotapi.types.message.content
import dev.inmo.tgbotapi.abstracts.TextedInput import dev.inmo.tgbotapi.abstracts.TextedInput
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.send.SendTextMessage import dev.inmo.tgbotapi.requests.send.SendTextMessage
import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.LinkPreviewOptions
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.abstracts.WithOptionalQuoteInfo
import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -16,8 +14,9 @@ import kotlinx.serialization.Serializable
data class TextContent( data class TextContent(
override val text: String, override val text: String,
override val textSources: TextSourcesList = emptyList(), override val textSources: TextSourcesList = emptyList(),
val linkPreviewOptions: LinkPreviewOptions? = null val linkPreviewOptions: LinkPreviewOptions? = null,
) : TextedContent { override val quote: TextQuote? = null
) : TextedContent, WithOptionalQuoteInfo {
override fun createResend( override fun createResend(
chatId: ChatIdentifier, chatId: ChatIdentifier,
messageThreadId: MessageThreadId?, messageThreadId: MessageThreadId?,

View File

@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.types.media.TelegramMediaVideo
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.TextQuote
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.VideoFile import dev.inmo.tgbotapi.types.files.VideoFile
import dev.inmo.tgbotapi.types.files.toTelegramMediaVideo import dev.inmo.tgbotapi.types.files.toTelegramMediaVideo
@ -18,7 +19,8 @@ data class VideoContent(
override val media: VideoFile, override val media: VideoFile,
override val text: String? = null, override val text: String? = null,
override val textSources: TextSourcesList = emptyList(), override val textSources: TextSourcesList = emptyList(),
override val spoilered: Boolean = false override val spoilered: Boolean = false,
override val quote: TextQuote? = null
) : VisualMediaGroupPartContent { ) : VisualMediaGroupPartContent {
override fun createResend( override fun createResend(
chatId: ChatIdentifier, chatId: ChatIdentifier,

View File

@ -2,22 +2,21 @@ package dev.inmo.tgbotapi.types.message.content
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.send.media.SendVoice import dev.inmo.tgbotapi.requests.send.media.SendVoice
import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.media.TelegramMediaAudio import dev.inmo.tgbotapi.types.media.TelegramMediaAudio
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.abstracts.WithOptionalQuoteInfo
import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.VoiceFile import dev.inmo.tgbotapi.types.files.VoiceFile
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.threadId
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class VoiceContent( data class VoiceContent(
override val media: VoiceFile, override val media: VoiceFile,
override val text: String? = null, override val text: String? = null,
override val textSources: TextSourcesList = emptyList() override val textSources: TextSourcesList = emptyList(),
override val quote: TextQuote? = null
) : TextedMediaContent { ) : TextedMediaContent {
override fun createResend( override fun createResend(
chatId: ChatIdentifier, chatId: ChatIdentifier,