Reference channels
Channel referencing lets users mention specific channel names in a chat conversation. They can do it by adding #
and typing at least three first letters of the channel name they want to reference. As a result, they get a list of suggested names when typing such a suggestion, like #Sup
.
Generic referencing
Channel references, user mentions, and links are instances of MessageElement
with different MentionTarget
types.
The list of returned channels contains all channels in an app (channel data taken from the Admin Portal keyset for the app), regardless of whether the users are members of these channels. The number of returned suggested channels for reference also depends on the app configuration and can show up to 100
suggestions. The names of the suggested channels can consist of multiple words.
You can configure your app to let users refer up to 100
channels in a single message (default value is 10
) and show references as links.
You can implement channel referencing in your app similarly to user mentions and links.
Requires App Context
To reference channels from a keyset, you must enable App Context for your app's keyset in the Admin Portal.
Add channel references
You can let users reference channels in a message by adding #
and typing at least three first letters of the channel name they want to reference, like #Sup
.
Method signature
You can add a channel reference by calling the AddMention()
method with the target
of MentionTarget.Channel
.
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 #offtopic
is a channel reference.
// 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 channel mention for the "#offtopic" channel
messageDraft.AddMention(45, 9, new MentionTarget
{
Target = "group.offtopic", // Assuming the channel ID is "group.offtopic"
Type = MentionType.Channel
});
Remove channel references
RemoveMention()
lets you remove a previously added channel reference from a draft message.
Method signature
You can remove channel references from a draft message by calling the RemoveMention()
method at the exact offset where the channel reference 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 channel reference from the Hello Alex! I have sent you this link on the #offtopic channel.
message where #offtopic
is a channel reference.
// assume the message reads
// Hello Alex! I have sent you this link on the #offtopic channel.`
// Remove the channel reference for "#offtopic"
messageDraft.RemoveMention(45);
Get channel suggestions
The event handler you attached to the OnDraftUpdated
event returns all channels 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 are you a member of the #offtop channel?");
Get referenced channels
You can access the ReferencedChannels
property of the Message
object to return all channel names referenced in a message.
Method signature
This is how you can access the property:
message.ReferencedChannels
Properties
Property | Type | Description |
---|---|---|
ReferencedChannels | List<Channel> | List of all channels referenced in a message. |
Basic usage
Check if the message with the 16200000000000000
timetoken contains any channel references.
// 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 channel references
if (message.ReferencedChannels != null && message.ReferencedChannels.Count > 0)
{
Console.WriteLine("The message contains channel references.");
foreach (var referencedChannel in message.ReferencedChannels)
show all 33 lines