al3x5 / xbot-laravel
Laravel integration for xBot library
v0.1.1
2025-12-11 06:14 UTC
Requires
- php: ^8.2
- al3x5/laravel-psr16-cache: ^1.0
- al3x5/xbot: ^2.3
- laravel/framework: ^12.40
README
Seamless integration of xBot the powerful PHP library for creating Telegram bots with the Laravel framework.
🚀 Features
- Artisan Commands: Use xBot through familiar Laravel commands
- Laravel Cache Integration: Automatic PSR-16 adapter
- Laravel Configuration: Native Laravel configuration system
- Dependency Injection: Type-hint Bot in controllers for automatic injection
- Service Container: Bot registered as singleton in Laravel container
- Webhook Management: Easy webhook configuration for Telegram bots
📦 Installation
composer require al3x5/xbot-laravel
⚡ Quick Start
1. Install and Configure
php artisan xbot
This command will:
- Configure the Laravel API (Sanctum)
- Publish the xBot configuration
- Guide you through setting up your bot
- Adapt the configuration for your environment Laravel
2. Configure Webhook
php artisan xbot:hook:set https://yourdomain.com/xbot/webhook
3. Create Your First Command
php artisan xbot telegram:command HelloWorld
4. Register all commands and callbacks
php artisan xbot register
🛠 Available Commands
| Command | Description |
|---|---|
php artisan xbot |
Main command (install or proxy to xBot CLI) |
php artisan xbot:hook:set <url> |
Set webhook URL |
php artisan xbot:hook:delete |
Delete webhook |
php artisan xbot:hook:info |
Get webhook info |
php artisan xbot:hook:about |
Get bot info |
php artisan xbot register |
Register all commands and callbacks |
php artisan xbot telegram:command <name> |
Create new command |
php artisan xbot telegram:callback <name> |
Create new callback handler |
php artisan xbot telegram:conversation <name> |
Create new conversation |
php artisan xbot telegram:handler <name> |
Create new handler |
php artisan xbot telegram:middleware <name> |
Create new middleware |
⚙️ Settings
After installation, add to your .env:
BOT_TOKEN=1234567890:ABCDEFGHIJKLMNOQRSTZ BOT_SECRET=your_secret_key (optional) ADMIN_TELEGRAM_ID=123456789,985632147 BOT_WEBHOOK_URL=https://yourdomain.com/bot (optional)
🌐 Webhook Route
Add to your routes/api.php:
use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; Route::post('/bot', function (Request $request) { $bot = app(\Al3x5\xBot\Bot::class); $bot->run(); });
🔧 Dependency Injection
Type-hinting in Controllers
The Bot is registered as a singleton in Laravel's service container. You can inject it directly:
<?php namespace App\Http\Controllers; use Al3x5\xBot\Bot; class TelegramController extends Controller { public function sendMessage(Bot $bot) { $bot->sendMessage([ 'chat_id' => '123456789', 'text' => 'Hello from Laravel!' ]); return response()->json(['status' => 'sent']); } }
Using the Service Container
use Al3x5\xBot\Bot; // Method 1: via type-hinting (recommended) public function myMethod(Bot $bot) { ... } // Method 2: via app() helper $bot = app(Bot::class); // Method 3: via alias 'xbot' $bot = app('xbot');
Using Facade (if needed)
Create a Facade in your Laravel app app/Facades/Bot.php:
<?php namespace App\Facades; use Illuminate\Support\Facades\Facade; class Bot extends Facade { protected static function getFacadeAccessor() { return \Al3x5\xBot\Bot::class; } }
Then use: Bot::sendMessage([...])
🎯 Usage Examples
Send a Message
public function example(Bot $bot) { $bot->sendMessage([ 'chat_id' => '123456789', 'text' => 'Hello World!', 'parse_mode' => 'Markdown' ]); }
Send with Keyboard
public function withKeyboard(Bot $bot) { $bot->sendMessage([ 'chat_id' => '123456789', 'text' => 'Choose an option:', 'reply_markup' => [ 'inline_keyboard' => [ [['text' => 'Option 1', 'callback_data' => 'opt1']], [['text' => 'Option 2', 'callback_data' => 'opt2']] ] ] ]); }
Answer Callback Query
public function handleCallback(Bot $bot) { $bot->answerCallbackQuery([ 'callback_query_id' => $callback_query_id, 'text' => 'Action completed!' ]); }
📚 Requirements
- PHP 8.2+
- Laravel 12.x
- xBot Library v4 (automatically installed)
🆘 Support
📄 License
MIT License - see the LICENSE file for details.
Need help? Open an issue on GitHub or consult the xBot documentation.