﻿# Process events on a new message in a session

Learn how to configure an event handler to track live chat messages and session events, trigger a business process, and automatically notify clients by email about new replies.

> [HTML Version](new-message-events-processing.html)

An event handler in a module can track:

- Sending an outgoing message or receiving an incoming message in a session.

- System messages about session state changes: assigned to an operator, in queue, in spam, or closed.

When one of these events occurs, a script or business process is triggered.

````
Начало внимание

````
A system administrator can configure the handler when creating a [custom module](extentions.md).

````
Конец внимание

## ````
Configure an event handler for sending a message in a session

For example, you can configure an event handler that tracks an operator's response in a session.

A user on an external portal submits an inquiry to a live chat from a ticket item details page using the [Chat with live chat operators](chat-with-operators.md) widget. A session is automatically created in BRIX. When an operator responds in the session, the event handler registers it and triggers a business process. During the process, the client receives an email notification about the new message in the session.

### Step 1. Configure a business process in the module

1. Go to **Administration > Modules** and [create a custom module](create-extention.md).

2. In the module settings, go to the **Processes** tab and [add a business process](extention-processes.md) that will execute each time an operator sends a message in a customer chat.

For example, add the **Notify about new messages** process. During its execution, the client's email address will be determined, and the operator's response will be sent to it. Configure the process diagram as follows:

**(new-message-events-processing-1.png)**

3. In the business process context, add variables to which the handler, after the operator responds, will pass the data required to send an email notification to the client:

- **Session **(`session`). With the **Arbitrary app (one)** type, stores the session in which the conversation takes place.

- **Message type **(`type`). With the **String (string)** type, identifies an outgoing message from the operator.

- **Message **(`message`). With the **String (string)** type, passes the message sent by the operator.

- **Email **(`email`). With the **String (line)** type, stores the email address to which the notification will be sent.

- **Ticket **(`ticket`). With the **App (one)** type, stores a link to the ticket item where correspondence with the operator takes place.

4. Add a **Script** block named **Determine notification email recipient** to the process diagram. In its settings, create the `getEmailRecipient` function, which will determine the client's email address for sending the notification.

Sample script for determining client email

````
async function getEmailRecipient(): Promise<void> \{  
````
// Script runs only for messages sent by the operator  
   ````
if (Context.data.type \!= 'Out' || \!Context.data.session) return  
   const session = await Imports?.lines?.app.\_sessions.search().where(f =>  
    f.\_\_id.eq(Context.data.session\!.id)).first();  
  if (\!session) return  
````
// Check that the session was created from a ticket page on the external portal  
and assigned to an operator or bot  
````
  if (session.data.\_externalChatId?.includes('service\_desk:applications') \&\& (  
       session.data.\_state?.code == LineSessionState.AssignedToOperator  
   )) \{  
````
// Get the client's email for sending the notification  
````
  if (session.data.\_clients) \{  
       const client = await session.data.\_clients\[0\].fetch();  
       const user = await client.data.\_user?.fetch();  
       if (user) \{  
          Context.data.email = user.data.email;  
           \}  
       \}  
````
// Determine the ticket item where the conversation takes place  
````
     const id\_ticket =  
   session.data.\_externalChatId.split(\`service\_desk:applications:\`)\[1\];  
     if (id\_ticket) \{  
        Context.data.application = await Context.fields.ticket.app.search().where(f =>  
   f.\_\_id.eq(ticket).first();  
       \}  
   \}  
\}
````

5. ````
Configure the process flow to follow different branches depending on whether the client's address was retrieved. To do this, add an [exclusive gateway](360012495911.md#exclusive-gateway) named **Email received?** with two transitions: **Yes** and **No**. If the email address is determined, a notification is sent to the client; otherwise, the process ends. Select **No** as the default transition.

6.  For the **Yes** transition, add an [Email Notification](360011443400.md) activity. In its settings, fill in the required fields using the variables created in Step 3.

**(new-message-events-processing-2.png)**

- **Recipient\***. Add the `\{\$email\}` variable containing the client's email address.

- **Email subject\***. Write a short subject line and add the `\{\$ticket\}` variable to it so the client knows which ticket item the response relates to.

- **Email body\***. Insert the `\{\$message\}` variable containing the operator's response.

7. Save and publish the process.

### Step 2. Configure an event handler

After creating the business process in the module, perform the following steps:

1. In the module settings, on the **Event Handlers** tab, click **+ Handler**. In the window that opens, fill in and save the data:

- Handler name, for example, **New message**.

- Handler type: **Start process**.

- Event domain: **Live Chats**.

- Event: **New message**.

2. In the window that opens, on the **Settings** tab, select the previously created **Notify about new messages** process.

3. Go to the **Input Variables** tab and map the variables from the process context to the corresponding event properties.

4. On the top panel of the page, click **Save**, then click **Publish** to activate the event handler.

5. Go to the module management page and enable it.

Now, when an operator sends a response in a session, the client will receive a timely notification on their email.