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 = 33, 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 = 33)
Get link suggestions
The message elements listener returns all links referenced in the draft 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
To return all text links in a given message, use the getMessageElements()
method.
Method signature
This method has the following signature:
message.getMessageElements(): List<MessageElement>
Input
This method doesn't take any parameters.
Output
Type | Description |
---|---|
List<MessageElement> | A list of message elements representing parsed components of the input text, including processed user mentions, links, and referenced channels based on the available data. |
Basic usage
Get all text links included in the message with the 16200000000000000
timetoken.
chat.getChannel("incident-management").async { channelResult ->
channelResult.onSuccess { channel ->
// Successfully retrieved the channel
channel?.getMessage(16200000000000000L)?.async { messageResult ->
messageResult.onSuccess { message ->
// Handle success
val elements = message?.getMessageElements()
// Find and print all text links in the message elements
val textLinks = elements?.filter { it is Link && it.target is Url }
if (textLinks.isNotEmpty()) {
println("Text links in the message:")
textLinks.forEach { link ->
println(link) // Print or process each text link as required
show all 29 linesGet text links (deprecated)
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