# Telegram Media Bot

PHP 8.1+, OOP, no framework. `longman/telegram-bot` for the Telegram API,
PDO for MySQL, `vlucas/phpdotenv` for config.

## Features implemented

- User system (auto-registers on `/start`, block/unblock)
- Mandatory channel join gate (checked via `getChatMember`, re-checked on every message until satisfied)
- Category tree browsing (unlimited depth) + search + "latest" / "most downloaded" lists
- Subscription plans + manual payment flow: user picks a plan → sends a receipt photo → admin approves/rejects from an inline keyboard → subscription auto-activates
- Free-tier daily download limit (configurable in `settings` table), premium files gated behind an active subscription
- Admin panel (reply-keyboard driven): stats, mandatory-channel management, plan management, pending-payment review, broadcast, and one-track-at-a-time audio upload
- Playlists (schema + read paths ready; admin CRUD not wired into the panel yet — extend `AdminHandler` the same way plans/channels are done)

## Setup

```bash
composer install
cp .env.example .env        # then fill in BOT_TOKEN, BOT_USERNAME, DB_*, ADMIN_IDS, WEBHOOK_URL
mysql -u root -p telegram_media_bot < database/schema.sql
php public/set_webhook.php  # registers the webhook with Telegram (needs HTTPS)
```

Point your webserver's document root at `public/`. Schedule the cron job to expire
outdated subscriptions:

```
*/10 * * * * php /path/to/project/cron/expire_subscriptions.php >> /path/to/project/storage/logs/cron.log 2>&1
```

## Structure

```
config/config.php        -> loads .env, returns the config array
src/Core/                -> Database (PDO singleton), Model (base CRUD), Config, Logger, AdminGuard, ModelFactory
src/Models/               -> one class per table (already provided/extended)
src/Services/            -> business rules: MandatoryChannelService, SubscriptionService, PaymentService
src/Bot/Keyboards.php    -> all reply-keyboard layouts, as constants
src/Bot/States.php       -> conversational FSM state names (stored in bot_states)
src/Bot/Handlers/        -> UserHandler (menus/browsing/payment), AdminHandler (panel)
src/Bot/TelegramBot.php  -> update dispatcher: join-gate, /start, text routing, callbacks, photo/audio uploads
public/webhook.php       -> Telegram POSTs updates here
public/set_webhook.php   -> CLI: registers the webhook URL
cron/expire_subscriptions.php
database/schema.sql
```

## Notes on the token you shared in chat

You pasted a real bot token earlier in this conversation. Please revoke/regenerate
it via @BotFather immediately — it should be treated as leaked. Put the new token
only in your local `.env` file, which is git-ignored.

## Extending

- To wire the future music website/app: `media_files.file_id` is Telegram-specific,
  but `source_url` is already reserved on that table for your own CDN link once you
  build the site — the bot and the site can then share the same `media_files` table.
- To add playlist management in the admin panel, follow the same pattern as
  `AdminHandler::listPlans()` / `addPlan()`.
