# Darex AI bot

Минимальное PHP-приложение для Bitrix24 Cloud.

На первом этапе используется только обычный чат-бот Bitrix24 для личного чата:

```text
сообщение пользователю боту -> public/bot_handler.php -> OpenAI -> ответ в чат
```

Списки промтов уже существуют и этим проектом не создаются.

## Bitrix24 local app

В настройках локального приложения:

```text
Путь обработчика: https://api.darex-moto.ru/darex_connector_app/public/handler.php
Путь установки:   https://api.darex-moto.ru/darex_connector_app/public/install.php
```

Права:

```text
im
imbot
lists
user
crm
```

После установки основной экран приложения открывает интерфейс настроек. Значения
сохраняются в `storage/app_settings.php` и имеют приоритет над `.env.php`.
Первичное заполнение текущими значениями выполняется командой:

```bash
php tools/migrate_settings.php
```

OpenAI API key и client secret в интерфейсе не раскрываются. Пустое поле секрета
при сохранении оставляет текущее значение без изменений.

## CLI

```bash
php tools/register_bot.php
php tools/bot_diagnostics.php
php tools/send_bot_message.php --dialog-id=162100 --message="Test"
php tools/check_prompts.php
```

## CRM qualification

For an open-line deal in the default retail category (`CATEGORY_ID = 0`), the bot
moves the deal to the `Qualified` stage (`STAGE_ID = 37`) when the accumulated
client data contains a phone, a matched city and the interest group is either
`техника` or empty.
The category and stage IDs can be changed through `RETAIL_DEAL_CATEGORY_ID` and
`TECHNIQUE_QUALIFIED_STAGE_ID` in `.env.php`.

When the interest is `запчасти`, a deal outside that category is moved to
`CATEGORY_ID = 26`, `STAGE_ID = C26:NEW`. For `сервис`, it is moved to
`CATEGORY_ID = 30`, `STAGE_ID = C30:NEW`. Deals already in the target category
keep their current stage.

For `другое`, the deal is moved to the default retail category
(`CATEGORY_ID = 0`) and the non-target stage (`STAGE_ID = 26`).
After a successful move, the bot posts a verification notice with links to the
deal and client dialog in `NON_TARGET_NOTIFICATION_DIALOG_ID`.

At the end of the Moscow business day, `tools/move_phone_only_deals.php` moves
retail deals where the client supplied a phone but no city to `STAGE_ID = 44`.
The daily cron runs at 20:55 UTC (23:55 Moscow time).
After a successful move, the bot posts a verification notice with links to the
deal and client dialog in `PHONE_ONLY_NOTIFICATION_DIALOG_ID`.

Inactivity followups are sent only while the deal remains in the initial retail
stage (`CATEGORY_ID = 0`, `STAGE_ID = NEW`). If an employee or automation moves
the deal to another stage or funnel, followups and the no-response marker stop.
After all followups and the three-day waiting period, a deal without a phone is
marked as not responding and moved to retail stage `28`.

Followup delays and texts are loaded from the Bitrix list configured by
`FOLLOWUP_SETTINGS_LIST_CODE`. Run `php tools/setup_followup_list.php` once to
create the list, its fields and the four default stages. Environment values are
used as a fallback when the list is unavailable.
Use `php tools/check_followup_settings.php` to inspect effective values and
`php tools/clear_followup_cache.php` to apply list edits immediately.
