Skip to main content
Updates the user_defined field inside a specific message’s state. This provides a mechanism to store custom state on a message that will be reloaded if a thread is reloaded from history. The new data merges with existing data. When a thread loads, this data can be found on the message at message.message_state.content[<contentId>].user_defined.

Syntax

instance.updateMessageStateUserDefined(messageId, contentId, data);

Parameters

messageId
string
required
The ID of the message to update.
contentId
string
required
The ID of the message content that must store the user_defined data.
data
unknown
required
The user-defined value to merge into the message state.

Returns

void
Returns a void operator.

Examples

The following example stores custom metadata on a message:
// Store custom metadata on a message
instance.updateMessageStateUserDefined(
    'msg-12345',
    'content-1',
    { customField: 'value', timestamp: Date.now() }
);

// Access the data when thread is reloaded
instance.on('pre:threadLoaded', (event, instance) => {
    event.messages.forEach((message) => {
        const userData = message.message_state?.content?.['content-1']?.user_defined;
        console.log('Custom data:', userData);
    });
});

Do you need practical examples?

Learn how to apply the features available for embedded chat into your implementation with guidance and examples.