aymericcucherousset/telegram-bot-bundle
A Symfony bundle for integrating a framework-agnostic Telegram Bot library. Designed for Symfony 7.4 and 8.x, it enables robust, multi-bot support with flexible provider architecture.
CompositeBotProviderStaticBotProvider for configuration-based bot definitionInstall via Composer:
composer require aymericcucherousset/telegram-bot-bundle
Create config/packages/telegram_bot.yaml:
telegram_bot:
bots:
main:
token: '%env(TELEGRAM_BOT_TOKEN)%'
Create config/routes/telegram_bot_bundle.yaml:
telegram_bot_bundle:
resource: '@TelegramBotBundle/config/routes.php'
Static bots are defined directly in the configuration file:
# config/packages/telegram_bot.yaml
telegram_bot:
bots:
main:
token: '%env(TELEGRAM_BOT_TOKEN)%'
Implement the BotProviderInterface to fetch bot credentials from a database:
<?php
declare(strict_types=1);
namespace App\Telegram;
use App\Repository\TelegramBotRepository;
use Aymericcucherousset\TelegramBotBundle\Bot\BotProviderInterface;
final class DatabaseBotProvider implements BotProviderInterface
{
public function __construct(
private TelegramBotRepository $repository,
) {}
public function supports(string $name): bool
{
return null !== $this->repository->findOneBy([
'name' => $name,
]);
}
public function getToken(string $name): string
{
$bot = $this->repository->findOneBy([
'name' => $name,
]);
if (!$bot) {
throw new \RuntimeException(sprintf('Bot "%s" not found.', $name));
}
return $bot->getToken();
}
}
Handlers are auto-registered via service tags. Example handler:
<?php
declare(strict_types=1);
namespace App\Telegram\Command;
use Aymericcucherousset\TelegramBot\Attribute\AsTelegramCommand;
use Aymericcucherousset\TelegramBot\Handler\HandlerInterface;
use Aymericcucherousset\TelegramBot\Method\Message\TextMessage;
use Aymericcucherousset\TelegramBot\Update\Update;
#[AsTelegramCommand(name: 'ping', description: 'Replies with pong')]
final class PingCommand implements HandlerInterface
{
public function handle(Update $update): void
{
$message = $update->message;
if ($message === null) {
return;
}
$textMessage = new TextMessage(
chatId: $message->chatId,
text: 'pong 🏓',
);
$update->bot->getClient()->send($textMessage);
}
}
Define a route for the webhook controller:
Create config/routes/telegram_bot_bundle.yaml:
telegram_bot_bundle:
resource: '@TelegramBotBundle/config/routes.php'
public function __construct(
// Inject Bot Manager
private readonly BotManager $botManager,
) {
}
public function yourMethod(): void
{
// Create your Telegram Method
$method = new TextMessage(
text: 'Hello world!',
chatId: new ChatId(1111111111),
);
// Then send the Telegram Method from the bot
$this->botManager->getClient('main')->send($method);
}
BotProviderInterface. Use built-in StaticBotProvider or implement custom providers (e.g., database, API).The bundle supports multiple bot providers. Register several providers (static, database, API, etc.) as services tagged with telegram_bot.provider. The CompositeBotProvider aggregates all bots, enabling scalable, multi-bot architectures.
Contributions are welcome. Please submit pull requests or open issues for bug reports and feature requests. Follow Symfony and PSR coding standards.
This bundle is released under the MIT License. See the LICENSE file for details.
How can I help you explore Laravel packages today?