Links
Kotlin Chat SDK lets you encode URLs that begin with www
, http
, or https
(plain links) so that they can be rendered as links.
Generic referencing
Channel references, user mentions, and links are instances of MessageElement
with different MentionTarget
types.
Add links
addMention()
lets you replace a plain link on the draft message to display a meaningful text in the published message.
Method signature
You can attach links to a draft message by calling the addMention()
method with the target
of MentionTarget.Url
.
Refer to the addMention()
method for details.
Basic usage
Create the Hello Alex! I have sent you this link on the #offtopic channel.
message where link
is a URL.
// create an empty message draft
val messageDraft = channel.createMessageDraft(isTypingIndicatorTriggered = channel.type != ChannelType.PUBLIC)
// add the text
messageDraft.update(text = "Hello Alex! I have sent you this link on the #offtopic channel.")
// add a link to the string 'link'
messageDraft.addMention(offset = 28, length = 4, target = MentionTarget.Url(url = "www.pubnub.com"))
Remove links
removeMention()
lets you remove a previously added link from a draft message.
Method signature
You can remove links from a draft message by calling the removeMention()
method at the exact offset where the link starts.
Refer to the removeMention()
method for details.
Offset value
If you don't provide the position of the first character of the message element to remove, it isn't removed.
Basic usage
Remove the link from the Hello Alex! I have sent you this link on the #offtopic channel.
message where link
is a URL.
// assume the message reads
// Hello Alex! I have sent you this link on the #offtopic channel.`
// remove the link mention
messageDraft.removeMention(offset = 32)
Get link suggestions
The message elements listener returns all links referenced in the sent message that match the provided 3-letter string from your app's keyset.
Single listener
The message elements listener returns suggested mentions for channel references, user mentions, and links.
Method signature
You must add a message elements listener to receive link suggestions.
Refer to the addChangeListener()
method for details.
Basic usage
// create a message draft
val messageDraft = channel.createMessageDraft(isTypingIndicatorTriggered = channel.type != ChannelType.PUBLIC)
// add the listener
val listener = { elements: List<MessageElement>, suggestedMentions: PNFuture<List<SuggestedMention>> ->
updateUI(elements) // updateUI is your own function for updating UI
suggestedMentions.async { result ->
result.onSuccess { updateSuggestions(it) } // updateSuggestions is your own function for displaying suggestions
}
}
messageDraft.addChangeListener(listener)
Get text links
You can access the textLinks
property of the Message
object to return all text links in a given message.
Method signature
This is how you can access the property:
message.textLinks
Basic usage
Get all text links included in the message with the 16200000000000000
timetoken.
val channel = chat.getChannel("your-channel")
channel.getMessage(16200000000000000).async { result ->
result.onSuccess { message: Message? ->
if (message != null) {
// Access the textLinks property
val textLinks = message.textLinks
if (textLinks != null && textLinks.isNotEmpty()) {
println("The message contains the following text links:")
textLinks.forEach { textLink ->
println("Link: ${textLink.link}, Start Index: ${textLink.startIndex}, End Index: ${textLink.endIndex}")
}
} else {
println("The message does not contain any text links.")
show all 23 lines