# Integration with an external messenger

*You can set up integration with an external messenger using webhooks. A webhook is sending requests based on events.*

### Integration setup

1. Create a channel of the «Page messenger» type.

In the control panel, go to the «[*Channels*](https://selarti.com/panel?menu=channels)» section and click the «*Add channel*» button located in the upper right corner of the screen.

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2FPphTcLGDHKi9G1ahTsW9%2Fpage_mess_1.png?alt=media&#x26;token=5a3fd944-cbf2-467c-baa1-5e360759efaf" alt=""><figcaption></figcaption></figure>

Next, in the «Add channel» window, select the channel type «*Page messenger*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2F5RcZ2vBYjn0TvRfynuqp%2Fpage_mess_2.png?alt=media&#x26;token=f7e46430-34ad-4c97-94b7-e9fb45fc9b28" alt="" width="458"><figcaption></figcaption></figure>

Specify any required parameters below and click «*Add channel*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2FKhZhdFASHGmG7mesZ2he%2Fpage_mess_3.png?alt=media&#x26;token=5de5d523-633b-467a-bd35-4d4c698c88db" alt="" width="458"><figcaption></figcaption></figure>

More about creating channels in the article «[Channels](https://manual.selarti.com/manual-en/main-sections-of-the-service/channels)».

2. Create AI for incoming messages.

To create AI, go to the «[*AI*](https://selarti.com/panel?menu=ai)» section and click the «*Create AI*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2FrY0vlkClYJX60gkJExYz%2Fpage_mess_4.png?alt=media&#x26;token=b284c764-dcd8-44ff-873b-f5ba5a84c6fb" alt=""><figcaption></figcaption></figure>

More in the article «[AI](https://manual.selarti.com/manual-en/main-sections-of-the-service/ai)»[.](https://manual.selarti.com/manual-en/main-sections-of-the-service/ai)

3. Create an integration of the «All messages» type to receive webhooks about new messages.

To create an integration of the «All messages» type, go to the «[*Integration*](https://selarti.com/panel?menu=integration)» section and click the «*Create integration*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2FgbZl4QOQeY84UmnPcQKX%2Fpage_mess_5.png?alt=media&#x26;token=01c2a9c1-aebf-4c88-9f12-1838c155359b" alt=""><figcaption></figcaption></figure>

In the «Add integration» window, select the integration type «*Another*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2FQZH50DrV4pJc7d1JK8s7%2Fpage_mess_6.png?alt=media&#x26;token=486c2f31-ebc0-46fe-ba31-781de2b8b41b" alt="" width="461"><figcaption></figcaption></figure>

In the «Name» field, enter a name. In the «webhook url» field, enter the URL to receive webhooks. Then in the dropdown list «*webhook type*» select «*All messages*». Click «*Create integration*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2FY6B9sGUD5AEvHCfzEnpi%2Fpage_mess_7.png?alt=media&#x26;token=c440a993-3392-4208-b40d-1dde26a558a6" alt="" width="461"><figcaption></figcaption></figure>

More in the article «[Integration](https://manual.selarti.com/manual-en/main-sections-of-the-service/integration)».

4. Create a task of the «Web» type.

To create a task of the «Web» type, go to the «[*Tasks*](https://selarti.com/panel?menu=tasks)» section and click the «*Create task*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2FlbMzlZxR392Opkt6YKcW%2Fpage_mess_8.png?alt=media&#x26;token=00bfa39d-37a1-42f5-897c-d34cb6565bfd" alt=""><figcaption></figcaption></figure>

In the «Create task» window, in the «Type» dropdown, select «*Web*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2FOJeJThFcCAINvKNDRpMb%2Fpage_mess_9.png?alt=media&#x26;token=bcafe341-5aeb-47af-aeb9-7156c71065e3" alt="" width="461"><figcaption></figcaption></figure>

Next, select the service operation format, the previously created channel, AI, integration, and click the «*Create task*».

<figure><img src="https://271796707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsBua8zDbkZPvmgjidnwA%2Fuploads%2F1QQ97FZUiskjFVo6pPu3%2Fpage_17.03.26_en.png?alt=media&#x26;token=18d1fab4-8e7e-4b5c-92a3-58f1e4540ae9" alt="" width="467"><figcaption></figcaption></figure>

After creation, copy the task id, you will need it for API requests.

More in the article «[Tasks](https://manual.selarti.com/manual-en/main-sections-of-the-service/tasks)»[.](https://manual.selarti.com/manual-en/main-sections-of-the-service/tasks)

5. Integrate the method for sending a message from the user to AI:

## send message external

> 🇬🇧 Send external message\
> \
> 🇷🇺 Отправить внешнее сообщение\
> \
> 🇺🇿 Tashqi xabar yuborish

```json
{"openapi":"3.0.3","info":{"title":"Selarti public api","version":"1.0.0"},"tags":[{"name":"Send message","description":"**Base URL:** https://selarti.com"}],"servers":[{"url":"https://selarti.com","description":"Main API server (for /api/1.1/wf/* endpoints)"},{"url":"https://api.selarti.com","description":"Advanced API server (for /v1/* endpoints)"}],"security":[{"bearerAuth":[]}],"components":{"securitySchemes":{"bearerAuth":{"type":"http","scheme":"bearer"}}},"paths":{"/api/1.1/wf/send_message_external":{"post":{"tags":["Send message"],"summary":"send message external","description":"🇬🇧 Send external message\n\n🇷🇺 Отправить внешнее сообщение\n\n🇺🇿 Tashqi xabar yuborish","operationId":"sendMessageExternal","requestBody":{"content":{"application/json":{"schema":{"type":"object","properties":{"task_id":{"type":"string"},"chat_id":{"type":"string","description":"unique chat_id in your system"},"message":{"type":"string"}},"required":["task_id","chat_id","message"]}}}},"responses":{"200":{"description":"example","headers":{"CF-Cache-Status":{"schema":{"type":"string"}},"CF-RAY":{"schema":{"type":"string"}},"Connection":{"schema":{"type":"string"}},"Content-Encoding":{"schema":{"type":"string"}},"Date":{"schema":{"type":"string"}},"Server":{"schema":{"type":"string"}},"Transfer-Encoding":{"schema":{"type":"string"}},"access-control-allow-origin":{"schema":{"type":"string"}},"alt-svc":{"schema":{"type":"string"}},"vary":{"schema":{"type":"string"}}},"content":{"application/json":{"schema":{"type":"object","properties":{"response":{"type":"object","properties":{"thread":{"type":"object","properties":{"Created By":{"type":"string"},"Created Date":{"type":"number"},"Modified Date":{"type":"number"},"_id":{"type":"string"},"ai_thread_id":{"type":"string"},"chat_id":{"type":"string"},"close":{"type":"boolean"},"company":{"type":"string"},"contact":{"type":"string"},"count_message":{"type":"number"},"get_contact":{"type":"boolean"},"interest":{"type":"boolean"},"is_bot":{"type":"boolean"},"operator":{"type":"boolean"},"success":{"type":"boolean"},"task":{"type":"string"},"title":{"type":"string"},"user_replied":{"type":"boolean"},"wh_get_contact":{"type":"boolean"},"wh_interest":{"type":"boolean"},"wh_success":{"type":"boolean"}}}}},"status":{"type":"string"}}}}}}}}}}}
```

Pass the unique user identifier, for example, Telegram user\_id, in chat\_id. The dialog context will be combined by this id, and you will be able to identify webhooks.

6. Receive a webhook after the AI generates a message and send the message to the user in the dialog.

### List of parameters and description

| Parameter          | Parameter description                                                                                                                                                                                        |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| channel\_id        | i**d of the channel**. Used to identify the specific channel through which communication is carried out. May be useful for routing and processing messages on your side.                                     |
| close              | **Dialog closed/not closed**. Used to indicate the closing of a dialog or completion of communication. It signals the end of the communication session with the user or the closure of a specific task.      |
| company\_id        | **company id**. Used to identify the organization to which the incoming message or command belongs. Allows accurate processing of data in the context of a specific company or department within the system. |
| contacts           | **Contact**. Used to transmit contact information, such as phone, username, etc. Allows automatic updating and synchronization of contact data between systems.                                              |
| external\_chat\_id | **dialog id**. Used to identify a specific chat or dialog in an external system or messenger. Allows linking messages and interactions with the required external dialog.                                    |
| get\_contact       | **Contact received/not received**. Informs that the user sent a phone, email, etc. in the dialog.                                                                                                            |
| interest           | **Interest shown yes/no**. Used to indicate the client's interest or activation of interest in a specific product or service.                                                                                |
| is\_bot            | <p><strong>Dialog is with a bot yes/no</strong>.<br>If yes, our system has determined that this dialog is with a bot.</p>                                                                                    |
| message\_id        | **message id**. Used for the unique identification of a specific message. Allows you to track, process, and refer to specific messages in the system.                                                        |
| message\_text      | **Message text**. Used to transmit the message text. It allows you to receive and process the content of the message sent by the user or system in the required format.                                      |
| message\_type      | Message type bot/operator/user.                                                                                                                                                                              |
| operator           | **Dialog transferred to operator yes/no.**                                                                                                                                                                   |
| panel\_url         | Link to the dialog in the personal account with the ability to reply to the user.                                                                                                                            |
| share\_url         | Link to the dialog for viewing only, without the ability to reply.                                                                                                                                           |
| success            | Dialog is **successful yes/no**.                                                                                                                                                                             |
| task\_id           | **task id**. Used for the unique identification of a task in the system.                                                                                                                                     |
| thread\_id         | **thread id**. Used to identify the dialog with the user.                                                                                                                                                    |
| title              | **Title**. Full name of the user.                                                                                                                                                                            |
| wh\_type           | **Webhook type**. Indicates the type of webhook, events, or actions. It helps to distinguish which event occurred or what information is being transmitted via the webhook for appropriate processing.       |

#### Webhook example:

<pre class="language-bison"><code class="lang-bison"><strong>{
</strong>  "channel_id": "5q",
  "close": "no",
  "company_id": "1708251850060x460235863640434700",
  "contacts": "123456789",
  "external_chat_id": "123456789", // Your dialog id, you can identify by it
  "get_contact": "no",
  "interest": "no",
  "is_bot": "no",
  "manager_id": "4q",
  "message_id": "1731336491876x582725124419040400",
  "message_text": "It looks like you are testing API functionality by sending a test message along with metadata like a timestamp and timezone. If you have any questions or need assistance with this, feel free to ask!",
  "message_type": "bot", // Send only bot and operator types to the dialog with the user. You can ignore a webhook with the user type by responding with status 200 
  "operator": "no",
  "panel_url": "https://selarti.com/panel?menu=chats&#x26;thread=1731336491572x316716105037601100",
  "share_url": "https://selarti.com/share?thread=1731336491572x316716105037601100",
  "success": "no",
  "task_id": "1721129985149x478019552540622850",
  "thread_id": "1731336491572x316716105037601100",
  "title": "API chat 123456789",
  "wh_type": "all_messages" // Set up processing only for this type of webhook, as webhooks of other types may be sent to your url 
}
</code></pre>

{% hint style="info" %}
A new parameter panel\_url has appeared. It contains a link to the dialog in your personal account. Unlike share\_url, where you can only view the dialog, with panel\_url you can reply to the user directly. Note that access requires authorization and company access rights.
{% endhint %}

7. Try replying to the user through your personal account.
