1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-16 13:49:26 +00:00

add support of custom emojis

This commit is contained in:
2022-08-12 23:21:53 +06:00
parent 5a4a6d5710
commit c3668e978b
8 changed files with 99 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
package dev.inmo.tgbotapi.types
import dev.inmo.tgbotapi.utils.BuiltinMimeTypes
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline
typealias Identifier = Long
typealias MessageIdentifier = Long
@@ -28,6 +30,11 @@ typealias GooglePlaceId = String
typealias GooglePlaceType = String
typealias MembersLimit = Int
typealias WebAppQueryId = String
@Serializable
@JvmInline
value class CustomEmojiId(
val string: String
)
typealias Seconds = Int
typealias MilliSeconds = Long

View File

@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.message
import dev.inmo.tgbotapi.types.CustomEmojiId
import dev.inmo.tgbotapi.types.chat.User
import dev.inmo.tgbotapi.types.message.textsources.*
import kotlinx.serialization.Serializable
@@ -11,7 +12,8 @@ internal data class RawMessageEntity(
val length: Int,
val url: String? = null,
val user: User? = null,
val language: String? = null
val language: String? = null,
val custom_emoji_id: CustomEmojiId? = null
) {
internal val range by lazy {
offset until (offset + length)
@@ -50,6 +52,7 @@ internal fun RawMessageEntity.asTextSource(
"underline" -> UnderlineTextSource(sourceSubstring, subPartsWithRegulars)
"strikethrough" -> StrikethroughTextSource(sourceSubstring, subPartsWithRegulars)
"spoiler" -> SpoilerTextSource(sourceSubstring, subPartsWithRegulars)
"custom_emoji" -> CustomEmojiTextSource(sourceSubstring, custom_emoji_id ?: error("For custom emoji custom_emoji_id should exists"), subPartsWithRegulars)
else -> RegularTextSource(sourceSubstring)
}
}
@@ -158,7 +161,8 @@ internal fun TextSource.toRawMessageEntities(offset: Int = 0): List<RawMessageEn
is UnderlineTextSource -> RawMessageEntity("underline", offset, length)
is StrikethroughTextSource -> RawMessageEntity("strikethrough", offset, length)
is SpoilerTextSource -> RawMessageEntity("spoiler", offset, length)
else -> null
is CustomEmojiTextSource -> RawMessageEntity("custom_emoji", offset, length)
is RegularTextSource -> null
}
) + if (this is MultilevelTextSource) {
subsources.toRawMessageEntities(offset)

View File

@@ -0,0 +1,31 @@
package dev.inmo.tgbotapi.types.message.textsources
import dev.inmo.tgbotapi.types.CustomEmojiId
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.extensions.makeString
import dev.inmo.tgbotapi.utils.internal.*
import kotlinx.serialization.Serializable
/**
* @see customEmoji
*/
@Serializable
data class CustomEmojiTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor (
override val source: String,
val customEmojiId: CustomEmojiId,
override val subsources: TextSourcesList
) : MultilevelTextSource {
override val markdown: String by lazy { source.customEmojiMarkdown() }
override val markdownV2: String by lazy { source.customEmojiMarkdownV2() }
override val html: String by lazy { source.customEmojiHTML() }
}
@Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_API_USAGE")
inline fun customEmoji(emojiId: CustomEmojiId, parts: TextSourcesList) = CustomEmojiTextSource(parts.makeString(), emojiId, parts)
@Suppress("NOTHING_TO_INLINE")
inline fun customEmoji(emojiId: CustomEmojiId, vararg parts: TextSource) = customEmoji(emojiId, parts.toList())
/**
* Without sharp (#)
*/
@Suppress("NOTHING_TO_INLINE")
inline fun customEmoji(emojiId: CustomEmojiId, text: String) = customEmoji(emojiId, regular(text))

View File

@@ -21,6 +21,7 @@ private val baseSerializers: Map<String, KSerializer<out TextSource>> = mapOf(
"hashtag" to HashTagTextSource.serializer(),
"cashtag" to CashTagTextSource.serializer(),
"spoiler" to SpoilerTextSource.serializer(),
"custom_emoji" to CustomEmojiTextSource.serializer(),
)
object TextSourceSerializer : TypedSerializer<TextSource>(TextSource::class, baseSerializers) {

View File

@@ -119,6 +119,10 @@ internal fun String.commandMarkdown(): String = command(String::toMarkdown)
internal fun String.commandMarkdownV2(): String = command(String::escapeMarkdownV2Common)
internal fun String.commandHTML(): String = command(String::toHtml)
internal fun String.customEmojiMarkdown(): String = toMarkdown()
internal fun String.customEmojiMarkdownV2(): String = escapeMarkdownV2Common()
internal fun String.customEmojiHTML(): String = toHtml()
internal fun String.regularMarkdown(): String = toMarkdown()
internal fun String.regularMarkdownV2(): String = escapeMarkdownV2Common()
internal fun String.regularHtml(): String = toHtml()