andrew-gos/telegram-bot-bundle
Install the Bundle
composer require andrew-gos/telegram-bot-bundle
The bundle auto-enables via Symfony Flex.
Configure a Bot
Add a minimal config to config/packages/andrew_gos_telegram_bot.yaml:
andrew_gos_telegram_bot:
bots:
my_bot:
factory:
method: 'getUpdates' # or 'default' for webhooks
arguments:
$token: '%env(TELEGRAM_BOT_TOKEN)%'
handlers:
- handler: App\Telegram\Handler\DefaultMessageHandler
Define a Handler
Create a basic handler (e.g., app/Telegram/Handler/DefaultMessageHandler.php):
use AndrewGos\TelegramBot\Kernel\Handler\HandlerInterface;
use AndrewGos\TelegramBot\Entity\Update;
use AndrewGos\TelegramBot\Telegram;
class DefaultMessageHandler implements HandlerInterface {
public function handle(Update $update, Telegram $telegram): void {
$telegram->getApi()->sendMessage(
$update->getMessage()->getChat()->getId(),
'Hello!'
);
}
}
Run the Bot Start long-polling:
php bin/console andrew-gos:telegram-bot:listen my_bot
Update Processing Pipeline
CheckerInterface to filter updates (e.g., /start command, media types).MiddlewareInterface for cross-cutting logic (e.g., auth, logging).Bot Lifecycle
getUpdates (long-polling) for testing.default factory) for scalability.factory:
method: 'default' # Webhook
arguments:
$token: '%env(TOKEN)%'
$webhookUrl: '%env(WEBHOOK_URL)%'
Service Organization
CommandChecker, MediaTypeChecker).HandlerInterface for bot responses.RequestLoggerPlugin).UserAuthMiddleware).Configuration-Driven Routing
Leverage priority to control handler execution order:
handlers:
- checker: App\Checker\AdminCommandChecker
handler: App\Handler\AdminHandler
priority: 100 # Highest priority
- handler: App\Handler\FallbackHandler
priority: -100 # Lowest priority
$this->eventDispatcher->dispatch(new TelegramUpdateEvent($update));
UserRepository) into handlers.Telegram and Update objects for unit tests:
$update = new Update([...]);
$handler = new MyHandler();
$handler->handle($update, $this->createMock(Telegram::class));
Webhook HTTPS Requirement
ngrok during development.php bin/console andrew-gos:telegram-bot:set-webhook my_bot https://your-domain.com/webhook
Long-Polling Timeouts
getUpdates) may fail if the script runs too long. Use pcntl for background processes or deploy as a systemd service.Checker Logic Errors
CheckerInterface::check() returns bool. Non-boolean values may cause silent failures.null updates).Priority Collisions
priority execute in arbitrary order. Use distinct values (e.g., 100, 50, -50).Token Validation
php bin/console andrew-gos:telegram-bot:bot-info my_bot
RequestLoggerPlugin to inspect raw Telegram updates:
plugins:
- class: App\Telegram\Plugin\RequestLoggerPlugin
arguments:
$logger: '@monolog.logger.telegram'
php bin/console andrew-gos:telegram-bot:webhook-info my_bot
Custom Factories
Extend the TelegramFactoryInterface for non-standard bot setups (e.g., multi-account bots).
Dynamic Handlers
Register handlers dynamically via Symfony’s container events:
$container->register('dynamic.handler', DynamicHandler::class);
Plugin Middleware Combine plugins and middlewares for layered processing:
plugins:
- App\Plugin\AuthPlugin
handlers:
- middlewares:
- App\Middleware\RateLimitMiddleware
handler: App\Handler\CoreHandler
Event Subscribers
Listen to TelegramUpdateEvent for cross-bot logic:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TelegramEventSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [TelegramUpdateEvent::class => 'onUpdate'];
}
public function onUpdate(TelegramUpdateEvent $event) { ... }
}
%env() for tokens to avoid hardcoding:
arguments:
$token: '%env(TELEGRAM_BOT_TOKEN)%'
andrew_gos_telegram_bot:
plugins:
&logger_plugin
class: App\Plugin\RequestLoggerPlugin
arguments:
$logger: '@monolog.logger.telegram'
bots:
bot1:
plugins: [*logger_plugin]
bot2:
plugins: [*logger_plugin]
How can I help you explore Laravel packages today?