Mention users
The Mentions feature lets users tag specific individuals within a chat or conversation.
Kotlin 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
.
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.
Requires App Context
To mention users from a keyset, you must enable App Context for your app's keyset in the Admin Portal.
Send message with 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
.
Method signature
Head over to the sendText()
method for details.
Basic usage
Mention Samantha
and Samir
(@Sam
) in a message.
fun mentionUsersExample(chat: ChatAPI) {
val mentionedUsers: MessageMentionedUsers = mapOf(
0 to MessageMentionedUser(id = "101", name = "Samantha"), // first mention
1 to MessageMentionedUser(id = "102", name = "Sam") // second mention
)
val messageText = "Hello @Samantha and @Sam! Welcome to the team!"
chat.getChannel("support").async { result ->
result.onSuccess {
it.sendText(
text = messageText,
mentionedUsers = mentionedUsers
).async { sendResult ->
sendResult.onSuccess {
show all 28 linesGet 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
Basic usage
Check if the message with the 16200000000000000
timetoken contains any mentions.
channel.getMessage(16200000000000000).async { result ->
result.onSuccess { message: Message? ->
if (message != null) {
// Access the mentionedUsers property
val mentionedUsers = message.mentionedUsers
if (mentionedUsers.isNotEmpty()) {
println("The message contains the following mentioned users:")
mentionedUsers.forEach { user ->
println("User: ${user.name}")
}
} else {
println("The message does not contain any mentions.")
}
} else {
show all 21 linesCollect all user-related mentions
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(
startTimetoken: Long?,
endTimetoken: Long?,
count: Int?
): PNFuture<GetCurrentUserMentionsResult>
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
startTimetoken | Long | No | n/a | Timetoken delimiting the start of a time slice (exclusive) to pull messages with mentions from. For details, refer to the Batch History section. |
endTimetoken | Long | No | n/a | Timetoken delimiting the end of a time slice (inclusive) to pull messages with mentions from. For details, refer to the Batch History section. |
count | Int | No | 100 | Number 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 Kotlin SDK docs. |
Output
Parameter | Type | Description |
---|---|---|
PNFuture<GetCurrentUserMentionsResult> | object | Returned object containing two fields: enhancedMentionsData and isMore . |
→ enhancedMentionsData | enhancedMentionsData (ChannelMentionData or ThreadMentionData ) | 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 ChannelMentionData includes these fields: event (of type Event<EventContent.Mention> ), channelId where you were mentioned, message that included the mention, userId that mentioned you. For mentions in threads, the returned ThreadMentionData 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. |
→ isMore | Boolean | Info whether there are more historical events to pull. |
Basic usage
List the last ten mentions for the current chat user.
chat.getCurrentUserMentions(count = 10).async { mentionsResult ->
mentionsResult.onSuccess { mentions ->
// handle success
println("Last 10 mentions for the current user:")
mentions.data.forEach { mention ->
println("Message: ${mention.message.content}, Mentioned At: ${mention.message.timetoken}")
}
}.onFailure { exception ->
// handle failure
println("Error getting user mentions")
}
}
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 listenForEvents()
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:
inline fun <reified T : EventContent> listenForEvents(
channel: String,
customMethod: EmitEventMethod?,
noinline callback: (event: Event<T>) -> Unit
): AutoCloseable {
return listenForEvents(T::class, channel, customMethod, callback)
}
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
T | reified T : EventContent | Yes | n/a | Reified type parameter bounded by the EventContent interface, allowing access to type information at runtime. |
channel | String | Yes | Mentioned user ID | Channel to listen for new mention events. By default, it's the current (mentioned) user's ID. |
customMethod | String | No | n/a | An optional custom method for emitting events. If not provided, defaults to null . |
callback | noinline (event: Event<T>) -> Unit | Yes | n/a | A lambda function that is called with an Event<T> as its parameter. It defines the custom behavior to be executed whenever an mention event type is detected on the specified channel. |
Output
Type | Description |
---|---|
AutoCloseable | Interface that lets you stop receiving moderation-related updates (moderation events) by invoking the close() method. |
Basic usage
Print a notification for a mention of the current chat user on the support
channel.
val user = chat.currentUser
chat.listenForEvents(user.id) { event: Event<EventContent.Mention> ->
if (event.payload.channel == "support") {
println("${user.id} has been mentioned on the support channel!")
}
}