diff --git a/CHANGELOG.md b/CHANGELOG.md index 841d33b4b8..ee9812fae7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,12 @@ mistake - don't hesitate to say this.** ### 0.22.1 MediaContent#asInputMedia * All `MediaContent` instances now can create their `InputMedia` analog +* New annotation `PreviewFeature` was added to mark new thing as preview for the time +while they can work incorrectly +* Added links utils: + * `makeLinkToMessage` have two signatures - for direct creating using username and for abstract creating using + chat id + * `makeFileLink` is unsafe way to create file link ## 0.21.0 TelegramBotAPI 4.5 diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Annotations.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Annotations.kt new file mode 100644 index 0000000000..c717bd2781 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Annotations.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import kotlin.Experimental.* + +@Experimental(Level.WARNING) +annotation class PreviewFeature diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/LinksFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/LinksFormatting.kt new file mode 100644 index 0000000000..3b62bea1f1 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/LinksFormatting.kt @@ -0,0 +1,40 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.UsernameChat +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.* + +private const val internalLinkBeginning = "https://t.me" + +@PreviewFeature +fun makeLinkToMessage( + username: String, + messageId: MessageIdentifier +): String = "$internalLinkBeginning/$username/$messageId" + +private val linkIdRedundantPartRegex = Regex("^-100") + +@PreviewFeature +fun makeLinkToMessage( + chat: ExtendedChat, + messageId: MessageIdentifier +): String? { + return when { + chat is UsernameChat && chat.username != null -> "$internalLinkBeginning/${chat.username}/$messageId" + chat !is PrivateChat -> chat.id.chatId.toString().replace( + linkIdRedundantPartRegex, + "" + ).let { bareId -> + "$internalLinkBeginning/c/$bareId/$messageId" + } + else -> return null + } +} + +@PreviewFeature +fun makeFileLink( + botToken: String, + filePath: String +) = "https://api.telegram.org/file/bot$botToken/$filePath"