This guide shows how to build a simple Telegram bot using Laravel 12 with the defstudio/telegraph package.
Bot behavior
- Receives webhook updates from Telegram
- Replies
hellowhen a user sendshi
Local development
- Uses ngrok to expose your local server over HTTPS
- PHP 8.2+
- Laravel 12
- Composer
- Telegram account
- ngrok
- Open Telegram.
- Search for @BotFather.
- Send:
/newbot
- Complete the setup.
- Save the bot token provided by BotFather.
Install the package:
composer require defstudio/telegraphPublish migrations and migrate:
php artisan vendor:publish --tag="telegraph-migrations"
php artisan migratePublish config:
php artisan vendor:publish --tag="telegraph-config"Run:
php artisan telegraph:new-bot- Enter the bot token.
- Optionally give the bot a name.
- Skip chat and webhook setup for now.
Note the bot ID shown at the end.
Start Laravel:
php artisan serve --port=8000Start ngrok in another terminal:
ngrok http 8000Copy the HTTPS forwarding URL.
Update .env:
APP_URL=https://your-ngrok-url.ngrok.io
TELEGRAM_BOT_TOKEN=your_bot_token
Clear caches:
php artisan config:clear
php artisan route:clearTelegram webhook requests do not include CSRF tokens.
Update config/app.php to exclude the Telegraph webhook route from CSRF validation.
This prevents 419 errors when Telegram sends updates.
Register the webhook for your bot:
php artisan telegraph:set-webhook {bot_id}Replace {bot_id} with your actual bot ID.
- Telegram sends messages to the webhook URL.
- Laravel receives the payload.
- If the message text is
hi, the bot replies withhello. - All requests are logged and acknowledged.
- Open your bot in Telegram.
- Send:
hi
- The bot replies:
hello
https://api.telegram.org/bot<YOUR_TOKEN>/getWebhookInfo
Confirm the webhook URL is set and no errors are shown.
- Restarting ngrok changes the HTTPS URL.
- After restarting ngrok, re-run:
php artisan telegraph:set-webhook {bot_id}-
You can inspect incoming webhook requests using ngrok inspect:
- Open
http://127.0.0.1:4040in your browser. - View request payloads, headers, and response status.
- Useful for debugging webhook issues and verifying Telegram calls.
- Open
-
The default Telegraph webhook route is:
POST /telegraph/{token}/webhook
- Use Telegraph command handlers
- Add multiple keyword responses