Links
Encode URLs (www, http, https) as clickable links or create hyperlinks with custom text.
Generic referencing
Channel references, user mentions, and links share the same MessageElement structure with different MentionTarget values.
Add links
addMention() adds a link to a draft message.
Method signature
Call addMention() with target set to MentionTarget.Url. See addMention() for details.
Sample code
Create the Hello Alex! I have sent you this link on the #offtopic channel. message where link is a URL.
1// create an empty message draft
2val messageDraft = channel.createMessageDraft(isTypingIndicatorTriggered = channel.type != ChannelType.PUBLIC)
3
4// add the text
5messageDraft.update(text = "Hello Alex! I have sent you this link on the #offtopic channel.")
6
7// add a link to the string 'link'
8messageDraft.addMention(offset = 33, length = 4, target = MentionTarget.Url(url = "www.pubnub.com"))
Remove links
removeMention() removes a link from a draft message.
Method signature
Call removeMention() at the exact offset where the link starts. See removeMention() 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.
Sample code
Remove the link from the Hello Alex! I have sent you this link on the #offtopic channel. message where link is a URL.
1// assume the message reads
2// Hello Alex! I have sent you this link on the #offtopic channel.`
3
4// remove the link mention
5messageDraft.removeMention(offset = 33)
Get link suggestions
The message elements listener returns link suggestions that match the 3-letter string in the draft.
Single listener
The message elements listener returns suggested mentions for channel references, user mentions, and links.
Method signature
Add a message elements listener to receive link suggestions. See addChangeListener() for details.
Sample code
1// create a message draft
2val messageDraft = channel.createMessageDraft(isTypingIndicatorTriggered = channel.type != ChannelType.PUBLIC)
3
4// add the listener
5val listener = { elements: List<MessageElement>, suggestedMentions: PNFuture<List<SuggestedMention>> ->
6 updateUI(elements) // updateUI is your own function for updating UI
7 suggestedMentions.async { result ->
8 result.onSuccess { updateSuggestions(it) } // updateSuggestions is your own function for displaying suggestions
9 }
10 }
11messageDraft.addChangeListener(listener)
Get text links
getMessageElements() returns URLs as link elements in published messages. Customize rendering with your own functions. This method also handles mention rendering.
Method signature
This method has the following signature:
1message.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. |
Sample code
Get all text links included in the message with the 16200000000000000 timetoken.
1chat.getChannel("incident-management").async { channelResult ->
2 channelResult.onSuccess { channel ->
3 // Successfully retrieved the channel
4 channel?.getMessage(16200000000000000L)?.async { messageResult ->
5 messageResult.onSuccess { message ->
6 // Handle success
7 val elements = message?.getMessageElements()
8
9 // Find and print all text links in the message elements
10 val textLinks = elements?.filter { it is Link && it.target is Url }
11
12 if (textLinks.isNotEmpty()) {
13 println("Text links in the message:")
14 textLinks.forEach { link ->
15 println(link) // Print or process each text link as required
show all 29 linesGet text links (deprecated)
textLinks returns all text links in a message.
Method signature
This is how you can access the property:
1message.textLinks
Sample code
Get all text links included in the message with the 16200000000000000 timetoken.
1val channel = chat.getChannel("your-channel")
2
3channel.getMessage(16200000000000000).async { result ->
4 result.onSuccess { message: Message? ->
5 if (message != null) {
6 // Access the textLinks property
7 val textLinks = message.textLinks
8
9 if (textLinks != null && textLinks.isNotEmpty()) {
10 println("The message contains the following text links:")
11 textLinks.forEach { textLink ->
12 println("Link: ${textLink.link}, Start Index: ${textLink.startIndex}, End Index: ${textLink.endIndex}")
13 }
14 } else {
15 println("The message does not contain any text links.")
show all 23 lines