Links

Chat SDK lets you encode URLs that begin with www, http, or https (plain links) so that they can be customized and rendered as clickable links. You can also let chat users create hyperlinks manually by replacing URLs in messages with descriptive texts (text links).

Add linked text

addLinkedText() lets you replace a plain link on the draft message to display a meaningful text in the published message.

Method signature

This method has the following signature:

messageDraft.addLinkedText({
text: string;
link: string;
positionInInput: number;
}): void

Input

ParameterTypeRequiredDefaultDescription
textstringYesn/aText that you want to replace the link with.
linkstringYesn/aLink that you want to replace with text.
positionInInputnumberYesn/aPosition 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
voidMethod 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.

Basic usage

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

messageDraft.addLinkedText(
{
text: "this support article",
link: "https://www.support-article.com/",
positionInInput: 10
}
)

Remove linked text

removeLinkedText() removes a linked URL from a given text on the draft message.

Method signature

This method has the following signature:

messageDraft.removeLinkedText(positionInInput: number): void

Input

ParameterTypeRequiredDefaultDescription
positionInInputnumberYesn/aBeginning 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
voidMethod 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.

Basic usage

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

messageDraft.removeLinkedText(
{
positionInInput: 10
}
)

Render URLs

getMessageElements() shows all URLs starting with www, http, or https as links in the final published message. You can create custom functions if you want to customize how these links render.

This is the same method that lets you show mentions as links.

Method signature

This method has the following signature:

message.getMessageElements(): MixedTextTypedElement[]

Input

This method doesn't take any parameters.

Output

ParameterTypeDescription
MixedTextTypedElement[]arrayA 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"}}.

Basic usage

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

message.getMessageElements()

Other examples

Check how you can customize the default behavior of the Chat SDK methods and render the links differently.

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

const renderMessagePart = (messagePart: MixedTextTypedElement) => {
if (messagePart.type === "plainLink") {
return messagePart.content.link;
}
return "";
}

Change the background color of all plain links to yellow.

if (messagePart.type === "plainLink") {
const linkStyle = { backgroundColor: "yellow" };
return (
<a href={messagePart.content.link} style={linkStyle}>
{messagePart.content.link}
</a>
);
}
return "";
}

Change the background color of all text links to red.

const renderMessagePart = (messagePart: MixedTextTypedElement) => {
if (messagePart.type === "textLink") {
return (
<a href={messagePart.content.link} style={{ backgroundColor: 'red' }}>
{messagePart.content.text}
</a>
);
}
return "";
}

Don't display YouTube links as plain links.

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

getMessagePreview() returns a list of message draft elements, such as: text, plain URLs, text URLs, user mentions, or referenced channels. You can decide how you want to render these elements in your app and format them separately. For example, you could use this method to render an anchor tag for your links.

Method signature

This method has the following signature:

messageDraft.getMessagePreview(): MixedTextTypedElement[]

Input

This method doesn't take any parameters.

Output

ParameterTypeDescription
MixedTextTypedElement[]arrayA 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"}}.

Basic usage

Show a preview of the modified link.

// replace a link with text
messageDraft.addLinkedText(
{
text: "this support article",
link: "https://www.support-article.com/",
positionInInput: 10
}
)
//show the preview of the changed link
messageDraft.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.

textLinks is a getter method that returns all text links in a given message.

Method signature

This method has the following signature:

message.textLinks: TextLink[]

Properties

PropertyTypeDescription
textLinksTextLink[]Array of text links included in the message.

Basic usage

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

// reference the "message" that you're interested in
const message = await channel.getMessage("16200000000000000")

// get all text links
message.textLinks

Render URLs (deprecated)

Alternative method

This method is deprecated. Use getMessageElements() instead.

getLinkedText() shows all URLs starting with www, http, or https as links in the final published message. You can create custom functions if you want to customize how these links render.

This is the same method that lets you show mentions as links.

Method signature

This method has the following signature:

message.getLinkedText(): MixedTextTypedElement[]

Input

This method doesn't take any parameters.

Output

ParameterTypeDescription
MixedTextTypedElement[]arrayA 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"}}.

Basic usage

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

message.getLinkedText()

Other examples

Check how you can customize the default behavior of the Chat SDK methods and render the links differently.

Last updated on