# Integrate chatbots

Source: [Integrate chatbots](<https://docs.gptunnel.ru/en/getting-started/bot-integration/>)

<a id="_top"></a>
<a id="working-principle"></a>
## Working principle

Use webhooks to connect a GPTunneL bot, such as a support bot, to an external chat service. Requests use HTTP POST with the integration token in the `Authorization` header and a JSON body.

![Integration scheme](<https://docs.gptunnel.ru/assets/schema.webp>)

When a user sends a message, the chat service posts `CLIENT_MESSAGE` to the bot’s webhook. The bot processes it and sends `BOT_MESSAGE` to the chat service’s webhook.

If the bot needs to hand the conversation to a human agent, it sends `INVITE_AGENT`.

<a id="authorisation-and-errors"></a>
## Authorisation and errors

For all three methods, send the integration token in the `Authorization` header without the `Bearer` prefix. For the examples below, save that token in the `GPTUNNEL_BOT_TOKEN` environment variable.

| HTTP status | When it occurs |
| --- | --- |
| **401** | The token is missing, invalid, revoked or does not identify an active bot and its owner unambiguously. Deleting the bot or its owner’s account also ends access. |
| **403** | For `/api/bot/balance`, the bot owner lacks active membership in the linked organization or the organization has been deleted. |
| **400** | The JSON could not be parsed or the `/api/bot` event has an invalid format. `CLIENT_MESSAGE` requires a nonempty string `chat_id` and a `message` object. |
| **413** | The body of an authorized request exceeds the permissible size. |

On **401**, `/api/bot/config` and `/api/bot/balance` return the following body.

```json
{ "ok": false, "message": "Authorization required!" }
```

The **401** response from `/api/bot` has an empty body. On **403**, `/api/bot/balance` returns `{ "ok": false, "message": "Access denied!" }` without a balance.

Check the HTTP status before parsing a successful response. If your HTTP client throws on 4xx, inspect the status and body in the error handler. Authorization failures use their HTTP status. An authorized `/api/bot/config` request with an invalid `webHookUrl` still returns **200** with `ok: false` and an explanation in `message`.

<a id="address-for-events"></a>
## Address for events

`POST https://gptunnel.ru/api/bot`

```bash
curl -X POST https://gptunnel.ru/api/bot \
  -H "Authorization: ${GPTUNNEL_BOT_TOKEN:?Set GPTUNNEL_BOT_TOKEN}" \
  -H 'Content-Type: application/json' \
  -d '{
    "event": "CLIENT_MESSAGE",
    "id": "9f7b373e-fce1-11ee-9da4-a912f79ee445",
    "chat_id": "4976",
    "client_id": "30005",
    "message": {
      "type": "TEXT",
      "text": "Добрый день",
      "timestamp": 1713375646
    },
    "agents_online": false
  }'
```

When the event is accepted, the API returns **200** with an empty body. The bot processes the event and sends `BOT_MESSAGE` or `INVITE_AGENT` to the chat service’s webhook.

> **Caution: Funds required for bot responses**
>
> The bot needs available funds to respond. The organization’s credit limit counts toward those funds. For an organization bot, check `canSpend` in the balance response.

> **Note: Webhook URL for replies**
>
> Set the reply URL when configuring the integration in [Bots](<https://gptunnel.ru/bots/?view=integration>).

 <a id="main-developments"></a>
 
<a id="main-events"></a>
## Main events

<a id="tab-panel-0-0"></a>
**CLIENT\_MESSAGE**

```json
{
  "event": "CLIENT_MESSAGE",
  "id": "9f7b373e-fce1-11ee-9da4-a912f79ee445",
  "chat_id": "4976",
  "client_id": "30005",
  "message": {
    "type": "TEXT",
    "text": "Добрый день",
    "timestamp": 1713375646
  },
  "agents_online": false
}
```

<a id="tab-panel-0-1"></a>
**BOT\_MESSAGE**

```json
{
  "event": "BOT_MESSAGE",
  "id": "6620ce58bb349500015fa366",
  "chat_id": "4976",
  "client_id": "30005",
  "client_event_id": "9f7b373e-fce1-11ee-9da4-a912f79ee445",
  "message": {
    "type": "MARKDOWN",
    "content": "Здравствуйте! Чем могу помочь?",
    "text": "Здравствуйте! Чем могу помочь?",
    "timestamp": 1713375648
  }
}
```

<a id="tab-panel-0-2"></a>
**INVITE\_AGENT**

```json
{
  "event": "INVITE_AGENT",
  "id": "6620ce58bb349500015fa367",
  "chat_id": "4976",
  "client_id": "30005",
  "client_event_id": "9f7b373e-fce1-11ee-9da4-a912f79ee445"
}
```

> **Tip: Send images**
>
> If the bot model supports images, pass their URLs in the `images` array of the `message` object.

 <a id="additional-developments"></a>
 
<a id="additional-events"></a>
## Additional events

<a id="tab-panel-1-0"></a>
**AGENT\_JOINED**

A human agent has taken over the conversation.

```json
{
  "event": "AGENT_JOINED",
  "id": "9f7b373e-fce1-11ee-9da4-a912f79ee445",
  "chat_id": "4976",
  "client_id": "30005"
}
```

<a id="tab-panel-1-1"></a>
**AGENT\_UNAVAILABLE**

No human agent is available after `INVITE_AGENT`.

```json
{
  "event": "AGENT_UNAVAILABLE",
  "id": "9f7b373e-fce1-11ee-9da4-a912f79ee445",
  "chat_id": "4976",
  "client_id": "30005"
}
```

<a id="tab-panel-1-2"></a>
**CHAT\_CLOSED**

The client has closed the conversation.

```json
{
  "event": "CHAT_CLOSED",
  "id": "9f7b373e-fce1-11ee-9da4-a912f79ee445",
  "chat_id": "4976",
  "client_id": "30005"
}
```

<a id="get-balance"></a>
## Get Balance

`POST https://gptunnel.ru/api/bot/balance`

```bash
curl -X POST https://gptunnel.ru/api/bot/balance \
  -H "Authorization: ${GPTUNNEL_BOT_TOKEN:?Set GPTUNNEL_BOT_TOKEN}" \
  -H 'Content-Type: application/json'
```

```json
{
  "ok": true,
  "message": "OK",
  "balance": "354.91"
}
```

For an organization bot, the response contains the linked organization’s balance and includes `balanceHidden` and `canSpend`.

```json
{
  "ok": true,
  "message": "OK",
  "balance": "0",
  "balanceHidden": true,
  "canSpend": true
}
```

If the bot owner lacks permission to view the balance, `balanceHidden` is `true` and `balance` is the string `"0"`. `canSpend` indicates whether the balance plus the credit limit is positive, even if the balance is hidden. Other restrictions still apply when processing a message. If the bot owner lacks access to the linked organization, the API returns **403** and does not fall back to the personal balance.

 <a id="настройка-webhook-через-api"></a>
 <a id="api-web-chook-settings"></a>
 
<a id="configure-a-webhook-through-the-api"></a>
## Configure a webhook through the API

`POST https://gptunnel.ru/api/bot/config`

```bash
curl -X POST https://gptunnel.ru/api/bot/config \
  -H "Authorization: ${GPTUNNEL_BOT_TOKEN:?Set GPTUNNEL_BOT_TOKEN}" \
  -H 'Content-Type: application/json' \
  -d '{"webHookUrl": "https://bot.example.com/webhook"}'
```

```json
{
  "ok": true,
  "message": "OK"
}
```

---

For integration questions, contact [t.me/gptunnel\_ru](<https://t.me/gptunnel_ru>) or **@ScriptHeads** on Telegram.
