mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
RegularTextMessageEntity and convertToFullMessageEntityList
This commit is contained in:
parent
2af4ebcd44
commit
81029db165
@ -14,6 +14,9 @@
|
||||
|
||||
* Updated built-in lengths restrictions
|
||||
* Apply restrictions of text limit for sending messages
|
||||
* Add `RegularTextMessageEntity` which is useful for representing regular text message entity
|
||||
* Add `convertToFullMessageEntityList` which create list of entities with `RegularTextMessageEntity` on places where
|
||||
must be regular text
|
||||
|
||||
### 0.8.5
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
data class RegularTextMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : MessageEntity
|
@ -1,6 +1,7 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RegularTextMessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.CaptionedMediaContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
|
||||
@ -9,21 +10,7 @@ fun createMarkdownText(
|
||||
text: String,
|
||||
messageEntities: List<MessageEntity>
|
||||
): String {
|
||||
val builder = StringBuilder()
|
||||
var offset = 0
|
||||
for (entity in messageEntities) {
|
||||
builder.append(
|
||||
text.substring(offset until entity.offset).toMarkdown()
|
||||
)
|
||||
builder.append(
|
||||
entity.asMarkdownSource
|
||||
)
|
||||
offset += entity.length
|
||||
}
|
||||
builder.append(
|
||||
text.substring(offset).toMarkdown()
|
||||
)
|
||||
return builder.toString()
|
||||
return convertToFullMessageEntityList(text, messageEntities).joinToString("", "", "") { it.asMarkdownSource }
|
||||
}
|
||||
|
||||
fun CaptionedMediaContent.toMarkdownCaption(): String? = caption ?.let {
|
||||
@ -33,7 +20,37 @@ fun CaptionedMediaContent.toMarkdownCaption(): String? = caption ?.let {
|
||||
)
|
||||
}
|
||||
|
||||
fun CaptionedMediaContent.fullEntitiesList(): List<MessageEntity> = caption ?.let {
|
||||
convertToFullMessageEntityList(it, captionEntities)
|
||||
} ?: emptyList()
|
||||
|
||||
fun TextContent.toMarkdownText(): String = createMarkdownText(
|
||||
text,
|
||||
entities
|
||||
)
|
||||
|
||||
fun TextContent.fullEntitiesList(): List<MessageEntity> = convertToFullMessageEntityList(text, entities)
|
||||
|
||||
fun convertToFullMessageEntityList(
|
||||
text: String,
|
||||
messageEntities: List<MessageEntity>
|
||||
): List<MessageEntity> {
|
||||
val result = mutableListOf<MessageEntity>()
|
||||
|
||||
var offset = 0
|
||||
for (entity in messageEntities) {
|
||||
val newEntitySize = entity.offset - offset
|
||||
if (newEntitySize > 0) {
|
||||
val regularEntity = RegularTextMessageEntity(offset, newEntitySize, text.substring(offset, entity.offset))
|
||||
result.add(regularEntity)
|
||||
offset += regularEntity.length
|
||||
}
|
||||
result.add(entity)
|
||||
offset += entity.length
|
||||
}
|
||||
val newEntitySize = text.length - offset
|
||||
if (newEntitySize > 0) {
|
||||
result.add(RegularTextMessageEntity(offset, newEntitySize, text.substring(offset, text.length)))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user