Manage message updates
Edit messages and receive real-time update events.
Requires Message Persistence
Enable Message Persistence in the Admin Portal.
Edit messages
editText() replaces an existing message's content.
Method signature
This method takes the following parameters:
1message.editText(newText: String): PNFuture<Message>
Input
| Parameter | Description |
|---|---|
newText *Type: StringDefault: n/a | New/updated text that you want to add in place of the existing message. |
Output
| Type | Description |
|---|---|
PNFuture<Message> | An updated message instance with an added edited action type. |
Sample code
Correct the number of the support ticket you sent to 78398.
message.editText("Support ticket number corrected to 78398").async { result ->
result.onSuccess {
// handle success
println("Ticket number successfully updated to 78398.")
}.onFailure {
// handle failure
println("Failed to update the ticket number.")
}
}
Get message updates
Receive real-time updates when messages or reactions change:
streamUpdates()- updates for a singleMessageobjectstreamUpdatesOn()- updates for multipleMessageobjects
Both return an unsubscribe function.
Stream update behavior
streamUpdates()returns the entire updatedMessageon each changestreamUpdatesOn()returns the complete list of monitored messages on each change
Method signature
These methods take the following parameters:
-
streamUpdates()1message.streamUpdates(callback: (message: T) -> Unit): AutoCloseable -
streamUpdatesOn()1class Message {
2 companion object {
3 fun streamUpdatesOn(
4 messages: Collection<Message>,
5 callback: (messages: Collection<Message>) -> Unit
6 ): AutoCloseable
7 }
8}
Input
| Parameter | Required in streamUpdates() | Required in streamUpdatesOn() | Description |
|---|---|---|---|
messagesType: Collection<Message>Default: n/a | No | Yes | A collection of Message objects for which you want to get updates on changed messages or message reactions. |
callbackType: (message: T) -> UnitDefault: n/a | Yes | No | Function that takes a single Message object. It defines the custom behavior to be executed when detecting message or message reaction changes. |
callbackType: messages: (Collection<Message>) -> UnitDefault: n/a | No | Yes | Function that takes a set of Message objects. It defines the custom behavior to be executed when detecting message or message reaction changes. |
Output
| Type | Description |
|---|---|
AutoCloseable | Interface that lets you stop receiving message-related updates by invoking the close() method. |
Sample code
-
streamUpdates()Get message and message reaction-related updates for the message with the timetoken
16200000000000000published on thesupportchannel.
show all 20 lines1val supportChannel: Channel
2//...
3
4// fetch the message with timetoken
5val timetoken = 16200000000000000L
6supportChannel.getMessage(timetoken).async { messageResult ->
7 messageResult.onSuccess { message ->
8 // stream updates for the specific message
9 val autoCloseable = message?.streamUpdates { updatedMessage: Message ->
10 // The callback receives the entire updated Message object (including all reactions)
11 // each time a change occurs.
12 println("-=Updated message: $updatedMessage")
13 }
14
15 // to stop streaming updates at some later point, use: -
streamUpdatesOn()Get message and message reaction-related updates for the first page of messages published on the
supportchannel.
show all 25 lines1// get the support channel
2val supportChannel: Channel
3// ...
4
5// fetch the first page of messages
6supportChannel.getHistory(count = 25).async { historyResult ->
7 historyResult.onSuccess { historyResponse ->
8 val messages = historyResponse.messages
9
10 // stream updates for the fetched messages
11 val autoCloseable = Message.streamUpdatesOn(messages = messages) { updatedMessages ->
12 // The callback receives the complete list of all messages you're monitoring
13 // each time any change occurs.
14 updatedMessages.forEach { updatedMessage ->
15 println("-=Updated message: $updatedMessage")
Other examples
-
streamUpdates()Stop listening to updates for the message with the timetoken
16200000000000000published on thesupportchannel.
show all 28 lines1// retrieve the support channel
2val supportChannel: Channel
3// ...
4
5// fetch the message with timetoken
6val timetoken = 16200000000000000L
7supportChannel.getMessage(timetoken).async { messageResult ->
8 messageResult.onSuccess { message ->
9 // stream updates for the specific message
10 val autoCloseable = message?.streamUpdates<Message> { updatedMessage ->
11 // The callback receives the entire updated Message object (including all reactions)
12 // each time a change occurs.
13 println("-=Updated message: $updatedMessage")
14 }
15 -
streamUpdatesOn()Stop listening to updates for the last ten messages published on the
supportchannel.
show all 31 lines1// get the support channel
2val supportChannel: Channel
3// ...
4
5// fetch the last ten messages
6supportChannel.getHistory(count = 10).async { historyResult ->
7 historyResult.onSuccess { historyResponse ->
8 val messages = historyResponse.messages
9
10 // stream updates for the fetched messages
11 val autoCloseable = Message.streamUpdatesOn(messages = messages) { updatedMessages ->
12 // The callback receives the complete list of all messages you're monitoring
13 // each time any change occurs.
14 updatedMessages.forEach { updatedMessage ->
15 println("-=Updated message: $updatedMessage")