How to create a Telegram bot?
In this article, I will tell you how to easily create a Telegram bot using Symfony and EvMVTelegramBundle and implement IoC.
To begin, you need to install the package with the command:
composer require evmv/telegram-bundle
Next, register the bundle in config/bundles.php:
return [
Evmv\TelegramBot\TelegramBundle::class => ['all' => true]
];
Add bot key to .env:
BOT_KEY=your_telegram_bot_key
And the last thing that is needed on our side to finish configuring the package is to register an endpoint in config/routes.yaml that will receive data via a web hook from Telegram:
telegram:
path: /handle
controller: Evmv\TelegramBot\Controller\Handle::__invoke
Telegram sends data only via an https connection, so for local development you need to install ngrok and expose our endpoint to the world:
ngrok http 8000
And register via telegram api, using this endpoint:
https://api.telegram.org/botTELEGRAM_BOT_KEY_HERE/setWebhook?url=ENDPOINT_THAT_RETURN_NGROK_FOR_HTTPS
That’s it, now we’re ready to accept and process commands!
Let’s process the “start” command, for this we need to create a command class, implement CommandInterface and add the #[Command] annotation:
<?php
declare(strict_types=1);
namespace App\Controller;
use Evmv\TelegramBot\Handle\Command\Command;
use Evmv\TelegramBot\Handle\Command\CommandInterface;
use Evmv\TelegramBot\Helper\Process\TelegramProcessHelper;
use TelegramBot\Api\BotApi;
use TelegramBot\Api\Types\Update;
#[Command(command: '/start')]
class StartCommand implements CommandInterface
{
public function __construct(
private readonly BotApi $client,
private readonly TelegramProcessHelper $processHelper,
) {
}
public function __invoke(Update $update): void
{
$chatId = $this->processHelper->extractChatId($update);
$this->client->sendMessage($chatId, 'Hello. How can I help you?');
}
}
Also we can generate keyboard, using InlineKeyboardGenerator:
<?php
declare(strict_types=1);
namespace App\Controller;
use Evmv\TelegramBot\Handle\Command\Command;
use Evmv\TelegramBot\Handle\Command\CommandInterface;
use Evmv\TelegramBot\Service\Keyboard\Inline\InlineKeyboardGenerator;
use TelegramBot\Api\BotApi;
use TelegramBot\Api\Types\Update;
#[Command(command: '/start')]
class StartCommand implements CommandInterface
{
public function __construct(
private readonly InlineKeyboardGenerator $generator,
private readonly BotApi $client
) {
}
public function __invoke(Update $update): void
{
$chat = $update->getMessage()->getChat();
$keyboard = $this->generator
->button('First row first button', ButtonClick::class, ['item' => 1])
->button('Second row second button', ButtonClick::class, ['item' => 2])
->br()
->button('Second row first button', ButtonClick::class, ['item' => 3])
->generateKeyboard();
$this->client->sendMessage($chat->getId(), 'Hello. Click to button', replyMarkup: $keyboard);
}
}
To handle the button click, create a class that implements the interface ActionInterface:
<?php
declare(strict_types=1);
namespace App\Controller;
use Evmv\TelegramBot\Handle\Action\Action;
use Evmv\TelegramBot\Handle\Action\ActionInterface;
use Evmv\TelegramBot\Helper\Process\TelegramProcessHelper;
use TelegramBot\Api\Types\Update;
#[Action(action: 'customHandleClick')]
class ButtonClick implements ActionInterface
{
public function __construct(private readonly TelegramProcessHelper $processHelper)
{
}
public function __invoke(Update $update): void
{
$data = $this->processHelper->callbackQueryPayload($update);
// Do something
}
}
Also you can create Text handler, Middleware, Reply keyboard and event handler. For more information read bundle documentation:
That’s it, enjoy =)