

How to Hide Some Chat Messages for Certain Users in a TalkJS Chat
source link: https://dev.to/talkjs/how-to-hide-some-chat-messages-for-certain-users-in-a-talkjs-chat-23g4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this tutorial, we’ll learn how to hide specific chat messages for users using the TalkJS Chat API message filters. We will also learn how to combine TalkJS custom message attributes with message filters to implement a wide variety of use cases for filtering chat messages.
What Are Message Filters?
TalkJs message filters are used to control which messages show up in the Chatbox UI. You can filter messages based on type, origin, sender, and custom attributes.
Note: Messages are only filtered in the message list (Chatbox UI). The inbox UI's conversation feed will always show the last message sent to the conversation, regardless of the message filter set.
Applying Message Filters in a Chat UI
To apply filters to a Chatbox, use the setMessageFilter method and pass a MessagePredicate to apply filters based on Custom attributes, sender, origin, and type.
Let’s see different examples of filters that can be applied in a Chat UI using a simple Vanilla Js implementation of TalkJS Chatbox. Here, we create a TalkJs user and bind that user object to a new Chatbox instance using the TalkJs session.
Note that, apart from plain JavaScript, TalkJs SDK supports Vue, Angular, and React. And the API will be the same across these frameworks. You can check out our docs for more information.
<script>
Talk.ready.then(function() {
var me = new Talk.User({
id: "333333",
name: "Chris Pratt",
email: "[email protected]",
photoUrl: "https://i.picsum.photos/id/1025/4951/3301.jpg?hmac=_aGh5AtoOChip_iaMo8ZvvytfEojcgqbCH7dzaz-H8Y",
welcomeMessage: "Hey there! How are you? :-)",
role: "Accountant"
});
window.talkSession = new Talk.Session({
appId: "<appID>",
me: me
});
var chatbox = talkSession.createChatbox();
chatbox.mount(document.getElementById("talkjs-container"));
});
</script>
Fig: Demo VanillaJS application
Only Showing Text Messages
A chat can contain text messages, attachments, and locations. To show only text messages in a Chat, you can first add a custom attribute to every message sent using the on("sendMessage") method.
Specifically, we’re adding a custom attribute named “textMessage” to every message sent except for attachments. The value of the custom attribute does not matter here as we’ll not be using it. We just need to have the custom attribute present for all text messages.
chatbox.on("sendMessage", function(ev) {
if (!ev.message.attachment) {
ev.override({
custom: {
textMessage: "true"
}
});
}
});
Now, to show only text messages in a Chatbox, simply filter out messages with the “textMessage'' custom attribute set using the “exists'' check. As an example, I’m adding the filter on the click of a button here.
document.getElementById('filterTextMessages').onclick = function filter() {
chatbox.setMessageFilter({
custom: {
textMessage: "exists"
}
})
}
Only Showing Messages with Attachments
We can use the same custom “textMessage” attribute, but with “!exists” check and filter out just the attachments. This filter will omit all messages with the “textMessage” attribute present, leaving us with only those messages with attachments.
document.getElementById('filterAttachments').onclick = function filter() {
chatbox.setMessageFilter({
custom: {
textMessage: "!exists"
}
})
}
Only Showing Admin Messages (Filter Based on Sender's Role)
Every TalkJS user has a role assigned to them during creation, and you can filter the messages based on this role using the “sender” FieldPredicate.
Let’s take an example and show only those messages sent by users with the role “Admin.”
document.getElementById('showAdminMessages').onclick = function filter() {
chatbox.setMessageFilter({sender: {role: ["==", "Admin"]}})
}
Filtering out Messages from Banned Users
In your chat application, you will sometimes need to block or filter messages from banned users for various reasons. You can change the user’s role to “Banned” and then filter out messages in a chatbox based on the Sender role.
var me = new Talk.User({
id: "154721",
name: "Matt Wong ",
email: "[email protected]",
photoUrl: "https://picsum.photos/id/237/200/300",
welcomeMessage: "Hey there! How are you? :-)",
role: "Banned",
});
document.getElementById('hideBannerUserMessages').onclick = function banned() {
chatbox.setMessageFilter({
sender: {
role: ["!=", "Banned"]
}
})
}
Reset Filters
Finally, to reset or remove all filters, just call the setMessageFilter method again and pass NULL; this should clear all current filters.
document.getElementById('resetFilters').onclick = function reset() {
chatbox.setMessageFilter(null)
}
If you run into any issues, please get in touch via the popup on our website or send an email to [email protected].
Recommend
-
5
How to connect a chatbot to a TalkJS chat Jun 8 ・9 min read
-
7
Today, we'll show you how to create a chat UI similar to WhatsApp using the TalkJS Chat API. We'll start with the TalkJS Default Theme and customize it by using the
-
5
Recently, we have been looking at how we can use the Theme Editor present in the TalkJS Chat API to theme our chat. We previously th...
-
4
Creating a chat function in your app can be a long and difficult process, thankfully the TalkJS chat API makes that easy, we can even customise it to make it look in keeping with our app. We'll explore how we...
-
8
Imagine you are starting your very own marketplace start-up. Something along the lines of Airbnb. You would need a real-time chat that allows your users to talk to each other. Airbnb has a very simple and easy-to-follow interface for its chat...
-
9
How to build a chat similar to Upwork with TalkJS Aug 3 ・5 min read...
-
12
How to hide Memories of certain people, pets, and dates in Google Photos By Taylor Kerns Published 4 days ago Somet...
-
6
How to Hide Your Focus Status From Certain iPhone Contacts By Joe Cason Published 5 hours ago Sharing your Focus...
-
4
Home ...
-
10
Here’s how to hide messages and chats on WhatsApp
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK