1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-26 10:37:13 +00:00
This commit is contained in:
InsanusMokrassar 2020-11-06 01:14:48 +06:00
parent e45f9cf46a
commit 00a75801a8
4 changed files with 48 additions and 24 deletions

View File

@ -13,8 +13,6 @@ import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
import kotlinx.serialization.*
private val ResultDeserializer = TelegramBotAPIMessageDeserializationStrategyClass<ContentMessage<*>>()
fun CopyMessage(
fromChatId: ChatIdentifier,
toChatId: ChatIdentifier,
@ -60,8 +58,8 @@ data class CopyMessage internal constructor(
override val allowSendingWithoutReply: Boolean? = null,
@SerialName(replyMarkupField)
override val replyMarkup: KeyboardMarkup? = null
): SimpleRequest<ContentMessage<*>>,
ReplyingMarkupSendMessageRequest<ContentMessage<*>>,
): SimpleRequest<MessageIdentifier>,
ReplyingMarkupSendMessageRequest<MessageIdentifier>,
MessageAction,
TextedOutput {
override val chatId: ChatIdentifier
@ -72,8 +70,8 @@ data class CopyMessage internal constructor(
override fun method(): String = "copyMessage"
override val resultDeserializer: DeserializationStrategy<ContentMessage<*>>
get() = ResultDeserializer
override val resultDeserializer: DeserializationStrategy<MessageIdentifier>
get() = MessageIdSerializer
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@ -84,22 +84,23 @@ internal fun createTextPart(from: String, entities: RawMessageEntities): List<Te
internal fun List<TextPart>.asRawMessageEntities(): List<RawMessageEntity> = mapNotNull {
val source = it.source
val length = it.range.last - it.range.first + 1
when (source) {
is MentionTextSource -> RawMessageEntity("mention", it.range.first, it.range.last - it.range.first)
is HashTagTextSource -> RawMessageEntity("hashtag", it.range.first, it.range.last - it.range.first)
is CashTagTextSource -> RawMessageEntity("cashtag", it.range.first, it.range.last - it.range.first)
is BotCommandTextSource -> RawMessageEntity("bot_command", it.range.first, it.range.last - it.range.first)
is URLTextSource -> RawMessageEntity("url", it.range.first, it.range.last - it.range.first)
is EMailTextSource -> RawMessageEntity("email", it.range.first, it.range.last - it.range.first)
is PhoneNumberTextSource -> RawMessageEntity("phone_number", it.range.first, it.range.last - it.range.first)
is BoldTextSource -> RawMessageEntity("bold", it.range.first, it.range.last - it.range.first)
is ItalicTextSource -> RawMessageEntity("italic", it.range.first, it.range.last - it.range.first)
is CodeTextSource -> RawMessageEntity("code", it.range.first, it.range.last - it.range.first)
is PreTextSource -> RawMessageEntity("pre", it.range.first, it.range.last - it.range.first, language = source.language)
is TextLinkTextSource -> RawMessageEntity("text_link", it.range.first, it.range.last - it.range.first, source.url)
is TextMentionTextSource -> RawMessageEntity("text_mention", it.range.first, it.range.last - it.range.first, user = source.user)
is UnderlineTextSource -> RawMessageEntity("underline", it.range.first, it.range.last - it.range.first)
is StrikethroughTextSource -> RawMessageEntity("strikethrough", it.range.first, it.range.last - it.range.first)
is MentionTextSource -> RawMessageEntity("mention", it.range.first, length)
is HashTagTextSource -> RawMessageEntity("hashtag", it.range.first, length)
is CashTagTextSource -> RawMessageEntity("cashtag", it.range.first, length)
is BotCommandTextSource -> RawMessageEntity("bot_command", it.range.first, length)
is URLTextSource -> RawMessageEntity("url", it.range.first, length)
is EMailTextSource -> RawMessageEntity("email", it.range.first, length)
is PhoneNumberTextSource -> RawMessageEntity("phone_number", it.range.first, length)
is BoldTextSource -> RawMessageEntity("bold", it.range.first, length)
is ItalicTextSource -> RawMessageEntity("italic", it.range.first, length)
is CodeTextSource -> RawMessageEntity("code", it.range.first, length)
is PreTextSource -> RawMessageEntity("pre", it.range.first, length, language = source.language)
is TextLinkTextSource -> RawMessageEntity("text_link", it.range.first, length, source.url)
is TextMentionTextSource -> RawMessageEntity("text_mention", it.range.first, length, user = source.user)
is UnderlineTextSource -> RawMessageEntity("underline", it.range.first, length)
is StrikethroughTextSource -> RawMessageEntity("strikethrough", it.range.first, length)
else -> null
}
}
@ -108,7 +109,7 @@ internal fun List<TextSource>.toRawMessageEntities(): List<RawMessageEntity> {
var i = 0
return map {
TextPart(
i until it.source.length,
i until (i + it.source.length),
it
).also {
i = it.range.last + 1

View File

@ -0,0 +1,24 @@
package dev.inmo.tgbotapi.types
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
object MessageIdSerializer : KSerializer<MessageIdentifier> {
override val descriptor: SerialDescriptor = JsonObject.serializer().descriptor
override fun deserialize(decoder: Decoder): MessageIdentifier = JsonObject.serializer().deserialize(decoder)[messageIdField]!!.jsonPrimitive.long
override fun serialize(encoder: Encoder, value: MessageIdentifier) {
JsonObject.serializer().serialize(
encoder,
JsonObject(
mapOf(
messageIdField to JsonPrimitive(value)
)
)
)
}
}

View File

@ -22,6 +22,7 @@ import dev.inmo.tgbotapi.types.payments.Invoice
import dev.inmo.tgbotapi.types.payments.SuccessfulPayment
import dev.inmo.tgbotapi.types.polls.Poll
import dev.inmo.tgbotapi.types.venue.Venue
import dev.inmo.tgbotapi.utils.fullListOfSubSource
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.reflect.KClass
@ -91,11 +92,11 @@ internal data class RawMessage(
) {
private val content: MessageContent? by lazy {
val adaptedCaptionEntities = caption ?.let {
caption_entities ?.asTextParts(caption)
it.fullListOfSubSource(caption_entities ?.asTextParts(caption) ?: emptyList())
} ?: emptyList()
when {
text != null -> TextContent(text, entities ?.asTextParts(text) ?: emptyList())
text != null -> TextContent(text, text.fullListOfSubSource(entities ?.asTextParts(text) ?: emptyList()))
audio != null -> AudioContent(
audio,
caption,