How to show different email notifications for different types of users in a TalkJS chat
Tags: webdev, javascript, api, chat
Published: 2021-06-14 14:21:23

Roles allow you to change the default behavior of [TalkJS](https://talkjs.com/) for different users. You can assign roles to a certain group of your users and have full control over which user gets which role. Email notifications can then be customized for different roles. In this post, we will look at how we can set this up.

## TalkJS Roles

TalkJS allows different groups of users to have different settings by assigning them a specific role. You have full control over which user gets which role.

For example, you may want one group of users to receive email notifications if they have a new message, or give another group of users the ability to share files. You can do all of this using roles.

A role allows you to define the following settings for a group of users:

*   Allow/disallow file or location sharing
*   Create custom email notification templates
*   Configure SMS settings
*   Configure text/contact information suppression
*   Configure how links in chat messages will be opened
*   Customize the standard TalkJS user interface using [themes](https://talkjs.com/docs/Features/Themes/index.html)

You can create roles from the TalkJS [dashboard](https://talkjs.com/dashboard). Let’s create two roles, with each receiving a different type of email notification.


## Creating a role in TalkJS

Creating a role in TalkJS is simple, and can be accomplished using the dashboard.

### 1. Add a role in the dashboard

To add a role, click on **Roles** at the top left corner of your [dashboard](https://talkjs.com/dashboard).

Next, click on the **Create new role** button and put “buyer” as the name of the role. You can decide if the role will copy data from previous roles or use the default role setting.

After this, you can manage the settings for the role by using the checkboxes next to each setting.

### 2. Assign a role to a user

You assign a role to a user when you create the user. For example, if the name of the role you created on the dashboard is buyer, you can assign this role to the user "Alice" in your code like this:

```
const me = new Talk.User({
    id: "123456",
    name: "Alice",
    email: "alice@example.com",
    photoUrl: "https://demo.talkjs.com/img/alice.jpg",
    welcomeMessage: "Hey there! How are you? :-)"
    role: "buyer" // <-- that's the one!
});
```

Make sure the role matches the role name you chose in the dashboard, in this case, “buyer”.

For the purpose of this example, you may want to set the email to an email address you have access to. This will allow you to access the email that gets sent.

Repeat this process, except this time create a new role called “seller”. Create another user and set the role to be “seller”. At this stage, you should have two roles, “buyer” and “seller”, and a single user set up with each role.

## How Email notifications are Sent

If a user has a [role](https://talkjs.com/docs/Reference/Concepts/Roles.html) set and has at least one email address, they will automatically start receiving email notifications when they are offline. Users can have more than one email address and TalkJS will notify all email addresses on record.

Email notifications are not sent with each message but rather grouped and sent after a period of inactivity to avoid spamming the user's inbox.

The TalkJS notification system has been carefully designed to send notifications as quickly as possible while ensuring your users do not feel like they’re being spammed. We use a number of heuristics to get this balance right.

There are a few conditions that must be met in order to send notifications:
*   The user is offline.
*   The user is online but has a different conversation selected in the UI.
*   The user is online, has the current conversation selected in the UI, but the browser tab/window does not have focus.

In other words, a notification is not sent out when the user has the current conversation selected in the UI and the tab containing TalkJS is focused.

To keep the email count low, subsequent messages are grouped together. After a user sends a message, TalkJS tracks whether they continue typing. A notification is sent when the user has stopped typing for several seconds. This notification will contain all of the messages they sent since they first started typing.

This also holds for group conversations. if two users have a quick real-time interaction, then the notification sent to the other participants will include all messages sent since the first user started typing until the last user stopped typing.

TalkJS is designed to be a slow chat solution, which supports reply-via-email functionality. When a user replies to a notification email, their reply will show up in the conversation.

## Managing your email notification settings through the dashboard

To modify the email notification that TalkJS sends, first head to the TalkJS Dashboard and then click on **Roles** in the top left corner, inside of the role editor you'll see a section for **Email settings**.

The first option you’ll see is the **Enable Email notifications** checkbox. When enabled, you have the option to change the subject, theme, and template for your email

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ej8uomvft3pip8ddvhwe.PNG)

The next option you’ll see is the **Enable replying to email notifications via email** checkbox. This allows recipients of email notifications to reply directly to the email that they receive, and have the response sent back to the chat. 

You can also decide whether users can send attachments with their replies. However, no images are allowed to be sent no matter what. This is due to being unable to determine the difference between purposefully attached images, and those automatically include in the footer such as company logos. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s80lj0joojgftupzqgyq.PNG)

Use these role settings to send different emails to users depending on whether they are buyers or sellers.

## Complete control over the emails your users receive

The concept of roles allows you to easily set up different email templates for different types of users. We have covered how to create roles, how to add a role to a user, and finally how to configure the notifications that are sent for a role. This provides you with a highly configurable system that can be tailored to your specific needs. It also ensures your users are getting relevant notifications that are genuinely helpful and address their unique situations.