Skip to content

Zero-Config App & Web Dashboard ​

Tueen\Telegram\App is the high-level application bootstrapper and orchestrator for tueen/telegram. It eliminates boilerplate configuration, automatically sets up file-based conversation flow storage, auto-discovers attribute controllers, and provides a built-in Web Setup Wizard and CLI tooling.


🚀 1. The Single-File Entry Point ​

With App, you can launch a complete, production-ready Telegram bot from a single file:

php
<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Tueen\Telegram\App;

$app = App::create(__DIR__);
$app->run();

Flexible Entry Point Naming

While index.php is standard, you can freely name your entry point file whatever you want (such as bot.php, update.php, or webhook.php). App::create(__DIR__) dynamically inspects the executing script and automatically resolves the correct public webhook URL.


⚡ 2. Adaptive Execution Environments ​

When $app->run() is invoked, it intelligently detects the execution context:

CLI / Terminal
CLI Mode
Triggered by 'php index.php'. Runs interactive Long-Polling or CLI helper commands (webhook:set, doctor, etc.).
HTTP POST (Webhook)
Webhook Mode
Triggered by incoming Telegram webhook requests. Validates secret_token, dispatches update handlers and flows, and returns 200 OK.
HTTP GET (Browser)
Web Dashboard
Triggered when index.php is opened in a web browser. Launches the interactive Web Setup Wizard or live Management Console.

🌐 3. Web Setup Wizard & Management Console ​

Opening index.php in a web browser activates the Royal Web Dashboard:

1. First-Time Setup Wizard (When Token is Empty) ​

If no bot token is configured in config.php:

  • Displays a setup form to enter your Bot Token (from @BotFather).
  • Automatically detects your public webhook URL (https://yourdomain.com/index.php).
  • Includes a button to generate a secure 32-character random secret_token.
  • Tests connection with getMe() and automatically sets the webhook via setWebhook().
  • Automatically writes credentials into config.php.

2. Live Management Console (When Configured) ​

Once configured, visiting the URL in a browser displays a real-time status console:

  • Bot Profile: Name, @username, Bot ID, group permissions, and Bot API ping latency.
  • Webhook Telemetry: Active webhook URL, pending update queue count, SSL certificate type, max connections, and any recent delivery errors from Telegram servers.
  • One-Click Actions:
    • 🎯 Set / Update Webhook: Sets the webhook to the current URL with optional drop_pending_updates.
    • 🗑️ Delete Webhook: Removes the webhook from Telegram servers.
    • 🔄 Refresh: Reloads live status from Telegram Bot API.
  • Storage & Flow Metrics: Displays active flow session files and storage path.

Securing the Dashboard ​

To protect the web dashboard with a password, specify setup.password in your config.php, or set SETUP_PASSWORD in your .env file:

php
return [
    'token' => '...',
    'setup' => [
        'enabled' => true,
        'password' => 'my-secure-dashboard-password',
    ],
];

Or in .env:

env
SETUP_PASSWORD=my-secure-dashboard-password

Once logged in, an active session is securely maintained. You can click 🔒 Logout in the header at any time to lock the dashboard again.


💾 4. Automatic Flow State Storage (storage/flow) ​

App automatically configures persistent conversation flow storage:

  • By default, it creates $basePath . '/storage/flow' and attaches FileStateStore.
  • Protects the directory against direct web browser access with an Apache .htaccess (Deny from all) and an index.php fallback (403 Forbidden).
  • Multi-step conversational flows ($bot->startFlow(...)) automatically persist state without any external database or Redis setup.

To customize the flow storage path, configure flow_storage in config.php:

php
return [
    'token' => '...',
    'flow_storage' => '/var/data/telegram_flows',
];

📁 5. Automatic Controller Discovery ​

If your bot project has a controllers/ directory in its project root, App automatically scans all PHP files inside it. Any class decorated with routing attributes (#[OnCommand], #[OnCallbackQuery], #[OnMessage], #[OnInlineQuery], #[OnUpdate]) is registered automatically with zero boilerplate!

php
// controllers/StartController.php
namespace App\Controllers;

use Tueen\Telegram\Routing\Attributes\OnCommand;
use Tueen\Telegram\Types\Update;
use Tueen\Telegram\Telegram;

class StartController
{
    #[OnCommand('start')]
    public function start(Update $update, Telegram $bot): void
    {
        $bot->sendMessage(text: "Hello from auto-discovered controller!");
    }
}

🩺 6. System Diagnostics (doctor) ​

To troubleshoot webhook issues, SSL certificate mismatches, or file permission errors on shared hosting:

  • Run php index.php doctor in your terminal to see a colored diagnostic status table.
  • Or open your bot in a browser to inspect the System Diagnostics (Doctor) card in the Web Dashboard.

It verifies:

  • PHP version compatibility (PHP 8.4+).
  • Required extensions (curl, json, mbstring, openssl).
  • Flow storage permissions (storage/flow writable).
  • Telegram Bot API server reachability and latency.
  • Webhook URL HTTPS compliance.

🧭 7. App & CLI API Reference ​

Below is the complete reference of methods and properties available on Tueen\Telegram\App, along with terminal CLI commands.

🏛️ App Class (Tueen\Telegram\App) ​

App::create(string $basePath, array $config = []): static
Factory
Initializes and boots the App instance for the given project base root path.
App::boot(string $basePath, array $config = []): static
Alias
👉DX Shorthand: Alias for App::create()
Expressive shorthand alias for App::create().
private(set) Telegram $bot
→TelegramAsymmetric Visibility
Underlying Telegram client facade instance.
public Router $router
→RouterProperty Hook
Active update Router instance proxied directly from $bot->router.
public FlowManager $flowManager
→FlowManagerProperty Hook
Active FlowManager state orchestrator proxied directly from $bot->flowManager.
private(set) string $basePath
→stringAsymmetric Visibility
Resolved absolute root directory path of the bot project.
private(set) string $storagePath
→stringAsymmetric Visibility
Directory path for application storage ($basePath/storage).
private(set) string $flowStoragePath
→stringAsymmetric Visibility
Directory path for persistent conversational flow state files ($basePath/storage/flow).
run(mixed ...$handlers): mixed
Runner
Executes the bot using adaptive environment detection (CLI polling, HTTP webhook, or Web dashboard).
reply(string|Text $text, mixed ...$args): mixed
Helper
Quick reply helper to send a text message to the active chat in context.
catch(string|callable $exceptionOrHandler, ?callable $handler = null): static
Error Handling
Registers a global exception handler for incoming update processing errors.
reloadConfig(array $overrides = []): static
Lifecycle
Reloads configuration from config.php and rebuilds client and flow storage instances.

💻 CLI Administrative Commands ​

php index.php
CLI Command
Starts interactive Long-Polling mode with real-time log output in terminal.
php index.php webhook:set <url> [--drop]
CLI Command
Sets Telegram webhook URL with optional --drop flag to discard pending updates.
php index.php webhook:delete [--drop]
CLI Command
Deletes active webhook from Telegram servers.
php index.php webhook:info
CLI Command
Inspects current webhook status, URL, pending update count, and error telemetry.
php index.php bot:info
CLI Command
Displays bot profile, username, Bot API ping latency, and group permissions.
php index.php doctor
CLI Command
Runs complete system self-diagnostics (PHP version, extensions, writable storage, HTTPS).

Released under the MIT License.