Solution:
For building complex Telegram bots in Laravel, the most mature and popular package is irazasyed/telegram-bot-sdk. It provides a rich API, easy webhook handling, support for inline keyboards, callback queries, and more.
Step 1: Install the package
composer require irazasyed/telegram-bot-sdk
Step 2: Publish Config and Set API Key
php artisan vendor:publish --provider="Telegram\Bot\Laravel\TelegramServiceProvider"
Set your API key in .env:
TELEGRAM_BOT_TOKEN=your-telegram-bot-token
Step 3: Setup Webhook Routing
In routes/web.php or preferably in a dedicated controller:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BotController;
Route::post('/' . env('TELEGRAM_BOT_TOKEN') . '/webhook', [BotController::class, 'handle']);
Step 4: Controller Example
Create app/Http/Controllers/BotController.php:
namespace App\Http\Controllers;
use Telegram\Bot\Api;
use Illuminate\Http\Request;
class BotController extends Controller
{
public function handle(Request $request)
{
$telegram = new Api(env('TELEGRAM_BOT_TOKEN'));
$update = $telegram->getWebhookUpdate();
if ($update->isType('callback_query')) {
$data = $update->getCallbackQuery()->getData();
// Process callback data (e.g., handle order, product, etc.)
} elseif ($update->isType('message')) {
$message = $update->getMessage();
switch ($message->getText()) {
case '/start':
// Reply with main command buttons
$keyboard = [
['Sell Products', 'Check Orders'],
['Payments', 'Profile Data'],
// ...
];
$replyMarkup = $telegram->replyKeyboardMarkup([
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => false
]);
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' => 'Welcome! Choose a command:',
'reply_markup' => $replyMarkup
]);
break;
// Handle other commands...
}
}
return 'ok';
}
}
Step 5: Managing Callback Data and Multiple Actions
- Handle callback queries inside
handle()by switching on$data. - For complex bots, consider organizing your actions using Laravel's Command Bus or custom service classes.
- You can use inline keyboards with carefully crafted
callback_dataper button.
Alternative Packages:
- Telegram Bot (php-telegram-bot/core): Not Laravel-specific but very advanced.
- nutgram/laravel: Modern, composable, works with async/middleware/pipeline style.
- botman/botman: Multi-platform, but for very complex, Telegram-only bots, the irazasyed package is more direct.
Summary Table:
| Package | Laravel Support | Inline Keyboard | Callback Query | Docs/Community |
|---|---|---|---|---|
| irazasyed/telegram-bot-sdk | Yes | Yes | Yes | Excellent |
| nutgram/laravel | Yes | Yes | Yes | Good, Modern |
| botman/botman | Yes | Yes | Yes | Large, Multi-bot |
Recommendation:
Start with irazasyed/telegram-bot-sdk for Laravel Telegram bots, unless you want a fresh approach--then evaluate Nutgram.
Telegram Bot SDK Docs:
https://github.com/irazasyed/telegram-bot-sdk
Let me know if you need an example for a specific command or callback handling!