mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-23 00:57:13 +00:00
reworking of links extension
This commit is contained in:
parent
af1d6d6f1a
commit
6650dd9cfe
@ -10,9 +10,7 @@ import kotlinx.serialization.encoding.Encoder
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
@Serializable(UserSerializer::class)
|
||||
sealed class User : PrivateChat {
|
||||
abstract override val id: UserId
|
||||
}
|
||||
sealed class User : PrivateChat
|
||||
|
||||
@Serializable
|
||||
data class CommonUser(
|
||||
|
@ -1,10 +1,13 @@
|
||||
package dev.inmo.tgbotapi.types.chat.abstracts
|
||||
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.UserId
|
||||
import dev.inmo.tgbotapi.types.chat.PreviewChatSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable(PreviewChatSerializer::class)
|
||||
interface PrivateChat : Chat, UsernameChat {
|
||||
override val id: UserId
|
||||
val firstName: String
|
||||
val lastName: String
|
||||
}
|
||||
|
@ -1,13 +1,20 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.formatting
|
||||
|
||||
import dev.inmo.tgbotapi.types.*
|
||||
import dev.inmo.tgbotapi.types.MessageEntity.textsources.link
|
||||
import dev.inmo.tgbotapi.types.ParseMode.*
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.PrivateChat
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.UsernameChat
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.*
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.extended.ExtendedChat
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.extended.ExtendedPublicChat
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
||||
|
||||
private const val internalLinkBeginning = "https://t.me"
|
||||
|
||||
fun makeLink(username: String) = "$internalLinkBeginning/$username"
|
||||
inline val Username.link
|
||||
get() = makeLink(username)
|
||||
inline fun makeLink(username: Username) = username.link
|
||||
|
||||
fun makeLinkToMessage(
|
||||
username: String,
|
||||
messageId: MessageIdentifier
|
||||
@ -24,8 +31,12 @@ fun makeLinkToMessage(
|
||||
private val linkIdRedundantPartRegex = Regex("^-100")
|
||||
private val usernameBeginSymbolRegex = Regex("^@")
|
||||
|
||||
/**
|
||||
* Link which can be used as by any user to get access to [Message]. Returns null in case when there are no
|
||||
* known way to build link (for [PrivateChat]s, for example)
|
||||
*/
|
||||
fun makeLinkToMessage(
|
||||
chat: ExtendedChat,
|
||||
chat: Chat,
|
||||
messageId: MessageIdentifier
|
||||
): String? {
|
||||
return when {
|
||||
@ -43,8 +54,55 @@ fun makeLinkToMessage(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see makeLinkToMessage
|
||||
*/
|
||||
val Message.link: String?
|
||||
get() = makeLinkToMessage(
|
||||
chat,
|
||||
messageId
|
||||
)
|
||||
|
||||
/**
|
||||
* Link which can be used as by any user to get access to [Chat]. Returns null in case when there are no
|
||||
* known way to build link
|
||||
*/
|
||||
val Chat.link: String?
|
||||
get() {
|
||||
if (this is UsernameChat) {
|
||||
username ?.username ?.let { return it }
|
||||
}
|
||||
if (this is ExtendedPublicChat) {
|
||||
inviteLink ?.let { return it }
|
||||
}
|
||||
if (this is PrivateChat) {
|
||||
return id.link
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private const val stickerSetAddingLinkPrefix = "$internalLinkBeginning/addstickers"
|
||||
|
||||
val StickerSetName.stickerSetLink
|
||||
get() = link(this, "$stickerSetAddingLinkPrefix/$this")
|
||||
|
||||
/**
|
||||
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [MarkdownV2]
|
||||
*/
|
||||
@Deprecated("Use extension `stickerSetLink` + getting of `asMarkdownV2Source` property")
|
||||
fun makeLinkToAddStickerSetInMarkdownV2(
|
||||
stickerSetName: StickerSetName
|
||||
) = stickerSetName.stickerSetLink.asMarkdownV2Source
|
||||
/**
|
||||
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [Markdown]
|
||||
*/
|
||||
@Deprecated("Use extension `stickerSetLink` + getting of `asMarkdownSource` property")
|
||||
fun makeLinkToAddStickerSetInMarkdown(stickerSetName: StickerSetName) = stickerSetName.stickerSetLink.asMarkdownSource
|
||||
/**
|
||||
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [HTML]
|
||||
*/
|
||||
@Deprecated("Use extension `stickerSetLink` + getting of `asHtmlSource` property")
|
||||
fun makeLinkToAddStickerSetInHtml(stickerSetName: StickerSetName) = stickerSetName.stickerSetLink.asHtmlSource
|
||||
/**
|
||||
* Create a link for adding of sticker set with name [stickerSetName]. Was added thanks to user Djaler and based on
|
||||
* https://github.com/Djaler/evil-bot/blob/master/src/main/kotlin/com/github/djaler/evilbot/utils/StickerUtils.kt#L6-L8
|
||||
@ -53,34 +111,12 @@ private const val stickerSetAddingLinkPrefix = "$internalLinkBeginning/addsticke
|
||||
* @see [makeLinkToAddStickerSetInMarkdown]
|
||||
* @see [makeLinkToAddStickerSetInHtml]
|
||||
*/
|
||||
@Deprecated("Use extension `stickerSetLink` + getting of required property")
|
||||
fun makeLinkToAddStickerSet(
|
||||
stickerSetName: StickerSetName,
|
||||
parseMode: ParseMode
|
||||
) = (stickerSetName to "$stickerSetAddingLinkPrefix/$stickerSetName").link(
|
||||
parseMode
|
||||
)
|
||||
|
||||
/**
|
||||
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [MarkdownV2]
|
||||
*/
|
||||
fun makeLinkToAddStickerSetInMarkdownV2(stickerSetName: StickerSetName) =
|
||||
makeLinkToAddStickerSet(
|
||||
stickerSetName,
|
||||
MarkdownV2
|
||||
)
|
||||
/**
|
||||
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [Markdown]
|
||||
*/
|
||||
fun makeLinkToAddStickerSetInMarkdown(stickerSetName: StickerSetName) =
|
||||
makeLinkToAddStickerSet(
|
||||
stickerSetName,
|
||||
Markdown
|
||||
)
|
||||
/**
|
||||
* @return Link for adding of sticker set with name [stickerSetName] with formatting for [HTML]
|
||||
*/
|
||||
fun makeLinkToAddStickerSetInHtml(stickerSetName: StickerSetName) =
|
||||
makeLinkToAddStickerSet(
|
||||
stickerSetName,
|
||||
HTML
|
||||
)
|
||||
) = when (parseMode) {
|
||||
MarkdownParseMode -> makeLinkToAddStickerSetInMarkdown(stickerSetName)
|
||||
MarkdownV2ParseMode -> makeLinkToAddStickerSetInMarkdownV2(stickerSetName)
|
||||
HTMLParseMode -> makeLinkToAddStickerSetInHtml(stickerSetName)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user