Mention users

The Mentions feature lets users tag specific individuals within a chat or conversation.

Unity Chat SDK lets one user tag another user by adding @ and typing at least three first letters of the username they want to mention. As a result, they get a list of suggested usernames when typing such a suggestion, like @Mar.

Generic referencing

Channel references, user mentions, and links are instances of MessageElement with different MentionTarget types.

The list of returned users depends on your app configuration - these can be either all members in the channel where you write a message or all users of your app (user data taken from the Admin Portal keyset for your app). The number of returned suggested users for the mention also depends on your app configuration and can show up to 100 suggestions. The names of the suggested users can consist of multiple words and contain up to 200 characters.

You can configure your app to let users mention up to 100 users in a single message (default value is 10).

You can implement mentions in your app in a similar way you would implement channel referencing and links.

Requires App Context

To mention users from a keyset, you must enable App Context for your app's keyset in the Admin Portal.

Add user mentions

You can let users mention other users in a message by adding @ and typing at least three first letters of the username they want to mention, like @Mar.

Whenever you mention a user, this user is added to the list of all mentioned users inside the MessageDraft object. This draft contains the text content and all mentioned users and their names from the selected user metadata source (all channel members or all users on the app's keyset). Once you send this message (Send()), that information gets stored in the message metadata.

Method signature

You can add a user reference by calling the AddMention() method with the target of MentionTarget.User.

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 Alex is a user mention.

// 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 user mention to the string "Alex"
messageDraft.AddMention(6, 4, new MentionTarget
{
Target = "alex_d",
Type = MentionType.User
});

Remove user mentions

RemoveMention() lets you remove a previously added user mention from a draft message.

Method signature

You can remove user mentions from a draft message by calling the RemoveMention() method at the exact offset where the user mention 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 user mention from the Hello Alex! I have sent you this link on the #offtopic channel. message where Alex is a user mention.

// assume the message reads
// Hello Alex! I have sent you this link on the #offtopic channel.`

// remove the user reference
messageDraft.RemoveMention(6)

Get user suggestions

The event handler you attached to the OnDraftUpdated event returns all users referenced in the draft message that match the provided 3-letter string from your app's keyset.

icon

Single listener


For example, if you type Sam, you will get the list of users starting with Sam like Samantha or Samir. The default number of returned suggested usernames is 10 which is 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 are you there?");

Get mentioned users

You can access the MentionedUsers property of the Message object to return all users mentioned in a message.

Method signature

This is how you can access the property:

message.MentionedUsers

Properties

PropertyTypeDescription
MentionedUsersList<User>List of all users mentioned in a message.

Basic usage

Check if the message with the 16200000000000000 timetoken contains any mentions.

// reference the "support" channel
if (chat.TryGetChannel("support", out var channel))
{
Console.WriteLine($"Found channel with name {channel.Name}");

// get the message with the specified timetoken
if (channel.TryGetMessage("16200000000000000", out var message))
{
Console.WriteLine($"Message: {message.MessageText}");

// check if the message contains any mentions
if (message.MentionedUsers != null && message.MentionedUsers.Count > 0)
{
Console.WriteLine("The message contains mentions.");
foreach (var mentionedUser in message.MentionedUsers)
show all 33 lines

The GetCurrentUserMentions() method lets you collect in one place all instances when a specific user was mentioned by someone — either in channels or threads. You can use this info to create a channel with all user-related mentions.

Method signature

This method has the following signature:

chat.GetCurrentUserMentions(string startTimeToken, string endTimeToken, int count)

Input

ParameterTypeRequiredDefaultDescription
startTimetokenstringYesn/aTimetoken delimiting the start of a time slice (exclusive) to pull messages with mentions from. For details, refer to the Fetch History section.
endTimetokenstringYesn/aTimetoken delimiting the end of a time slice (inclusive) to pull messages with mentions from. For details, refer to the Fetch History section.
countintYes100Number of historical messages with mentions to return in a single call. Since each call returns all attached message reactions by default, the maximum number of returned messages is 100. For more details, refer to the description of the IncludeMessageActions parameter in the Unity SDK docs.

Output

This method returns an object containing two fields: Mentions and IsMore.

ParameterTypeDescription
MentionsList<UserMentionData>Array listing the requested number of historical mention events with a set of information that differ slightly depending on whether you were mentioned in the main (parent) channel or in a thread.

For mentions in the parent channel, the returned UserMentionData includes these fields: ChannelId where you were mentioned, userId that mentioned you, Event (of type Mention), Message that included the mention.

For mentions in threads, the returned UserMentionData includes similar fields, the only difference is that you'll get ParentChannelId and ThreadChannelId fields instead of just ChannelId to clearly differentiate the thread that included the mention from the parent channel in which this thread was created.
IsMoreboolInfo whether there are more historical events to pull.

Basic usage

List the last ten mentions for the current chat user.

// fetch the last 10 mentions for the current user
var mentions = chat.GetCurrentUserMentions(string.Empty, string.Empty, 10);

if (mentions != null && mentions.Mentions.Any())
{
foreach (var mention in mentions.Mentions)
{
Console.WriteLine($"Mentioned in Channel ID: {mention.ChannelId}, Message: {mention.Message.MessageText}");
}
}
else
{
Console.WriteLine("No mentions found.");
}

Show notifications for mentions

You can monitor all events emitted when you are mentioned in a parent or thread channel you are a member of using the StartListeningForMentionEvents() method. You can use this method to create pop-up notifications for the users.

Events documentation

To read more about the events of type Mention, refer to the Chat events documentation.

Method signature

This method has the following parameters:

// start listening
chat.StartListeningForMentionEvents(string userId)

// triggered mention event
public event Action<MentionEvent> OnMentionEvent;
// needs a corresponding event handler
void EventHandler(MentionEvent event)
Input
ParameterTypeRequiredDefaultDescription
userIdstringYesChannel equal to user IDChannel to listen for new Mention events. In the case of Mention events, this channel is always the current user's ID.
Output

This method doesn't return any data.

Basic usage

Print a notification for a mention of the current chat user on the support channel.

if(!chat.TryGetCurrentUser(out var user))
{
return;
}
chat.StartListeningForMentionEvents(user.Id);
chat.OnMentionEvent += mentionEvent =>
{
if(mentionEvent.ChannelId == "support")
{
Console.WriteLine($"{user.Id} has been mentioned on the support channel!");
}
};
Last updated on