Links
Unity 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.
Send message with 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 add a link suggestion 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 a message draft
var messageDraft = testChannel.CreateMessageDraft();
// Update the message with the initial text
messageDraft.Update("Hello Alex! I have sent you this link on the #offtopic channel.");
// Add a URL to the word "link"
messageDraft.AddMention(33, 4, new MentionTarget
{
Target = "https://example.com",
Type = MentionType.Url
});
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(33)
Get link suggestions
The event handler you attached to the OnDraftUpdated
event returns all links referenced in the draft message that match the provided 3-letter string from your app's keyset.
For example, if you type #Sup
, you will get the list of channels starting with Sup
like Support
or Support-Agents
. The default number of returned suggested channel names is 10
, configurable to a maximum value of 100
.
Method signature
You must handle the event to update to the contents of a message draft, as well as retrieve the current message elements suggestions for user mentions, channel reference, and links. For example, when the message draft contains ... @name ...
or ... #chann ...
.
Enable receiving suggested mentions
You must enable receiving suggested mentions by calling messageDraft.SetSearchForSuggestions(true);
before introducing your event handler.
Refer to the Add a message draft listener section for details.
Basic usage
Insert the first suggested mention whenever the draft is updated and mentions are detected.
var messageDraft = channel.CreateMessageDraft();
messageDraft.SetSearchForSuggestions(true);
messageDraft.OnDraftUpdated += (elements, mentions) =>
{
if (!mentions.Any())
{
return;
}
messageDraft.InsertSuggestedMention(mentions[0], mentions[0].ReplaceTo);
};
messageDraft.Update("Alex, update the link to https://www.pubnub.com ");
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
Properties
Property | Type | Description |
---|---|---|
TextLinks | List<TextLink> | List of all links included in a message. |
Basic usage
Get all text links included in the message with the 16200000000000000
timetoken.
// reference the "support" channel
if (chat.TryGetChannel("support", out var channel))
{
// get the message with the specific timetoken
if (channel.TryGetMessage("16200000000000000", out var message))
{
// check if the message contains any text links
if (message.TextLinks != null && message.TextLinks.Count > 0)
{
Console.WriteLine("The message contains the following text links:");
foreach (var textLink in message.TextLinks)
{
Console.WriteLine($"Text Link: {textLink.Link}");
}
}
show all 29 lines