1. Go to this page and download the library: Download vet1kk/telegram-bot-core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vet1kk / telegram-bot-core example snippets
declare(strict_types=1);
use Bot\Bot;
use App\Command\StartCommand;
use App\Action\MenuAction;
use App\Listener\FallbackListener;
use App\Middleware\TimingMiddleware;
use App\Provider\AppServiceProvider;
s)
->withListener(FallbackListener::class)
->runFromWebhook();
declare(strict_types=1);
use Bot\Bot;
->registerWebhook('https://example.com/telegram/webhook.php');
declare(strict_types=1);
namespace App\Command;
use Bot\Attribute\Command;
use Bot\Command\CommandInterface;
use Bot\DTO\Update\MessageUpdateDTO;
#[Command(name: 'start', description: 'Send a welcome message')]
final class StartCommand implements CommandInterface
{
public function handle(MessageUpdateDTO $update): void
{
$update->reply('Hello! Your bot is running.');
}
}
declare(strict_types=1);
namespace App\Action;
use Bot\Action\ActionInterface;
use Bot\Attribute\Action;
use Bot\DTO\Update\CallbackQueryUpdateDTO;
use Bot\Keyboard\Buttons\InlineButton;
use Bot\Keyboard\InlineKeyboard;
#[Action(name: 'menu', description: 'Open the main menu')]
final class MenuAction implements ActionInterface
{
public function handle(CallbackQueryUpdateDTO $update, array $params = []): void
{
$keyboard = InlineKeyboard::create()
->addButton(
InlineButton::create()
->setText('Help')
->setCallbackData('help'),
1
)
->addButton(
InlineButton::create()
->setText('Settings')
->setCallbackData('settings'),
1
);
$update->reply('Choose an option:', $keyboard);
}
}
declare(strict_types=1);
namespace App\Middleware;
use Bot\DTO\Update\UpdateDTO;
use Bot\Middleware\MiddlewareInterface;
use Psr\Log\LoggerInterface;
final class TimingMiddleware implements MiddlewareInterface
{
public function __construct(private LoggerInterface $logger)
{
}
public function process(UpdateDTO $update, callable $next): void
{
$startedAt = microtime(true);
$next($update);
$this->logger->info('Update handled', [
'duration_ms' => (int) ((microtime(true) - $startedAt) * 1000),
]);
}
}
declare(strict_types=1);
namespace App\Listener;
use Bot\Attribute\Listener;
use Bot\Event\Events\UnhandledEvent;
use Bot\Listener\ListenerInterface;
use Psr\Log\LoggerInterface;
final class FallbackListener implements ListenerInterface
{
public function __construct(private LoggerInterface $logger)
{
}
#[Listener(eventClass: UnhandledEvent::class)]
public function onUnhandled(UnhandledEvent $event): void
{
$this->logger->info('Unhandled update received');
$update = $event->getUpdate();
if ($update->getChatId() !== null) {
$update->reply("Sorry, I didn't understand that message.");
}
}
}
declare(strict_types=1);
namespace App\Provider;
use Bot\Provider\ServiceProviderInterface;
use Bot\Webhook\WebhookHandlerInterface;
use DI\Container;
use App\Webhook\FrameworkWebhookHandler;
final class AppServiceProvider implements ServiceProviderInterface
{
public function register(Container $container): void
{
$container->set(
WebhookHandlerInterface::class,
\DI\autowire(FrameworkWebhookHandler::class)
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.