diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/CashTagTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/CashTagTextSource.kt
index ed61c49489..cd1a809282 100644
--- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/CashTagTextSource.kt
+++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/CashTagTextSource.kt
@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.message.textsources
+import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.extensions.makeString
import dev.inmo.tgbotapi.utils.internal.*
@@ -13,6 +14,12 @@ data class CashTagTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
override val source: String,
override val subsources: TextSourcesList
) : MultilevelTextSource {
+ val username: Username? by lazy {
+ val potentialUsername = source.dropWhile { it != '@' }
+ if (potentialUsername.isEmpty()) return@lazy null
+
+ Username(potentialUsername)
+ }
override val markdown: String by lazy { source.cashTagMarkdown() }
override val markdownV2: String by lazy { cashTagMarkdownV2() }
override val html: String by lazy { cashTagHTML() }
diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/HashTagTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/HashTagTextSource.kt
index 09a042aa1d..1548b7cc54 100644
--- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/HashTagTextSource.kt
+++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/HashTagTextSource.kt
@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.message.textsources
+import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.extensions.makeString
import dev.inmo.tgbotapi.utils.internal.*
@@ -13,6 +14,13 @@ data class HashTagTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
override val source: String,
override val subsources: TextSourcesList
) : MultilevelTextSource {
+ val username: Username? by lazy {
+ val potentialUsername = source.dropWhile { it != '@' }
+ if (potentialUsername.isEmpty()) return@lazy null
+
+ Username(potentialUsername)
+ }
+
override val markdown: String by lazy { source.hashTagMarkdown() }
override val markdownV2: String by lazy { hashTagMarkdownV2() }
override val html: String by lazy { hashTagHTML() }
diff --git a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/EntitiesTestText.kt b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/EntitiesTestText.kt
index 5ed7366ca3..50c39b7343 100644
--- a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/EntitiesTestText.kt
+++ b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/EntitiesTestText.kt
@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.MessageEntity
+import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.types.message.RawMessageEntity
import dev.inmo.tgbotapi.types.message.asTextSources
import dev.inmo.tgbotapi.types.message.textsources.*
@@ -8,14 +9,14 @@ import dev.inmo.tgbotapi.utils.extensions.makeSourceString
import kotlin.test.assertEquals
import kotlin.test.assertTrue
-const val testText = "It (is?) is simple hello world with #tag and @mention. Start of blockquote: Block quotation started\n" +
+const val testText = "It (is?) is simple hello world with #tag@sample and @mention. Start of blockquote: Block quotation started\n" +
"Block quotation continued\n" +
"The last line of the block quotation\n" +
". Start of expandable blockquote: Block quotation started\n" +
"Block quotation continued\n" +
"The last line of the block quotation"
-const val formattedV2Text = "It \\(is?\\) *_is_ ~__simple__~* ||hello world|| with \\#tag and @mention\\. Start of blockquote: >Block quotation started\n>Block quotation continued\n>The last line of the block quotation\n\\. Start of expandable blockquote: **>Block quotation started\n>Block quotation continued\n>The last line of the block quotation||"
-const val formattedHtmlText = "It (is?) is simple hello world with #tag and @mention. Start of blockquote:
Block quotation started\nBlock quotation continued\nThe last line of the block quotation\n. Start of expandable blockquote:
Block quotation started\nBlock quotation continued\nThe last line of the block quotation" +const val formattedV2Text = "It \\(is?\\) *_is_ ~__simple__~* ||hello world|| with \\#tag@sample and @mention\\. Start of blockquote: >Block quotation started\n>Block quotation continued\n>The last line of the block quotation\n\\. Start of expandable blockquote: **>Block quotation started\n>Block quotation continued\n>The last line of the block quotation||" +const val formattedHtmlText = "It (is?) is
Block quotation started\nBlock quotation continued\nThe last line of the block quotation\n. Start of expandable blockquote:
Block quotation started\nBlock quotation continued\nThe last line of the block quotation" internal val testTextEntities = listOf( RawMessageEntity( "bold", @@ -45,21 +46,21 @@ internal val testTextEntities = listOf( RawMessageEntity( "hashtag", 36, - 4 + 11 ), RawMessageEntity( "mention", - 45, + 52, 8 ), RawMessageEntity( "blockquote", - 76, + 83, 86 ), RawMessageEntity( "expandable_blockquote", - 197, + 204, 86 ) ) @@ -71,6 +72,7 @@ fun TextSourcesList.testTextSources() { assertTrue (get(3) is SpoilerTextSource) assertTrue (get(4) is RegularTextSource) assertTrue (get(5) is HashTagTextSource) + assertEquals(Username("@sample"), (get(5) as HashTagTextSource).username) assertTrue (get(6) is RegularTextSource) assertTrue (get(7) is MentionTextSource) assertTrue (get(8) is RegularTextSource) diff --git a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt index d62fe47014..063e20a185 100644 --- a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt +++ b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt @@ -46,7 +46,7 @@ class StringFormattingTests { " " + spoiler("hello world") + " with " + - hashtag("tag") + + hashtag("tag@sample") + " and " + mention("mention") + ". Start of blockquote: " +