diff --git a/CHANGELOG.md b/CHANGELOG.md index 92812809d9..3b344fdf37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,20 @@ * `User` now have no field `userLocale` * For `Java` there is `User#javaLocale` extension function, which will give an old locale work way +### 0.20.1 + +* `User` now implement `PrivateChat` +* `TextMentionMessageEntity` now accept `PrivateChat` instead of `User` in main constructor + * `TextMentionMessageEntity` now contains not user, but contains `PrivateChat` + * Fixeed: `TextMentionMessageEntity#asHtmlSource` previously worked incorrect +* Abstraction `TextSource` + * `MessageEntity` now extends `TextSource` + * `createFormattedText` method now accept `List` + * `createHtmlText` method now accept `List` + * `createMarkdownText` method now accept `List` + * A lot of `TextSource` implementors was added. More info [here](src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/) + * All `MessageEntity` implementations now are using new `TextSource` analogues as delegates + ## 0.19.0 ImplicitReflection removing * Total rework of serialization for requests. Now all `SimpleRequest` children have: diff --git a/build.gradle b/build.gradle index 556ba50fe5..df3dce2e6c 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ plugins { id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" } -project.version = "0.20.0" +project.version = "0.20.1" project.group = "com.github.insanusmokrassar" apply from: "publish.gradle" diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt new file mode 100644 index 0000000000..6a40155809 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts + +interface TextSource { + val asMarkdownSource: String + val asHtmlSource: String +} \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt index 5336c72289..2e02d6b935 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BoldTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown @@ -7,7 +9,4 @@ data class BoldTextMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.boldMarkdown() - override val asHtmlSource: String = sourceString.boldHTML() -} +) : MessageEntity, TextSource by BoldTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt index 2454b2760f..088956a44b 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt @@ -1,19 +1,16 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.commandHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown -private val commandRegex = Regex("[/!][^@\\s]*") - data class BotCommandMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.commandMarkdown() - override val asHtmlSource: String = sourceString.commandHTML() - - val command: String by lazy { - commandRegex.find(sourceString) ?.value ?.substring(1) ?: sourceString.substring(1)// skip first symbol like "/" or "!" - } + override val sourceString: String, + private val botCommandTextSource: BotCommandTextSource = BotCommandTextSource(sourceString) +) : MessageEntity, TextSource by botCommandTextSource { + val command: String + get() = botCommandTextSource.command } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt index d5adda2106..bb45546d75 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.CodeTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown @@ -7,7 +9,4 @@ data class CodeTextMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.codeMarkdown() - override val asHtmlSource: String = sourceString.codeHTML() -} +) : MessageEntity, TextSource by CodeTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt index 4c88ce875a..f5456fbdea 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt @@ -1,13 +1,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.EMailTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown -class EMailMessageEntity( +data class EMailMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.emailMarkdown() - override val asHtmlSource: String = sourceString.emailHTML() -} +) : MessageEntity, TextSource by EMailTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt index 702a3f75a9..63733e5d8a 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.HashTagTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown @@ -7,7 +9,4 @@ data class HashTagMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.hashTagMarkdown() - override val asHtmlSource: String = sourceString.hashTagHTML() -} +) : MessageEntity, TextSource by HashTagTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt index 744dbaae9e..db25785295 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.ItalicTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown @@ -7,7 +9,4 @@ data class ItalicTextMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.italicMarkdown() - override val asHtmlSource: String = sourceString.italicHTML() -} +) : MessageEntity, TextSource by ItalicTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt index 010559e880..9886f4592e 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.MentionTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown @@ -7,7 +9,4 @@ class MentionMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.mentionMarkdown() - override val asHtmlSource: String = sourceString.mentionHTML() -} +) : MessageEntity, TextSource by MentionTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt index 5b85448cec..79a68f9997 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt @@ -1,10 +1,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity -interface MessageEntity { +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource + +interface MessageEntity : TextSource { val offset: Int val length: Int val sourceString: String - - val asMarkdownSource: String - val asHtmlSource: String } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt index 9c4433a301..79a5fb5357 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.PhoneNumberTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown @@ -7,7 +9,4 @@ data class PhoneNumberMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.phoneMarkdown() - override val asHtmlSource: String = sourceString.phoneHTML() -} +) : MessageEntity, TextSource by PhoneNumberTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt index 37424b7b4f..80943d942b 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.PreTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown @@ -7,7 +9,4 @@ data class PreTextMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.preMarkdown() - override val asHtmlSource: String = sourceString.preHTML() -} +) : MessageEntity, TextSource by PreTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt index 0c4af76680..b7e683f80a 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.RegularTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown @@ -7,7 +9,4 @@ data class RegularTextMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.toMarkdown() - override val asHtmlSource: String = sourceString.toHtml() -} +) : MessageEntity, TextSource by RegularTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt index fae946f170..576f8043bc 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.TextLinkTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown @@ -8,7 +10,4 @@ data class TextLinkMessageEntity( override val length: Int, override val sourceString: String, val url: String -) : MessageEntity { - override val asMarkdownSource: String = sourceString.linkMarkdown(url) - override val asHtmlSource: String = sourceString.linkHTML(url) -} +) : MessageEntity, TextSource by TextLinkTextSource(sourceString, url) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt index 4bbe2ea2da..ad7334e9af 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt @@ -1,14 +1,23 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.TextMentionTextSource import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat +import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown class TextMentionMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String, - val user: User -) : MessageEntity { - override val asMarkdownSource: String = sourceString.mentionMarkdown(user.id) - override val asHtmlSource: String = sourceString.mentionMarkdown(user.id) + val privateChat: PrivateChat +) : MessageEntity, TextSource by TextMentionTextSource(sourceString, privateChat) { + @Deprecated("Deprecated due to the fact that there is more common constructor") + constructor( + offset: Int, + length: Int, + sourceString: String, + user: User + ) : this(offset, length, sourceString, user as PrivateChat) } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt index d4cecba36f..482ad28fe6 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.URLTextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown @@ -7,9 +9,6 @@ data class URLMessageEntity( override val offset: Int, override val length: Int, override val sourceString: String -) : MessageEntity{ +) : MessageEntity, TextSource by URLTextSource(sourceString) { val url: String = sourceString - - override val asMarkdownSource: String = sourceString.linkMarkdown(url) - override val asHtmlSource: String = sourceString.linkHTML(url) } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt new file mode 100644 index 0000000000..9d1a1f7d7e --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown + +class BoldTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.boldMarkdown() + override val asHtmlSource: String = sourceString.boldHTML() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt new file mode 100644 index 0000000000..2c6eb4e0a3 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt @@ -0,0 +1,18 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.commandHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown + +private val commandRegex = Regex("[/!][^@\\s]*") + +class BotCommandTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.commandMarkdown() + override val asHtmlSource: String = sourceString.commandHTML() + + val command: String by lazy { + commandRegex.find(sourceString) ?.value ?.substring(1) ?: sourceString.substring(1)// skip first symbol like "/" or "!" + } +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt new file mode 100644 index 0000000000..ab0066a60b --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown + +class CodeTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.codeMarkdown() + override val asHtmlSource: String = sourceString.codeHTML() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt new file mode 100644 index 0000000000..34b5ed34ce --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown + +class EMailTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.emailMarkdown() + override val asHtmlSource: String = sourceString.emailHTML() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt new file mode 100644 index 0000000000..4894fabecf --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown + +class HashTagTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.hashTagMarkdown() + override val asHtmlSource: String = sourceString.hashTagHTML() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt new file mode 100644 index 0000000000..2e1d1170e0 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown + +class ItalicTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.italicMarkdown() + override val asHtmlSource: String = sourceString.italicHTML() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt new file mode 100644 index 0000000000..8d2b0cc71a --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown + +class MentionTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.mentionMarkdown() + override val asHtmlSource: String = sourceString.mentionHTML() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt new file mode 100644 index 0000000000..f5004fa7d5 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown + +class PhoneNumberTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.phoneMarkdown() + override val asHtmlSource: String = sourceString.phoneHTML() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt new file mode 100644 index 0000000000..7a0fea96dc --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown + +class PreTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.preMarkdown() + override val asHtmlSource: String = sourceString.preHTML() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt new file mode 100644 index 0000000000..0125346add --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown + +class RegularTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.toMarkdown() + override val asHtmlSource: String = sourceString.toHtml() +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt new file mode 100644 index 0000000000..a9e7ae225a --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt @@ -0,0 +1,13 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown + +class TextLinkTextSource( + sourceString: String, + url: String +) : TextSource { + override val asMarkdownSource: String = sourceString.linkMarkdown(url) + override val asHtmlSource: String = sourceString.linkHTML(url) +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt new file mode 100644 index 0000000000..720436e8d7 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt @@ -0,0 +1,21 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat +import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown + +class TextMentionTextSource( + sourceString: String, + privateChat: PrivateChat +) : TextSource { + @Deprecated("Deprecated due to the fact that there is more common constructor") + constructor( + sourceString: String, + user: User + ) : this(sourceString, user as PrivateChat) + + override val asMarkdownSource: String = sourceString.mentionMarkdown(privateChat.id) + override val asHtmlSource: String = sourceString.mentionHTML(privateChat.id) +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt new file mode 100644 index 0000000000..581af0f015 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown + +class URLTextSource( + sourceString: String +) : TextSource{ + override val asMarkdownSource: String = sourceString.linkMarkdown(sourceString) + override val asHtmlSource: String = sourceString.linkHTML(sourceString) +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt index d3c99ba12a..7e2e291df5 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt @@ -1,19 +1,20 @@ package com.github.insanusmokrassar.TelegramBotAPI.types +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @Serializable data class User( - val id: ChatId, + override val id: ChatId, @SerialName(isBotField) val isBot: Boolean = false, @SerialName(firstNameField) - val firstName: String, + override val firstName: String, @SerialName(lastNameField) - val lastName: String? = null, + override val lastName: String = "", @SerialName(usernameField) - val username: Username? = null, + override val username: Username? = null, @SerialName(languageCodeField) val languageCode: String? = null -) +) : PrivateChat diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt index 2a4584a44a..5eca85548f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RegularTextMessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* @@ -37,7 +38,7 @@ fun convertToFullMessageEntityList( } fun createFormattedText( - entities: List, + entities: List, partLength: Int = 4096, mode: ParseMode = MarkdownParseMode ): List { diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt index 7a8bc212ce..a36b52bee5 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength @@ -8,7 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextCont import com.github.insanusmokrassar.TelegramBotAPI.types.textLength fun createHtmlText( - entities: List, + entities: List, partLength: Int = 4096 ): List = createFormattedText(entities, partLength, HTMLParseMode) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt index 30d32aa32e..c506670702 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength @@ -8,7 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextCont import com.github.insanusmokrassar.TelegramBotAPI.types.textLength fun createMarkdownText( - entities: List, + entities: List, partLength: Int = 4096 ): List = createFormattedText(entities, partLength, MarkdownParseMode)