Filter messages
Message filters let you control which messages appear in a chat, based on properties such as the sender, message type, or creation time.
This guide shows how to set and combine message filters in a chatbox using the setMessageFilter method in the classic JavaScript SDK. You can also use setMessageFilter
with the Inbox and Popup UIs. For other SDKs, check out their messageFilter
reference docs:
Use setMessageFilter to control which messages the chatbox displays. For example, to show only messages from a user with the ID sample_user_alice
:
1chatbox.setMessageFilter({ sender: { id: ['==', 'sample_user_alice'] } });
You can pass one or more filter conditions to match messages by sender, timestamp, message type, or other properties.
The following examples show how to use different filters.
Use the createdAt
filter to show all messages created before, after, or between timestamps (in Unix milliseconds). For example:
1// Messages created after the specified timestamp2chatbox.setMessageFilter({ createdAt: ['>', 1719298371586] });34// Messages created between two timestamps5chatbox.setMessageFilter({6 createdAt: ['between', [1619298371586, 172298371586]],7});
Use the sender
filter to match messages from specific users or roles. For example:
1// Messages from a single sender2chatbox.setMessageFilter({3 sender: { id: ['==', 'sample_user_alice'] },4});56// Messages where the sender has both a role and email7chatbox.setMessageFilter({9});
Use the custom
filter to match messages that include specific custom fields. For example:
1// Messages with a custom category of 'shoes'2chatbox.setMessageFilter({ custom: { category: ['==', 'shoes'] } });34// Messages with a topic of 'inquiry' or 'reservation'5chatbox.setMessageFilter({6 custom: { topic: ['oneOf', ['inquiry', 'reservation']] },7});
Filter messages by their creation source:
web
: messages sent in the chat UIrest
: message created with the REST APIimport
: messages imported with the REST APIemail
: messages created by replying via email
For example:
1// Exclude imported messages2chatbox.setMessageFilter({ origin: ['!=', 'import'] });
Use the type
filter to choose whether a message is user-generated or system-generated. For example:
1// Show only system messages2chatbox.setMessageFilter({ type: ['==', 'SystemMessage'] });
You can combine multiple filters to match complex conditions. You can filter for messages that match all conditions or any of them.
Pass an array of filters to show messages that match all conditions. For example:
1// Show system messages created after a set timestamp2chatbox.setMessageFilter([3 { type: ['==', 'SystemMessage'] },4 { createdAt: ['>', 1719298371586] },5]);
Use the any
operator to show messages that match any of the listed filters. For example:
1// Show messages that are system messages or created2// after a specified timestamp, or both3chatbox.setMessageFilter([4 'any',5 [{ type: ['==', 'SystemMessage'] }, { createdAt: ['>', 1719298371586] }],6]);
To turn off filtering and show all messages, use an empty filter:
1chatbox.setMessageFilter({});
The following operators are available when defining message filters.
Operator | Description | Example |
---|---|---|
== | Equals | { type: ['==', 'SystemMessage'] } |
!= | Not equal | { origin: ['!=', 'import'] } |
> / < | Greater than or less than (for timestamps) | { createdAt: ['>', 1719298371586] } |
between | Between two values | { createdAt: ['between', [start, end]] } |
oneOf | Matches one of several values | { custom: { topic: ['oneOf', ['inquiry', 'reservation']] } } |