---
updatedAt: 2026-05-04T10:04:55.000Z
agentTools:
  projectIndex: https://developers.kommo.com/llms.txt
---

# Salesbot

# Getting Started with Salesbot

To get started, you need to connect the integration with chats in the left column of the Digital Pipeline. You will find instructions for each messenger in the settings window. After connecting the chat integration, you can activate the <Glossary>Salesbot</Glossary> to handle incoming new chats, as well as configure bots for specific stages.

## Configure Salesbot

You can connect the Salesbot in two ways:

1. Go to the **Leads** section ➡️ **Automate** ➡️ select a stage ➡️ **Add Trigger** ➡️ choose **Salesbot**.

<Image align="center" border={true} width="800px" src="https://files.readme.io/6c3b313a4d7f8b40d3051210b825def3fb1b89a932b290f6ec1cc1425dd53be1-Group_881234045.png" className="border" />

Next, select **Create a new bot**. When the modal window opens, you can choose a standard template or create a new bot.

<Image align="center" border={true} width="400px" src="https://files.readme.io/8389801347f2d013810daecfaed28053f5a4f5bd38caaab81af73597f259a994-Screenshot_2024-10-07_at_19.11.30.png" className="border" />

2. Go to the **Settings** section ➡️ **Communication tools** tab ➡️ **Salesbots** ➡️ **Create a new bot or import**.

<Image align="center" src="https://files.readme.io/5cd48167dcb93cec7d2dc23c222a7246c6325d3a89fd64847f3d76025d8a4fe5-Group_881234046.png" />

When opening the modal window with Salesbot steps, you can click **View Source** to view the bot's code.

<Image align="center" src="https://files.readme.io/5fc427da0adb8307b509ed2706aa701f21a2f2281dfb417adc715c00cb0cb89f-Group_881234047.png" />

Leads that entered the stage before the action appeared in the Digital Pipeline will be ignored unless you tick **Apply the trigger to all leads already in this stage**.

<Image align="center" width="400px" src="https://files.readme.io/613d2c7899f282bac5711af76b10bc6d372f02d838419772b5896331ba670817-Screenshot_2024-10-08_at_12.09.43.png" />

# Salesbot language

Salesbot uses a structured JSON object with specific keys. The example below will ask the question, `"Please provide your phone number and email,"` and set the `"salesbot"` tag.  After the user responds, it will validate the data and reply with one of the specified messages. For more details about presets, please refer to the next section.

```json
[
    {
        "question": [
            {
                "handler": "show",
                "params": {
                    "type": "text",
                    "value": "Please provide your phone number and email"
                }
            },
            {
                "handler": "action",
                "params": {
                    "name": "set_tag",
                    "params": {
                        "type": 2,
                        "value": "salesbot"
                    }
                }
            }
        ],
        "answer": [
            {
                "handler": "preset",
                "params": {
                    "name": "contacts.validate_base_info",
                    "params": {
                        "empty_email": "Please provide your e-mail",
                        "empty_phone": "Please provide your phone number",
                        "invalid_phone": "It appears that the phone number may be incorrect",
                        "success": "Thank you",
                        "empty_all": "Please provide your phone number and email"
                    }
                }
            }
        ]
    }
]
```

The object must contain the keys `"question"`, `"answer"`, or `"finish"`.

* The data in the `"question"` object handle the actions that will occur when a message is sent to the user.
* The data in the `"answer"` object manage the actions that will take place when the user responds.
* The data in the `"finish"` object define the actions that will occur when the bot is completed.

<Callout icon="📘" theme="info">
  There can be multiple keys. However, there is a limitation on the size of the JSON, which cannot exceed **64KB.**
</Callout>

In the `"question"`, `"answer"`, and `"finish"` objects, there should be <Glossary>handler</Glossary>s.

Before adding the JSON object to the bot, make sure to validate it.

# Salesbot Error Handler

If a message from the bot cannot be delivered to the client, for example, when the client has blocked messages from that chat, the bot can handle the error and execute specific handlers.

**Example**:

```json Error handler
{
     "0":{
        "question":[
           ...
        ],
        "answer":[
           ...
        ]
     },
     "error":[
        {
           "handler":"action",
           "params":{
              "name":"change_status",
              "params":{
                 "value":142
              }
           }
        }
     ]
  }
```

# Salesbot Handlers

| Code           | Description                                                       |
| :------------- | :---------------------------------------------------------------- |
| show           | Sends a message with text                                         |
| buttons        | Processing response received from buttons from instant messengers |
| action         | Action                                                            |
| meta           | Meta data processing                                              |
| condition      | Condition                                                         |
| validations    | Validation                                                        |
| preset         | Processes data for a specific algorithm                           |
| goto           | Transition of the script to a certain step                        |
| wait\_answer   | Waits for the answer                                              |
| find           | Search                                                            |
| filter         | Filtration                                                        |
| send\_internal | Sends an internal message                                         |
| stop           | Stops the bot                                                     |

## show

<Callout icon="🚧" theme="warn">
  **This handler accepts incoming leads if it is configured in Salesbot.**
</Callout>

The `"show"` handler sends a message or buttons to the client's chat. Any text sent currently supports the following placeholders:

<Table align={["left","left"]}>
  <thead>
    <tr>
      <th>
        Placeholder
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        \{\{contact.name}}, \{\{name}}
      </td>

      <td>
        Contact name
      </td>
    </tr>

    <tr>
      <td>
        \{\{lead.id}}
      </td>

      <td>
        Lead ID
      </td>
    </tr>

    <tr>
      <td>
        \{\{contact.id}}
      </td>

      <td>
        Contact ID
      </td>
    </tr>

    <tr>
      <td>
        \{\{origin}}
      </td>

      <td>
        Source of the lead (Telegram,Viber, Facebook)
      </td>
    </tr>

    <tr>
      <td>
        \{\{lead.source_id}}
      </td>

      <td>
        Lead source ID
      </td>
    </tr>

    <tr>
      <td>
        \{\{message_text}}
      </td>

      <td>
        Received message from the client in the logical block of the response
      </td>
    </tr>

    <tr>
      <td>
        \{\{lead.cf.#custom_field_id#}},  
        \{\{contact.cf.#custom_field_id#}}, \{\{company.cf.#custom_field_id#}}
      </td>

      <td>
        The value of the custom field of a lead/contact/company. Replace #custom_field_id# with the ID of the additional field.
      </td>
    </tr>

    <tr>
      <td>
        \{\{rand}}
      </td>

      <td>
        Random string
      </td>
    </tr>

    <tr>
      <td>
        \{\{short_rand}}
      </td>

      <td>
        Short random string
      </td>
    </tr>

    <tr>
      <td>
        \{\{short_rand_num}}
      </td>

      <td>
        Random number from 1111 to 9999
      </td>
    </tr>

    <tr>
      <td>
        \{\{message_text.email}}
      </td>

      <td>
        Email (if it is present in the client's message)
      </td>
    </tr>

    <tr>
      <td>
        \{\{message_text.phone}}
      </td>

      <td>
        Phone number (if it is present in the client's message)
      </td>
    </tr>

    <tr>
      <td>
        \{\{regexp./([1-9]+) things /}}
      </td>

      <td>
        The value by regular expression from the user's response. The value from the parentheses will be substituted. It can be used in the answer block.
      </td>
    </tr>

    <tr>
      <td>
        \{\{lead.price}}
      </td>

      <td>
        Lead sale value
      </td>
    </tr>

    <tr>
      <td>
        \{\{current_date}}
      </td>

      <td>
        Current date
      </td>
    </tr>

    <tr>
      <td>
        \{\{lead.status_id}}
      </td>

      <td>
        Lead stage ID
      </td>
    </tr>

    <tr>
      <td>
        \{\{cf.talk.nps}}
      </td>

      <td>
        Conversation evaluation
      </td>
    </tr>

    <tr>
      <td>
        \{\{lead.responsible.id}},  \{\{contact.responsible.id}}, \{\{company.responsible.id}}
      </td>

      <td>
        Lead/contact/company responsible user ID
      </td>
    </tr>

    <tr>
      <td>
        \{\{lead.responsible.name}},  \{\{contact.responsible.name}}, \{\{company.responsible.name}}
      </td>

      <td>
        Lead/contact/company responsible user name
      </td>
    </tr>

    <tr>
      <td>
        \{\{lead.responsible.email}},  \{\{contact.responsible.email}}, \{\{company.responsible.email}}
      </td>

      <td>
        Lead/contact/company responsible user email
      </td>
    </tr>
  </tbody>
</Table>

For contact placeholders, either the primary contact of the lead or the contact from the chat in which communication with the client is taking place is used.

### text

Parameters of the handler for sending text.

```json text
{
  "handler": "show",
  "params": {
    "type": "text",
    "value": "Please provide your phone number and e-mail",
    "quick_replies": [
      "user_phone_number",
      "user_email"
    ]
  }
}
```

| Parameter      | Data type | Description                                                                                                                                                                                                                                                                                                                                      |
| :------------- | :-------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| type           | string    | It should be `"text"`                                                                                                                                                                                                                                                                                                                            |
| value          | string    | Text to be sent to the user                                                                                                                                                                                                                                                                                                                      |
| quick\_replies | array     | An array of elements (currently available: "user\_phone\_number" and "user\_email") sends [quick reply buttons](https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies). This feature is available only for Facebook and displays buttons containing the user's Facebook account information (phone number, email). |

### buttons

Parameters of the handler for sending buttons.

```json buttons
{
  "handler": "show",
  "params": {
    "type": "buttons",
    "value": "Please choose the type of participation:",
    "buttons": [
      "Personal presence",
      "Online"
    ]
  }
}
```

| Parameter        | Data type | Description                                                                                       |
| :--------------- | :-------- | :------------------------------------------------------------------------------------------------ |
| type             | string    | It should be `"buttons"`                                                                          |
| value            | string    | Text to be sent to the user **(In Messenger, texts longer than 80 characters will be truncated)** |
| buttons          | array     | An array whose elements are the text of the buttons that will be sent                             |
| accept\_unsorted | bool      | Set to false if you do not need to parse the incoming leads at the first response                 |

### buttons\_url

Parameters of the handler for sending buttons with links to external resources.

```json buttons_url
{
    "handler": "show",
    "params": {
        "type": "buttons_url",
        "value": "Links to external resources",
        "buttons": [
            {
                "text": "Bing",
                "url": "https://www.bing.com"
            },
            {
                "text": "Google",
                "url": "https://google.com"
            }
        ]
    }
}
```

| Parameter | Data type | Description                                                                                      |
| :-------- | :-------- | :----------------------------------------------------------------------------------------------- |
| type      | string    | It should be `"buttons_url"`. It sends buttons with links.                                       |
| value     | string    | Text to be sent to the user **(In Messenger, text longer than 80 characters will be truncated)** |
| buttons   | array     | An array whose elements are the text of the buttons that will be sent                            |

If you want to send links to social media and enable auto-linking, the links should be in the following format:

| Social media       | Link                                                           | Comments                                                         |
| :----------------- | :------------------------------------------------------------- | :--------------------------------------------------------------- |
| Facebook Messenger | <https://m.me/123/?ref=VisitorUid_{{visitor_uid}}>             | Where '123' is the group ID                                      |
| WhatsApp           | <https://wa.me/7895?text=ID:%20{{session_id}}>                 | where '7895' is the WhatsApp phone number linked to the account. |
| Viber              | viber://pa?chatURI=bot\&context=VisitorUid\_\{\{visitor\_uid}} | where 'bot' is the name of the public account                    |
| Telegram           | tg://resolve?domain=bot\&start=VisitorUid\_\{\{visitor\_uid}}  | where 'bot' is the name of the bot                               |

## buttons

The buttons handler is designed to be inserted into the response logic block and allows for processing responses from the sent buttons or exact match responses.

```json buttons handler
{
  "handler": "buttons",
  "params": [
    {
      "value": "Personal presence",
      "params": [
        {
          "handler": "...",
          "params": {...}
        }
      ]
    },
    {
      "value": "Online",
      "params": [
        {
          "handler": "...",
          "params": {...}
        }
      ]
    }
  ]
}
```

The `"buttons"` handler expects an array of objects to enter into parameters, in which any of the handlers specified on this page can be called.

## goto

The goto handler allows you to jump to a specific step in the scenario, for example, if you need to repeatedly perform certain actions.

<Callout icon="📘" theme="info">
  The step count starts from 0.
</Callout>

```json goto handler
{
    "handler": "goto",
    "params": {
        "type": "question",
        "step": 3
    }
}
```

| Parameter | Data type | Description                                                                                                |
| :-------- | :-------- | :--------------------------------------------------------------------------------------------------------- |
| type      | string    | The block to which the jump will be made to. Possible values are `"question"`,` "answer"`, and `"finish"`. |
| step      | int       | The step of the bot the jump will be made to.                                                              |

## wait\_answer

It is used to wait for a response from the user to a given question. It allows you to ask the user a question and wait for their answer before proceeding with further actions.

```json wait_answer handler
{
  "handler": "wait_answer",
  "params": {
    "type": "question",
    "step": 2
  }
}
```

| Parameter | Data type | Description                                                                                    |
| :-------- | :-------- | :--------------------------------------------------------------------------------------------- |
| type      | string    | The block to which the jump will be made to. Possible values are `"question"` and ` "answer"`. |
| step      | int       | The step of the bot the jump will be made to.                                                  |

## find

The `"find"` handler allows you to locate an entity and use its data. If an element is found, you can use the following placeholders:

* `{{founded_id}}` – ID of the first found catalog item.
* `{{contact_double.*}}` – if a contact duplicate is found, allowing you to access its fields similarly to the `{{contact.*}}` [placeholders](https://developers.kommo.com/docs/salesbot-dp#show).

```json Contact double
{
  "handler": "find",
  "params": {
    "type": "contact_double",
    "params":{
      "type": "name",
      "actions": [
        {
          "handler": "show",
          "params": {
            "type": "buttons",
            "value": "Is it your number {{contact_double.cf.3574}}?",
            "buttons": [
              "Yes",
              "No"
            ]
          }
        }
      ]
    }
  }
}
```
```json Catalog element
{
  "handler": "find",
  "params": {
  "type": "catalog_elements",
    "params": {
      "value": "Salesbot",
      "catalog_id": "15123",
      "actions": [
        {
          "handler": "show",
          "params": {
            "type": "buttons",
            "value": "An element was found with ID - {{founded_id}}",
            "buttons": [
                "Yes",
                "No"
            ]
          }
        }
      ]
    }
  }
}
```

<Table align={["left","left","left"]}>
  <thead>
    <tr>
      <th>
        Parameter
      </th>

      <th>
        Data type
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        type
      </td>

      <td>
        string
      </td>

      <td>
        `contact_double` – search for a duplicate of the current contact  
        `catalog_elements` – search for a list element
      </td>
    </tr>

    <tr>
      <td>
        params[type]
      </td>

      <td>
        string
      </td>

      <td>
        `type` – can be `"name"` (search is available by name; not required for searching by catalog).
      </td>
    </tr>

    <tr>
      <td>
        params[actions]
      </td>

      <td>
        array
      </td>

      <td>
        Actions to be performed if an entity was found
      </td>
    </tr>

    <tr>
      <td>
        value
      </td>

      <td>
        string
      </td>

      <td>
        The word that is looking for, placeholders from the `"show"` block can be used. For the `catalog_elements` type – the name of the catalog element.
      </td>
    </tr>

    <tr>
      <td>
        catalog_id
      </td>

      <td>
        int
      </td>

      <td>
        ID of the list where you are looking for an element
      </td>
    </tr>
  </tbody>
</Table>

## filter

The `"filter"` handler allows you to find an entity and use its data. If an element is found, you can use the placeholders for the custom fields of `external_lead `and `external_contact`.

```json filter handler
{
   "handler": "filter",
   "params": {
     "type": 2,
     "value": "{{lead.cf.111}}",
     "custom_fields_id": 222,
     "actions": [
       {
         "handler": "action",
         "params": {
           "name": "set_custom_fields",
           "params": {
             "type": 1,
             "value": "{{external_contact.cf.333}}",
             "custom_fields_id": 444,
             "enum": "WORK"
           }
         }
       }
     ]
   }
 }
```

| Parameter          | Date type | Description                                                                  |
| :----------------- | :-------- | :--------------------------------------------------------------------------- |
| type               | int       | The type of entity for which the filtering occurs: 1 – contact, 2 – lead     |
| value              | string    | The word by which we filter can include placeholders from the `"show"` block |
| custom\_fields\_id | int       | The ID of the custom field by which the filter will be applied               |

## action

The `"action"` handler allows you to perform one of the possible actions:

| Action                    | Description                                                                |
| :------------------------ | :------------------------------------------------------------------------- |
| unsorted                  | Actions with Incoming Leads                                                |
| change\_status            | Change of a stage                                                          |
| set\_tag                  | Setting the tag                                                            |
| unset\_tag                | Deleting a tag                                                             |
| set\_custom\_fields       | Setting lead/contact field values                                          |
| subscribe                 | User subscription to the chat in the entity                                |
| unsubscribe               | User unsubscription from the chat in the entity                            |
| add\_lead\_contact        | Adding a lead and a contact that are linked to each other                  |
| set\_budget               | Setting the lead budget (sale)                                             |
| add\_linked\_company      | Add company                                                                |
| add\_note                 | Add a note                                                                 |
| link                      | Linking elements                                                           |
| change\_responsible\_user | Changing the responsible person                                            |
| link\_to\_unsorted        | Creating a contact from the incoming lead and associates it with the lead. |

### unsorted

The `"unsorted"` action allows you to accept or reject an Incoming Lead.

```json unsorted
{
  "handler": "action",
  "params": {
    "name": "unsorted",
    "params": {
      "value": "accept"
    }
  }
},
{
  "handler": "action",
  "params": {
    "name": "unsorted",
    "params": {
      "value": "decline"
    }
  }
}
```

| Parameter | Data type | Description                                                            |
| :-------- | :-------- | :--------------------------------------------------------------------- |
| value     | string    | `"accept"` / `"decline"` for acceptance / decline of the incoming lead |

### change\_status

The action `"change_status"` allows you to change the stage of the lead.

```json change_status
{
  "handler": "action",
  "params": {
    "name": "change_status",
    "params": {
      "value": 142
    }
  }
}
```

| Parameter | Data type     | Description                                                                                                                                                           |
| :-------- | :------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| value     | int           | ID of the stage to which the lead will be transferred                                                                                                                 |
| entity    | int or string | An optional parameter that can be `"double"`. If the `"find"` handler was used beforehand, the stage will be updated for the lead of the found contact, if it exists. |

### set\_tag

The `"set_tag"` action will assign a tag to the lead or contact and supports the `{{origin}}` placeholder, which will specify the source of the lead.

```json set_tag
{
  "handler": "action",
  "params": {
    "name": "set_tag",
    "params": {
      "type": 2,
      "value": "Salesbot"
    }
  }
},
{
  "handler": "action",
  "params": {
    "name": "set_tag",
    "params": {
      "type": 2,
      "value": "{{origin}}"
    }
  }
}
```

| Parameter | Data type | Description                                         |
| :-------- | :-------- | :-------------------------------------------------- |
| type      | int       | The type of entity to be tagged (1-contact, 2-lead) |
| value     | string    | Name of a tag                                       |

### unset\_tag

The `"unset_tag"` action removes the tag from a trade or contact

```json unset_tag
{
  "handler": "action",
  "params": {
    "name": "unset_tag",
    "params": {
      "type": 2,
      "value": "Salesbot"
    }
  }
}
```

| Parameter | Data type | Description                                                       |
| :-------- | :-------- | :---------------------------------------------------------------- |
| type      | int       | The type of entity where tag will be deleted (1-contact, 2-leads) |
| value     | string    | Name of a tag to be deleted                                       |

### set\_custom\_fields

The `"set_custom_fields"` action will set custom field values for a lead or contact. You can find the field IDs in the Setup section of a lead/contact or by using the [method to get the list of entity fields](https://developers.kommo.com/reference/custom-field-by-entity). You can use placeholders described in the [`"show"`](https://developers.kommo.com/docs/salesbot-dp#show) section as values for the fields.

```json set_custom_fields
{
  "handler": "action",
  "params": {
    "name": "set_custom_fields",
    "params": {
      "type": 2,
      "value": "Value of the field",
      "custom_fields_id": 123,
      "option": "add"
    }
  }
},
{
  "handler": "action",
  "params": {
    "name": "set_custom_fields",
    "params": {
      "type": 2,
      "value": "{{message_text}}",
      "custom_fields_id": 987
    }
  }
},
{
  "handler": "action",
  "params": {
    "name": "set_custom_fields",
    "params": {
      "type": "lead",
      "value": "{{last_validation_result}}",
      "custom_field": "{{cf.talk.nps}}"
    }
  }
}
```

<Table align={["left","left","left"]}>
  <thead>
    <tr>
      <th>
        Parameter
      </th>

      <th>
        Data type
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        type
      </td>

      <td>
        int
      </td>

      <td>
        The entity type for which custom fields will be set (1-contact, 2-lead)
      </td>
    </tr>

    <tr>
      <td>
        value
      </td>

      <td>
        string
      </td>

      <td>
        The value of the field that will be set can use placeholders from the `"show"` block. After the `"validations"` handler, it is permissible to use the placeholder `{{last_validation_result}}`, which will use the recognized data from the last `true` condition. For example, if the condition `"contains email"` was used, the found email will be substituted as the value.
      </td>
    </tr>

    <tr>
      <td>
        custom_fields_id
      </td>

      <td>
        int
      </td>

      <td>
        The ID of the field in which the value will be set.
      </td>
    </tr>

    <tr>
      <td>
        custom_field
      </td>

      <td>
        string
      </td>

      <td>
        The identifier of the field being modified can include the following:  
        \{\{lead.price}} – the lead sale  
        \{\{lead.name}} – the lead name  
        \{\{contact.name}} – the contact name  
        \{\{cf.talk.nps}} – the rating of the current conversation
      </td>
    </tr>

    <tr>
      <td>
        calculated
      </td>

      <td>
        bool
      </td>

      <td>
        If it is necessary to try to calculate the value of this custom field using the formula, for example `{{lead.cf.123}} * {{lead.cf.456}}`
      </td>
    </tr>

    <tr>
      <td>
        option
      </td>

      <td>
        string
      </td>

      <td>
        Use `“add”` option to add a value to a field of your choice. Available field types: phone number, email, multiselect list
      </td>
    </tr>
  </tbody>
</Table>

### subscribe

The `"subscribe"` action will subscribe a user/group of users to chat.

```json subscribe
{
  "handler": "action",
  "params": {
    "name": "subscribe",
    "params": {
      "type": "group",
      "value": 111
    }
  }
},
{
  "handler": "action",
  "params": {
    "name": "subscribe",
    "params": {
      "type": "user",
      "value": "{{lead.responsible_user_id}}"
    }
  }
}
```

| Parameter | Data type | Description                              |
| :-------- | :-------- | :--------------------------------------- |
| type      | string    | `"group"` or `"user"`                    |
| value     | int       | The ID of the user group or the user ID. |

### unsubscribe

The `"unsubscribe"` action will unsubscribe a user/group of users to chat.

```json unsubscribe action
{
  "handler": "action",
  "params": {
    "name": "unsubscribe",
    "params": {
      "type": "user",
      "value": "{{lead.responsible_user_id}}"
    }
  }
}
```

| Parameter | Data type | Description                                                                          |
| :-------- | :-------- | :----------------------------------------------------------------------------------- |
| type      | string    | `"group"` ,`"user"` or `"all"`                                                       |
| value     | int       | The ID of the user group or the user ID. Optional if you passed `"all"` as `"type"`. |

### add\_lead\_contact

The `"add_lead_contact"` action will create a lead and a contact and link them together. All fields for the lead and contact can be configured. The custom field values support the same placeholders as in the `"show"` [handler](https://developers.kommo.com/docs/salesbot-dp#show) . A `"preset"` is also supported, allowing the contact and lead to be created only if a message or phone number is received from the client.

```json add_lead_contact action
{
  "handler": "action",
  "params": {
      "name": "add_lead_contact",
      "params": {
          "preset": "contacts.require_email_or_phone",
          "lead": {
              "name": "Lead name",
              "status_id": 142,
              "responsible_user_id": 123,
              "price": 2000,
              "tags": "",
              "custom_fields": [
                  {
                      "id": 77744111,
                      "values": [
                          {
                              "value": "{{contact.name}}"
                          }
                      ]
                  },
                  {
                      "id": 77744222,
                      "values": [
                          {
                              "value": "{{lead.cf.77744222}}"
                          }
                      ]
                  }
              ]
          },
          "contact": {
              "name": "Contact name",
              "responsible_user_id": 123,
              "tags": "",
              "custom_fields": [
                  {
                      "id": 77744444,
                      "values": [
                          {
                              "value": "{{message_text.email}}",
                              "enum": "WORK"
                          }
                      ]
                  },
                  {
                      "id": 77744555,
                      "values": [
                          {
                              "value": "{{message_text.phone}}",
                              "enum": "WORK"
                          }
                      ]
                  }
              ]
          }
      }
  }
}
```

<Table align={["left","left","left"]}>
  <thead>
    <tr>
      <th>
        Parameter
      </th>

      <th>
        Data type
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        lead
      </td>

      <td>
        object
      </td>

      <td>
        Set of lead fields
      </td>
    </tr>

    <tr>
      <td>
        contact
      </td>

      <td>
        object
      </td>

      <td>
        Set of contact fields
      </td>
    </tr>

    <tr>
      <td>
        preset
      </td>

      <td>
        string
      </td>

      <td>
        Optional parameter. One preset  
        `contacts.require_email_or_phone` is supported, which checks whether the phone or email is sent in a message from the client
      </td>
    </tr>
  </tbody>
</Table>

### set\_budget

The `"set_budget"` action will set the sale for the lead.

```json set_budget
{
  "handler": "action",
  "params": {
    "name": "set_budget",
    "params": {
      "value": "{{lead.cf.555123}}*{{lead.cf.555321}}"
    }
  }
}
```

| Parameter | Data type | Description                                                                                                                                                                                                                                                                                                |
| :-------- | :-------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| value     | string    | The number that will be recorded in the lead sale. This field can also be computed, allowing any placeholders from the `"show"` block to be used in the expression. Available operations include +, -, \_, and /. You can also use parentheses, for example: `({{lead.cf.555123}} + 1){{lead.cf.555321}}`. |

### add\_linked\_company

`"add_linked_company" `action adds a company linked to a lead and a contact.

```json add_linked_company action
{
  "handler": "action",
  "params": {
    "name": "add_linked_company",
    "params": {
      "name": "{{message_text}}"
    }
  }
}
```

| Parameter | Data type | Description                                                    |
| :-------- | :-------- | :------------------------------------------------------------- |
| name      | string    | Company name, placeholders can be used from the `"show" `block |

### add\_note

`add_note `action adds a note.

```json add_note action
{
  "handler": "action",
  "params": {
    "name": "add_note",
    "params": {
      "element_type": 1,
      "note_type": 4,
      "text": "Note text"
    }
  }
}
```

<Table align={["left","left","left"]}>
  <thead>
    <tr>
      <th>
        Parameter
      </th>

      <th>
        Data type
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        element_type
      </td>

      <td>
        int
      </td>

      <td>
        The entity to which the note will be attached: 1 – contact, 2 – lead
      </td>
    </tr>

    <tr>
      <td>
        note_type
      </td>

      <td>
        int
      </td>

      <td>
        Type of note:  
        4 – regular note  
        10 – Incoming call note  
        11 – Outgoing call note  
        102 – Incoming text message note  
        103 – Outgoing SMS message note  
        25 – Service note
      </td>
    </tr>

    <tr>
      <td>
        text
      </td>

      <td>
        string
      </td>

      <td>
        Note text, placeholders from the `"show" `block can be used in it
      </td>
    </tr>
  </tbody>
</Table>

### link

The `"link"` action links elements.

```json link action
{
  "handler": "action",
  "params": {
    "name": "link",
    "params": {
      "from": 2,
      "to": 11,
      "to_id": "{{founded_id}}",
      "to_catalog_id": 123
    }
  }
}
```

| Parameter       | Data type | Description                                                                                                                                                                                                 |
| :-------------- | :-------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| from            | int       | The entity that the second entity will be linked to ( 1 – contact, 2 – lead)                                                                                                                                |
| from\_id        | int       | An optional parameter. The ID of the entity that the second entity will be linked to. If not specified, the ID of the current entity will be used. Placeholders from the `"show"` section can also be used. |
| to              | int       | The entity to be linked: 1 – contact, 2 – lead, 3 – company, 11 – list element.                                                                                                                             |
| to\_id          | string    | Id of the element to be linked, you can use the placeholders from the `"show" `section                                                                                                                      |
| to\_catalog\_id | int       | An optional parameter, list ID (if the list element is linked)                                                                                                                                              |
| quantity        | int       | An optional parameter, the number of list elements to be attached to the entity. Placeholders from the `"show"` section can be used.                                                                        |

### change\_responsible\_user

The action `change_responsible_user `changes the responsible user for a lead or the associated contact.

```json change_responsible_user action
{
  "handler": "action",
  "params": {
    "name": "change_responsible_user",
    "params": {
      "value": 123,
      "type": 2
    }
  }
}
```

| Parameter | Data type | Description                                                                                                                                          |
| :-------- | :-------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- |
| value     | int       | ID of the user you want to make responsible                                                                                                          |
| type      | int       | An optional parameter, the type of entity for which the responsible user is being changed: 1 – contact, 2 – lead. If not provided, the default is 2. |

### link\_to\_unsorted

The `"link_to_unsorted"` action links a chat from incoming leads to the specified contact in a lead. If the specified contact does not exist in the lead, the linking will not occur. If `contact_id `is not provided, a contact will be created in the specified lead.

```json link_to_unsorted action
{
  "handler": "action",
  "params": {
    "name": "link_to_unsorted",
    "params": {
      "entity_type": 2,
      "entity_id": "12345"
    }
  }
}
```

<br />

| Parameter    | Data type | Description                                                                                                                                                                                                                         |
| :----------- | :-------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| entity\_type | int       | Entity type ( 2 -lead)                                                                                                                                                                                                              |
| entity\_id   | string    | The ID of the entity to which the contact should be linked. Placeholders can be used, e.g. if a `"filter"` handler was used, you can use `{{external_lead.id}}`.                                                                    |
| contact\_id  | string    | An optional parameter. The ID of the contact to which the chat should be linked. The contact must be in the specified lead. Placeholders can be used, e.g. if a `"filter"` handler was used, you can use `{{external_contact.id}}`. |

## meta

The `"meta"` handler is designed to handle additional data that is sent when the chat is initiated.

For more information about sending meta-data, see the messenger documentation:

* [Telegram](https://core.telegram.org/bots#deep-linking)
* [Viber](https://developers.viber.com/docs/tools/deep-links/)
* [Messenger](https://developers.facebook.com/docs/messenger-platform/discovery/m-me-links)

```json meta handler
{
  "handler": "meta",
  "params": {
    "delimiter": "-",
    "values": [
      "lead.tags",
      "lead.custom_fields.123",
      "lead.custom_fields.124",
      "lead.tags"
    ]
  }
}
```

| Parameter | Data type | Description                                                                                                                          |
| :-------- | :-------- | :----------------------------------------------------------------------------------------------------------------------------------- |
| delimiter | string    | The character that will separate the incoming content into elements.                                                                 |
| values    | array     | An array of values where the elements of the incoming information will be stored. The values can be recorded in tags or lead fields. |

## condition

The `"condition"` handler is designed to create logical conditions.

```json condition handler
{
  "handler": "condition",
  "params": {
    "term1": "chat.origin",
    "term2": "telegram",
    "operation": "=",
    "result": [
      {
        "handler": "action",
        "params": {
          "name": "change_status",
          "params": {
            "value": 123
          }
        }
      }
    ]
  }
}
```

<Table align={["left","left","left"]}>
  <thead>
    <tr>
      <th>
        Parameter
      </th>

      <th>
        Data type
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        term1
      </td>

      <td>
        string
      </td>

      <td>
        Condition 1 can be:  
        `lead.tags` – which returns a list of tags,  
        `chat.origin` – which returns the source which the request came from (Facebook, Telegram, Viber),  
        any placeholder from the block described in the `"show"` handler section, for example `{{contact.name}}`
      </td>
    </tr>

    <tr>
      <td>
        term2
      </td>

      <td>
        string/obj
      </td>

      <td>
        Condition 2: for the `in_range` operator, you need to pass an object with parameters; otherwise, a string or placeholder from the `"show"` handler section should be provided.
      </td>
    </tr>

    <tr>
      <td>
        term2.from
      </td>

      <td>
        int
      </td>

      <td>
        The "from" value of the range.
      </td>
    </tr>

    <tr>
      <td>
        term2.to
      </td>

      <td>
        int
      </td>

      <td>
        The "to" value of the range.
      </td>
    </tr>

    <tr>
      <td>
        operation
      </td>

      <td>
        string
      </td>

      <td>
        The comparison operator: `"="`, `"! ="`, `"in"`, `"not_in"`, or `"in_range"`
      </td>
    </tr>

    <tr>
      <td>
        result
      </td>

      <td>
        array
      </td>

      <td>
        If the comparison is successful, the array of handlers
      </td>
    </tr>
  </tbody>
</Table>

### Operators

| Operator     | Description                                                                                                                                                                                                                 |
| :----------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `"="`        | The "=" operator checks if `"term1"` is equal to `"term2"`                                                                                                                                                                  |
| `"!="`       | The "!=" operator checks if `"term1"` is not equal to `"term2"`                                                                                                                                                             |
| `"in"`       | It takes strings from `"term1"` and `"term2"`, splits them into values by commas, and searches for occurrences of values from `"term2"` in `"term1"`. If there is at least one match, the result handlers will be executed. |
| `"not_in"`   | It takes strings from `"term1"` and `"term2"`, splits them into values by commas, and searches for occurrences of values from `"term2"` in `"term1"`. If there are no matches, the result handlers will be executed.        |
| `"in_range"` | It finds all integers in `"term1"`. If at least one of these numbers falls within the range specified by `"term2"`, the array of result handlers will be executed.                                                          |

## validations

The `"validations"` handler is designed to create logical conditions.

```json validations handler
{
  "handler": "validations",
  "params": {
    "logic": "and",
    "conditions": [
      {
        "client_value": "{{message_text}}",
        "type": "regex",
        "condition_value": "/[0-9]+/",
        "operation": "contains"
      },
      {
        "client_value": "{{message_text}}",
        "type": "simple",
        "condition_value": "654",
        "operation": "equal"
      },
      {
        "client_value": "{{message_text}}",
        "type": "range_numbers",
        "condition_value": {
          "from": 123,
          "to": 321
        },
        "operation": "contains"
      },
      {
        "client_value": "{{message_text}}",
        "type": "email",
        "condition_value": "",
        "operation": "contains"
      }
    ],
    "result": [
      {
        "handler": "goto",
        "params": {
          "type": "question",
          "step": 3
        }
      }
    ]
  }
}
```

| Parameters | Data type | Description                                                                                                                                                                 |
| :--------- | :-------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| logic      | string    | The logic for checking conditions is as follows: if `"and"` is specified, all conditions must be true; if `"or"` is specified, at least one of the conditions must be true. |
| conditions | array     | The list of conditions for checking is described below                                                                                                                      |
| result     | array     | Upon successful validation, the array of handlers will be executed.                                                                                                         |

### simple condition

A `"simple"` condition checks for equality/inequality or compares length

```json simple condition
{
  "client_value": "{{message_text}}",
  "type": "simple",
  "condition_value": "654",
  "operation": "equal"
}
```

<Table align={["left","left","left"]}>
  <thead>
    <tr>
      <th>
        Parameter
      </th>

      <th>
        Data type
      </th>

      <th>
        Description
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        client_value
      </td>

      <td>
        string
      </td>

      <td>
        The value being checked can be a placeholder from `"show"` handler section, for example, `"{{contact.name}}"`.
      </td>
    </tr>

    <tr>
      <td>
        type
      </td>

      <td>
        string
      </td>

      <td>
        It should be set to `"simple"`
      </td>
    </tr>

    <tr>
      <td>
        condition_value
      </td>

      <td>
        string
      </td>

      <td>
        It can be a a placeholder from `"show"` handler section (e.g. `"{{contact.name}}"`), or a custom value.
      </td>
    </tr>

    <tr>
      <td>
        operation
      </td>

      <td>
        string
      </td>

      <td>
        Can be `"equal"`, `"not_equal"`, or `"length"`.  
        If the `"equal"` operator is selected, the condition will be `true `if `"client_value"` equals `"condition_value"`. If the `"not_equal"` operator is selected, the condition will be `true` if `"client_value"` does not equal `"condition_value"`. If the `"length"` operator is selected, the condition will be `true` if the length of the string `"client_value"` is equal to `"condition_value"`.
      </td>
    </tr>
  </tbody>
</Table>

### email/phone condition

The `"email"`  and `"phone"` conditions check if a string contains a phone number or an email.

```json email condition
{
  "client_value": "{{message_text}}",
  "type": "email",
  "condition_value": "",
  "operation": "contains"
}
```

| Parameter        | Data type | Description                                                                                                                                                                                                                                                                                                        |
| :--------------- | :-------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| client\_value    | string    | The value being checked can be a placeholder from `"show"` handler section, for example, `"{{contact.name}}"`.                                                                                                                                                                                                     |
| type             | string    | The condition type can be `"email"` for searching an email or `"phone"` for searching a phone number.                                                                                                                                                                                                              |
| condition\_value | string    | It should be an empty string (`""`).                                                                                                                                                                                                                                                                               |
| operation        | string    | It can be `"contains"` or `"not_contains"`. If the `"contains"` operator is selected, the condition will be `true` if `"client_value"` contains an email or phone number. If the `"not_contains"` operator is selected, the condition will be `true` if `"client_value"` doesn't contain an email or phone number. |

### regex condition

A `"regex"` condition checks if a string contains a regular expression.

```json regex condition
{
  "client_value": "{{message_text}}",
  "type": "regex",
  "condition_value": "/[0-9]+/",
  "operation": "contains"
}
```

| Parameter        | Data type | Description                                                                                                                                                                                                                                                                                                    |
| :--------------- | :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| client\_value    | string    | The value being checked can be a placeholder from `"show"` handler section, for example, `"{{contact.name}}"`.                                                                                                                                                                                                 |
| type             | string    | It should be "regex".                                                                                                                                                                                                                                                                                          |
| condition\_value | string    | Text of the condition.                                                                                                                                                                                                                                                                                         |
| operation        | string    | It can be `"contains"` or `"not_contains"`. If the `"contains"` operator is selected, the condition will be `true` if `"client_value"` contains the regular expression. If the `"not_contains"` operator is selected, the condition will be `true` if `"client_value"` doesn't contain the regular expression. |

### range\_numbers condition

A `"range_numbers"` condition checks if a string contains a number within a specified range.

```json range_numbers condition
{
  "client_value": "{{message_text}}",
  "type": "range_numbers",
  "condition_value": {
    "from": 123,
    "to": 321
  },
  "operation": "contains"
}
```

| Parameter             | Data type      | Description                                                                                                                                                                                                                                                                                                                              |
| :-------------------- | :------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| client\_value         | string         | The value being checked can be a placeholder from `"show"` handler section, for example, `"{{contact.name}}"`.                                                                                                                                                                                                                           |
| type                  | string         | It should be `“range_numbers”`.                                                                                                                                                                                                                                                                                                          |
| condition\_value      | obj            | The range in which we check all numbers in `"client_value"`.                                                                                                                                                                                                                                                                             |
| condition\_value.from | string, number | The `"from"` range value can be specified as a placeholder from `"show"` handler section (for example, `"{{lead.price}}"`), or as a custom value.                                                                                                                                                                                        |
| condition\_value.to   | string, number | The `"to"` range value can be specified as a placeholder from `"show"` handler section (for example, `"{{lead.price}}"`), or as a custom value.                                                                                                                                                                                          |
| operation             | string         | It can be `"contains"` or `"not_contains"`. If the `"contains"` operator is selected, the condition will be `true` if `"client_value"` contains a number within the specified range. If the `"not_contains"` operator is selected, the condition will be `true` if `"client_value"` doesn't contain a number within the specified range. |

## preset

The `"preset"` handler is designed to process incoming messages using predefined templates.

| Parameter | Type   | Description               |
| :-------- | :----- | :------------------------ |
| name      | string | Name of the handler       |
| params    | array  | Parameters of the handler |

### contacts.validate\_base\_info

Preset `"contacts.validate_base_info"` allows you to obtain and verify information from the user, and then request any missing information.

```json contacts.validate_base_info preset
{
  "handler": "preset",
  "params": {
    "name": "contacts.validate_base_info",
    "params": {
      "empty_email": "Please provide your email.",
      "empty_phone": "Please provide your phone number.",
      "invalid_phone": "It seems that there is a mistake in the phone number.",
      "success": "Thank you",
      "empty_all": "Please provide your email and phone number",
      "check_doubles": true,
      "phone_doubles": "This phone number is already in use. Please enter a different number.",
      "email_doubles": "This email is already in use. Please enter a different email.",
      "all_doubles": "This phone number and email are already in use. Please enter different contact information.",
      "use_quick_replies": true
    }
  }
}
```

| Parameter           | Type   | Description                                                                                                                                                                                                                                                                                                                                                               |
| :------------------ | :----- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| empty\_email        | string | Message in the absence of email from the contact                                                                                                                                                                                                                                                                                                                          |
| empty\_phone        | string | Message in the absence of a phone from a contact                                                                                                                                                                                                                                                                                                                          |
| invalid\_phone      | string | Message when receiving an invalid phone number                                                                                                                                                                                                                                                                                                                            |
| success             | string | Message after receiving all data                                                                                                                                                                                                                                                                                                                                          |
| empty\_all          | string | Message in the absence of all data                                                                                                                                                                                                                                                                                                                                        |
| check\_doubles      | bool   | A parameter that determines whether to enable the search for duplicate contacts based on email and phone number.If enabled, when a duplicate contact is found, the contact information will be requested again. `true` means enabled, while `false` or absence means disabled.                                                                                            |
| phone\_doubles      | string | The message that will be displayed if a duplicate contact is found based on the phone number.                                                                                                                                                                                                                                                                             |
| email\_doubles      | string | The message that will be displayed if a duplicate contact is found based on the email.                                                                                                                                                                                                                                                                                    |
| all\_doubles        | string | The message that will be displayed if duplicates are found for the contact based on both the phone number and email.                                                                                                                                                                                                                                                      |
| use\_quick\_replies | string | A parameter that sends [quick reply buttons](https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies)  when the contact is missing an email and/or phone number. This is only available for Facebook and displays buttons containing the user's Facebook account information (phone, email). To enable this feature, set the value to `true`. |

### contacts.get\_base\_info

Preset `"contacts.get_base_info"` allows you to obtain information without asking additional questions.

```json contacts.get_base_info preset
{
  "handler": "preset",
  "params": {
    "name": "contacts.get_base_info"
  }
}
```

## send\_internal

The `"send_internal"` handler allows you to send an internal message in the lead chat.

<Callout icon="🚧" theme="warn">
  If both `group_id` and `user_id` are specified simultaneously, the message will be sent to the group of users.
</Callout>

```json send_internal handler
{
  "handler": "send_internal",
  "params": {
    "entity_id": "{{lead.id}}",
    "entity_type": 2,
    "message": "Hi there!"
  }
}
```

| Parameter    | Data type     | Description                                                                                        |
| :----------- | :------------ | :------------------------------------------------------------------------------------------------- |
| entity\_id   | int or string | The ID of the lead the message will be sent to. A placeholder such as `"{{lead.id}}"` can be used. |
| entity\_type | int           | A type of the entity the message will be sent to. Only 2 (lead) is available.                      |
| message      | string        | A string with a message                                                                            |
| group\_id    | int           | An optional parameter, the ID of the user group to which the message should be sent.               |
| user\_id     | int           | An optional parameter, the ID of the user to whom the message should be sent.                      |

## send\_external\_message

The `send_external_message` handler sends a message/buttons to the client chat. The main difference from the `show` handler is the ability to specify recipients and chat channels of the sent message.

```json Text
{
    "handler": "send_external_message",
    "params": {
        "message": {
            "type": "external",
            "text": "Provide your phone number and email"
        },
        "recipient": {
            "type": "main_contact",
            "way_of_communication": "over_all"
        },
        "channels": [
            {
                "id": 23499795
            }
        ],
        "metadata": {
            "facebook_tag": "CONFIRMED_EVENT_UPDATE"
        },
        "on_error": {
            "handler": "goto",
            "params": {
                "step": 1,
                "type": "question"
            }
        }
    }
}
```
```json Buttons
{
    "handler": "send_external_message",
    "params": {
        "message": {
            "type": "external",
            "text": "Choose the option:",
            "buttons": [
                {
                    "type": "inline",
                    "text": "Option 1"
                },
                {
                    "type": "inline",
                    "text": "Option 2"
                }
            ]
        },
        "recipient": {
            "type": "main_contact",
            "way_of_communication": "over_all"
        },
        "channels": [
            {
                "id": 23499795
            }
        ],
        "metadata": {
            "facebook_tag": "CONFIRMED_EVENT_UPDATE"
        },
        "on_error": {
            "handler": "goto",
            "params": {
                "step": 1,
                "type": "question"
            }
        }
    }
}

```
```json Buttons with URLs
{
    "handler": "send_external_message",
    "params": {
        "message": {
            "type": "external",
            "text": "Buttons with links",
            "buttons": [
                {
                    "type": "url",
                    "text": "Google",
                    "url": "https://google.com"
                },
                {
                    "type": "url",
                    "text": "YouTube",
                    "url": "https://youtube.com"
                }
            ]
        },
        "recipient": {
            "type": "main_contact",
            "way_of_communication": "over_all"
        },
        "channels": [],
        "metadata": {
            "facebook_tag": "CONFIRMED_EVENT_UPDATE"
        },
        "on_error": null
    }
}
```
```json Text message with filtering by contact field value
{
    "handler": "send_external_message",
    "params": {
        "message": {
            "type": "external",
            "text": "Provide your phone number and email"
        },
        "recipient": {
            "type": "filtered_contacts",
            "filter": {
                "logic": "and",
                "result": 1,
                "conditions": [
                    {
                        "term1": "{{contact.cf.1880614.is_checked}}",
                        "term2": "{{contact.cf.1880614.1300836}}",
                        "operation": "=",
                        "value_type": "value"
                    }
                ]
            },
            "way_of_communication": "over_all"
        },
        "channels": [],
            "metadata": {
            "facebook_tag": "CONFIRMED_EVENT_UPDATE"
        },
        "on_error": {
            "handler": "goto",
            "params": {
                "step": 1,
                "type": "question"
            }
        }
    }
}
```

**Text message**

| Parameter                        | Data type | Description                                                                                                                                                                                                         |
| :------------------------------- | :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| message                          | array     | Sending text, buttons or buttons with links                                                                                                                                                                         |
| message.type                     | string    | Message type                                                                                                                                                                                                        |
| message.text                     | string    | Text of the message                                                                                                                                                                                                 |
| recipient                        | array     | Message recipient                                                                                                                                                                                                   |
| recipient.type                   | string    | Recipient type. There are 3 options – `all_contacts`(all contacts), `main_contact`(main contact) and `filtered_contacts`(contacts filtered by field value)                                                          |
| recipient.way\_of\_communication | string    | Communication method. Available options are `over_all` (all found), `any_first` (first found) and `last_active` (last active)                                                                                       |
| channels                         | array     | An array of the sources IDs to which a message needs to be sent to the client                                                                                                                                       |
| metadata                         | array     | Metadata sent with the message                                                                                                                                                                                      |
| metadata.facebook\_tag           | string    | A tag that allows your Page to send a message to a person outside of the standard 24-hour message window. [Available values](https://developers.facebook.com/docs/messenger-platform/reference/send-api/#---------) |
| on\_error                        | array     | Block that runs the specified step when there is an error sending the message                                                                                                                                       |

**Message with buttons**

| Parameter                        | Data type | Description                                                                                                                                                                                                         |
| :------------------------------- | :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| message                          | array     | Sending text, buttons or buttons with links                                                                                                                                                                         |
| message.type                     | string    | Message type                                                                                                                                                                                                        |
| message.text                     | string    | Text of the message                                                                                                                                                                                                 |
| message.buttons                  | array     | An array whose elements are the texts of the buttons that will be sent                                                                                                                                              |
| message.buttons.type             | string    | Button type                                                                                                                                                                                                         |
| message.buttons.text             | string    | Button text                                                                                                                                                                                                         |
| recipient                        | array     | Message recipient                                                                                                                                                                                                   |
| recipient.type                   | string    | Recipient type. There are 3 options – `all_contacts`(all contacts), `main_contact`(main contact) and `filtered_contacts`(contacts filtered by field value)                                                          |
| recipient.way\_of\_communication | string    | Communication method. Available options are `over_all` (all found), `any_first` (first found) and `last_active` (last active)                                                                                       |
| channels                         | array     | An array of the sources IDs to which a message needs to be sent to the client                                                                                                                                       |
| metadata                         | array     | Metadata sent with the message.                                                                                                                                                                                     |
| metadata.facebook\_tag           | string    | A tag that allows your Page to send a message to a person outside of the standard 24-hour message window. [Available values](https://developers.facebook.com/docs/messenger-platform/reference/send-api/#---------) |
| on\_error                        | array     | Block that runs the specified step when there is an error sending the message                                                                                                                                       |

**Buttons with link message**

| Parameter                        | Data type | Description                                                                                                                                                                                                         |
| :------------------------------- | :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| message                          | array     | Sending text, buttons or buttons with links                                                                                                                                                                         |
| message.type                     | string    | Message type                                                                                                                                                                                                        |
| message.text                     | string    | Text of the message                                                                                                                                                                                                 |
| message.buttons                  | array     | An array whose elements are the texts of the buttons that will be sent                                                                                                                                              |
| message.buttons.type             | string    | Button type                                                                                                                                                                                                         |
| message.buttons.text             | string    | Button text                                                                                                                                                                                                         |
| message.buttons.url              | string    | Link to the external resource                                                                                                                                                                                       |
| recipient                        | array     | Message recipient                                                                                                                                                                                                   |
| recipient.type                   | string    | Recipient type. There are 3 options – `all_contacts`(all contacts), `main_contact`(main contact) and `filtered_contacts`(contacts filtered by field value)                                                          |
| recipient.way\_of\_communication | string    | Communication method. Available options are `over_all` (all found), `any_first` (first found) and `last_active` (last active)                                                                                       |
| channels                         | array     | An array of the sources IDs to which a message needs to be sent to the client                                                                                                                                       |
| metadata                         | array     | Metadata sent with the message.                                                                                                                                                                                     |
| metadata.facebook\_tag           | string    | A tag that allows your Page to send a message to a person outside of the standard 24-hour message window. [Available values](https://developers.facebook.com/docs/messenger-platform/reference/send-api/#---------) |
| on\_error                        | array     | Block that runs the specified step when there is an error sending the message                                                                                                                                       |

If you want to send links to social networks and for automatic gluing to occur, the links must be in the following format:

* Facebook Messenger – <https://m.me/123/?ref=VisitorUid_{{visitor_uid}}> , where 123 is the group ID
* Telegram – tg://resolve?domain=bot\&start=VisitorUid\_\{\{visitor\_uid}} , where bot is the bot name
* Viber – viber://pa?chatURI=bot\&context=VisitorUid\_\{\{visitor\_uid}} , where bot is the public account name
* WhatsApp – <https://wa.me/7895?text=ID:%20{{session_id}}> , where 7895 is the WhatsApp phone number that is connected to the account

**Text message with filtering by the value of the contact field**

| Parameter                        | Data type | Description                                                                                                                                                                                                                                               |
| :------------------------------- | :-------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| message                          | array     | Sending text, buttons or buttons with links                                                                                                                                                                                                               |
| message.type                     | string    | Message type                                                                                                                                                                                                                                              |
| message.text                     | string    | Text of the message                                                                                                                                                                                                                                       |
| recipient                        | array     | Message recipient                                                                                                                                                                                                                                         |
| recipient.type                   | string    | Recipient type. There are 3 options – `all_contacts`(all contacts), `main_contact`(main contact) and `filtered_contacts`(contacts filtered by field value)                                                                                                |
| recipient.way\_of\_communication | string    | Communication method. Available options are `over_all` (all found), `any_first` (first found) and `last_active` (last active)                                                                                                                             |
| recipient.filter                 | array     | Setting up a filter by the value of contact fields. It is configured and works the same way as with the `[condition ]\(Setting up a filter by the value of contact fields. It is configured and works the same way as with the condition handler)`handler |
| channels                         | array     | An array of the sources IDs to which a message needs to be sent to the client                                                                                                                                                                             |
| metadata                         | array     | Metadata sent with the message                                                                                                                                                                                                                            |
| metadata.facebook\_tag           | string    | A tag that allows your Page to send a message to a person outside of the standard 24-hour message window. [Available values](https://developers.facebook.com/docs/messenger-platform/reference/send-api/#---------)                                       |
| on\_error                        | array     | Block that runs the specified step when there is an error sending the message                                                                                                                                                                             |

## widget\_request

The `widget_request` handler is designed to send webhooks to external URLs from Salesbot.

<Callout icon="🚧" theme="warn">
  You can use this handler only from the **Widget** step of Salesbot. Find additional information on widget in Salesbot [here](https://developers.kommo.com/docs/salesbot-sdk).
</Callout>

<Image align="center" border={true} width="400px" src="https://files.readme.io/0c520ff0dd39a2b984dd4454085a767bab7a5c47f0e98eabbc2baa59f1dacdf2-Group_881234720.png" className="border" />

```json widget_request
{
  "handler": "widget_request",
  "params": {
    "url": "https://example.com/endpoint",
    "data": {
      "contact": "{{contact.name}}",
      "from": "widget"
    }
  }
}
```
```json Example
onSalesbotDesignerSave: function (handler_code, params) {
  var request_data = {
    message: params.message,
  };

  if (APP.getBaseEntity() === 'lead') {
    request_data.lead = '{{lead.id}}';
  };

  return JSON.stringify([
    {
      question: [
        {
          handler: 'widget_request',
          params: {
            url: 'https://example.com/webhook',
            data: request_data,
          },
        },
        {
          handler: 'goto',
          params: {
            type: 'question',
            step: 1,
          },
        },
      ],
    },
    {
      question: [
        {
          handler: 'conditions',
          params: {
            logic: 'and',
            conditions: [
              {
                term1: '{{json.status}}',
                term2: 'success',
                operation: '=',
              },
            ],
            result: [
              {
                handler: 'exits',
                params: {
                  value: 'success',
                },
              },
            ],
          },
        },
        {
          handler: 'exits',
          params: {
            value: 'fail',
          },
        },
      ],
    },
  ]);
},

```

| Parameter | Data type | Decsription                                                                                                                    |
| :-------- | :-------- | :----------------------------------------------------------------------------------------------------------------------------- |
| url       | string    | The URL endpoint of the external server.                                                                                       |
| data      | array     | An array of any data containing strings and/or placeholders  from `"show"` handler section, for example, `"{{contact.name}}"`. |

The endpoint will receive a POST request. To acknowledge that the webhook has been received, you need to respond within 2 seconds with an HTTP status code 200.

```json request body example
{
      "token": "JWT_TOKEN",
      "data": {
          "contact": "Contact name",
          "from": "widget"
      },
      "return_url": "https://subdomain.kommo.com/api/v4/salesbot/321/continue/123"
    }
```

### JWT validation

The [JWT](https://developers.kommo.com/docs/one-time-tokens) is needed to validate the data sent in the request. It is signed with the client (integration) secret key. Here are the JWT fields description:

| Parameter    | Description                                                     |
| :----------- | :-------------------------------------------------------------- |
| account\_id  | Account ID where the Salesbot handler has been triggered        |
| subdomain    | Account subdomain where the Salesbot handler has been triggered |
| entity\_type | Entity type for which the Salesbot works                        |
| entity\_id   | Entity ID for which the Salesbot works                          |
| client\_uid  | Integration ID of the Salesbot widget                           |

### Widget Response and Resuming Bot Operations

To resume the bot's operation, you need to [make a request with the data](https://developers.kommo.com/reference/salesbot-widget-block-execution-confirmation). The current bot will not continue its operation until it receives the request. Additionally, you will not be able to continue the bot's execution if another bot for the same entity is already running.

## stop

The `"stop"` handler is intended to perform actions when the bot is finished.

```json stop handler
{
  "finish": [
    {
      "handler": "stop",
      "params": {
        "action": "talk-close"
      }
    },
    {
      "handler": "stop",
      "params": {
        "action": "salesbot-start",
        "bot": 1234
      }
    }
  ]
}
```

| Parameter | Data type | Description                                                                                                                                                                                                                                        |
| :-------- | :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| action    | string    | It can be `"talk-close"` or `"salesbot-start"`. `"talk-close"` ends the conversation. If this is executed within the NPS bot, the conversation will be closed; otherwise, the NPS bot will be initiated. `"salesbot-start"`  launches another bot. |
| bot       | int       | The parameter is mandatory if the action selected is `"salesbot-start"`. It specifies the ID of the bot that should be launched.                                                                                                                   |

# Examples

Subscribing a group of users to chat.

```json
[
    {
        "question": [
            {
                "handler": "action",
                "params": {
                    "name": "subscribe",
                    "params": {
                        "type": "group",
                        "value": 111
                    }
                }
            }
        ]
    }
]
```

Moving the lead to another stage.

```json
[
    {
        "question": [
            {
                "handler": "action",
                "params": {
                    "name": "change_status",
                    "params": {
                        "value": 142
                    }
                }
            }
        ]
    }
]
```

Sending any text to the client:

```json
[
    {
        "question": [
            {
                "handler": "show",
                "params": {
                    "type": "text",
                    "value": "Hello"
                }
            }
        ]
    }
]
```

Sending a message with the selection buttons:

```json
[
    {
        "question": [
            {
                "handler": "show",
                "params": {
                    "type": "buttons",
                    "value": "Please choose the type of participation:",
                    "buttons": [
                        "Offline",
                        "Online"
                    ]
                }
            }
        ],
        "answer": [
            {
                "handler": "buttons",
                "params": [{
                    "regex": "/offline/iu",
                    "params": [{
                        "handler": "action",
                        "params": {
                            "name": "set_custom_fields",
                            "params": {
                                "type": 2,
                                "value": "Offline",
                                "custom_fields_id": 4242
                            }
                        }
                    }]
                },
                    {
                    "regex": "/online/iu",
                    "params": [{
                        "handler": "action",
                        "params": {
                            "name": "set_custom_fields",
                            "params": {
                                "type": 2,
                                "value": "Online",
                                "custom_fields_id": 4242
                            }
                        }
                    }]
                }]
            }]
    }
]
```

Setting a tag for a lead:

```json
[
    {
        "question": [
            {
                "handler": "action",
                "params": {
                    "name": "set_tag",
                    "params": {
                        "type": 2,
                        "value": "salesbot"
                    }
                }
            }
        ]
    }
]
```

Setting a value to a custom field:

```json
[
    {
        "question": [
            {
                "handler": "action",
                "params": {
                    "name": "set_custom_fields",
                    "params": {
                        "type": 2,
                        "custom_fields_id": 123,
                        "value": "Field Value"
                    }
                }
            }
        ]
    }
]
```

Saving metadata to the lead card.

```json
[
    {
        "question": [
            {
                "handler": "meta",
                "params": {
                    "delimiter": "-",
                    "values": [
                        "lead.tags",
                        "lead.custom_fields.123",
                        "lead.custom_fields.124",
                        "lead.tags"
                    ]
                }
            }
        ]
    }
]
```

Requesting email and phone number, recording in the lead card only from the first response.

```json
[
    {
        "question": [
            {
                "handler": "show",
                "params": {
                    "type": "text",
                    "value": "Please provide your phone number and email."
                }
            }
        ],
        "answer": [
            {
                "handler": "preset",
                "params": {
                    "name": "contacts.get_base_info"
                }
            }
        ]
    }
]
```