# LMS -> Telegram bot API (api.php)

Kurator LMS admin paneldan o'quvchiga javob yozishi uchun. LMS bizga chat_id va
matn yuboradi — bot xabarni o'quvchiga kurator nomidan (Telegram Business orqali)
yetkazadi, javob suhbat tarixiga va bot_messages jurnaliga ham tushadi.

## Endpoint

POST https://<bot-server>/api.php
(aniq URL joylashtirishga qarab beriladi; test uchun: `php -S 0.0.0.0:8088` proyekt papkasida)

## Avtorizatsiya

Header: `Authorization: Bearer <LMS_API_TOKEN>`
Agar web-server Authorization headerni kesib tashlasa, zaxira varianti: `token` field.

## Parametrlar (form-data yoki JSON body)

| Field   | Tur    | Majburiy | Izoh                                   |
|---------|--------|----------|----------------------------------------|
| chat_id | int    | ha       | O'quvchining Telegram chat ID'si       |
| text    | string | ha       | Xabar matni, maksimum 4096 belgi       |

## Javoblar

| HTTP | Body                                                        | Sabab |
|------|-------------------------------------------------------------|-------|
| 200  | `{"success":true}`                                          | Yuborildi |
| 400  | `{"success":false,"error":"chat_id majburiy (int)"}`        | Parametr xato |
| 401  | `{"success":false,"error":"token noto'g'ri..."}`            | Token yo'q/xato |
| 404  | `{"success":false,"error":"chat_id topilmadi: ..."}`        | Bu o'quvchi kuratorga hali yozmagan — bot unga yoza olmaydi (Telegram Business cheklovi) |
| 405  | `{"success":false,"error":"faqat POST"}`                    | GET va boshqalar |
| 502  | `{"success":false,"error":"telegram: ..."}`                 | Telegram xatosi (masalan, o'quvchi botni block qilgan) |
| 503  | `{"success":false,"error":"LMS_API_TOKEN .env da sozlanmagan"}` | Serverda token yo'q |

## Testlar (curl)

1. Muvaffaqiyatli yuborish (chat_id o'rniga real o'quvchi ID qo'ying):

    curl -s -X POST 'https://<bot-server>/api.php' \
      -H 'Authorization: Bearer <LMS_API_TOKEN>' \
      -d 'chat_id=6430811721' \
      -d 'text=Assalomu alaykum! Bu LMS orqali yuborilgan test xabar.'

    Kutilgan: `{"success":true}` va xabar o'quvchining Telegramiga tushadi.

2. JSON body bilan:

    curl -s -X POST 'https://<bot-server>/api.php' \
      -H 'Authorization: Bearer <LMS_API_TOKEN>' \
      -H 'Content-Type: application/json' \
      -d '{"chat_id":6430811721,"text":"JSON orqali test"}'

3. Token xato -> 401:

    curl -s -X POST 'https://<bot-server>/api.php' -H 'Authorization: Bearer noto_gri' -d 'chat_id=1&text=x'

4. Noma'lum chat -> 404:

    curl -s -X POST 'https://<bot-server>/api.php' -H 'Authorization: Bearer <LMS_API_TOKEN>' -d 'chat_id=1&text=salom'

5. Bo'sh text -> 400:

    curl -s -X POST 'https://<bot-server>/api.php' -H 'Authorization: Bearer <LMS_API_TOKEN>' -d 'chat_id=6430811721&text='

## Muhim eslatmalar

- Faqat kuratorga YOZGAN o'quvchilarga yuborish mumkin (bot Telegram Business
  ulanishi orqali yozadi, ulanish o'quvchi birinchi yozganda eslab qolinadi).
- Kurator javobi bot suhbat tarixiga ham qo'shiladi — AI keyingi javoblarida
  buni hisobga oladi (takror javob bermaydi).
- Xabar bot_messages jurnaliga direction=out, role=assistant bo'lib yoziladi.
- LMS_API_TOKEN .env faylda saqlanadi; uni platforma tokenidan alohida qiling.
