On this page

Links

Version 2 functionality

This document uses message drafts v2 unless otherwise specified.

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 mentionType values.

addMention() adds a link to a draft message.

Method signature

Call addMention() with mentionType set to textLink. 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 a message draft
2messageDraft = channel.createMessageDraftV2({ userSuggestionSource: "global" })
3
4// Add initial text
5messageDraft.update("Hello Alex! I have sent you this link on the #offtopic channel.")
6
7// Add a user mention to the string "Alex"
8messageDraft.addMention(33, 4, "textLink","https://example.com")

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(33)

Render URLs

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(): MixedTextTypedElement[]

Input

This method doesn't take any parameters.

Output

ParameterDescription
MixedTextTypedElement[]
Type: array
A returned array of elements that the message is composed of. Each element is a separate array.

For plain text, you'll get a text type element, like {"type": "text", "content": { "text": "This is a sample text "}}.

For plain links, you'll get a plainLink element, like {"type": "plainLink", "content": {"link": "https://my-company.com"}}.

For text links, you'll get a textLink element, like {"type": "textLink", "content": {"link": "https://my-company.com", "text": "my company"}}.

For mentions, you'll get a mention element, like {"type": "mention", "content": {"name": "Peter", "id": "some-id-for-peter"}}.

For channel references, you'll get a channelReference element, like {"type": "channelReference", "content": {"name": "Support", "id": "some-id-for-channel"}}.

Sample code

Show all URLs starting with www, http, or https as clickable links.

1message.getMessageElements()

Other examples

Customize link rendering with these examples.

Show all URLs starting with www, http, or https as plain text instead of clickable links.

1const renderMessagePart = (messagePart: MixedTextTypedElement) => {
2 if (messagePart.type === "plainLink") {
3 return messagePart.content.link;
4 }
5 return "";
6}

Change the background color of all plain links to yellow.

1if (messagePart.type === "plainLink") {
2 const linkStyle = { backgroundColor: "yellow" };
3 return (
4 <a href={messagePart.content.link} style={linkStyle}>
5 {messagePart.content.link}
6 </a>
7 );
8 }
9 return "";

Change the background color of all text links to red.

1const renderMessagePart = (messagePart: MixedTextTypedElement) => {
2 if (messagePart.type === "textLink") {
3 return (
4 <a href={messagePart.content.link} style={{ backgroundColor: 'red' }}>
5 {messagePart.content.text}
6 </a>
7 );
8 }
9 return "";
10}

Don't display YouTube links as plain links.

1const renderMessagePart = (messagePart: MixedTextTypedElement) => {
2 if (messagePart.type === "plainLink") {
3 const youtubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\//i;
4 if (youtubeRegex.test(messagePart.content.link)) {
5 // if it's a YouTube link, you can handle it differently, for example, embedding the video or displaying a custom YouTube link format
6 return <div>Custom rendering for YouTube link: {messagePart.content.link}</div>;
7 } else {
8 // for other plain links, display them as regular anchor tags
9 return <a href={messagePart.content.link}>{messagePart.content.link}</a>;
10 }
11 }
12 return "";
13}

getMessagePreview() returns message draft elements (text, URLs, mentions, channel references) for custom rendering.

Method signature

This method has the following signature:

1messageDraft.getMessagePreview(): MixedTextTypedElement[]

Input

This method doesn't take any parameters.

Output

ParameterDescription
MixedTextTypedElement[]
Type: array
A returned array of elements that the message is composed of. Each element is a separate array.

For plain text, you'll get a text type element, like {"type": "text", "content": { "text": "This is a sample text "}}.

For plain links, you'll get a plainLink element, like {"type": "plainLink", "content": {"link": "https://my-company.com"}}.

For text links, you'll get a textLink element, like {"type": "textLink", "content": {"link": "https://my-company.com", "text": "my company"}}.

For user mentions, you'll get a mention element, like {"type": "mention", "content": {"name": "Peter", "id": "some-id-for-peter"}}.

Sample code

Show a preview of the modified link.

1// replace a link with text
2messageDraft.addLinkedText(
3 {
4 text: "this support article",
5 link: "https://www.support-article.com/",
6 positionInInput: 10
7 }
8)
9//show the preview of the changed link
10messageDraft.getMessagePreview()

For examples of how you can customize draft message previews, refer to the getMessageElements() method that's similar in structure but is meant for final, published messages.

Add linked text

addLinkedText() replaces a plain link with descriptive text in a draft message.

Method signature

This method has the following signature:

1messageDraft.addLinkedText({
2 text: string;
3 link: string;
4 positionInInput: number;
5}): void

Input

* required
ParameterDescription
text *
Type: string
Default:
n/a
Text that you want to replace the link with.
link *
Type: string
Default:
n/a
Link that you want to replace with text.
positionInInput *
Type: number
Default:
n/a
Position of a character in a message where the link you want to replace starts. It's counted from the beginning of the message (including spaces), with 0 as the first character.

Output

TypeDescription
void
Method returns no output data.

Errors

You'll get the You need to insert a URL error if you don't provide a URL. If you try to replace a link with a different link, you'll get the You cannot insert a link inside another link error.

Sample code

Replace Check out this support article https://www.support-article.com/. with Check out this support article.

1messageDraft.addLinkedText(
2 {
3 text: "this support article",
4 link: "https://www.support-article.com/",
5 positionInInput: 10
6 }
7)

Remove linked text

removeLinkedText() removes a linked URL from text in a draft message.

Method signature

This method has the following signature:

1messageDraft.removeLinkedText(positionInInput: number): void

Input

* required
ParameterDescription
positionInInput *
Type: number
Default:
n/a
Beginning of the text in a message where the link you want to remove starts. The position is counted from the beginning of the message (including spaces), with 0 as the first character.

Output

TypeDescription
void
Method returns no output data.

Errors

If you don't provide the link text's position in the message, you'll get the You need to insert a number error. If you try to remove a link that doesn't exist, you'll get the This operation is noop. There is no link at this position. error.

Sample code

Remove a link from the "Check out this support article." message.

1messageDraft.removeLinkedText(
2 {
3 positionInInput: 10
4 }
5)
Deprecated

This method is deprecated.

textLinks returns all text links in a message.

Method signature

This method has the following signature:

1message.textLinks: TextLink[]

Properties

PropertyDescription
textLinks
Type: TextLink[]
Array of text links included in the message.

Sample code

Get all text links included in the message with the 16200000000000000 timetoken.

1// reference the "message" that you're interested in
2const message = await channel.getMessage("16200000000000000")
3
4// get all text links
5message.textLinks
Last updated on