1. Go to this page and download the library: Download yabx/telegram 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/ */
yabx / telegram example snippets
use Yabx\Telegram\BotApi;
$token = getenv('TELEGRAM_BOT_TOKEN');
$bot = new BotApi($token);
$me = $bot->getMe();
$chatId = 123456789; // Telegram user/group/channel id from an allowed update context
$sent = $bot->sendMessage($chatId, 'Bot is up. Connected as @' . ($me->getUsername() ?? 'bot'));
use GuzzleHttp\RequestOptions;
use Psr\Log\LoggerInterface;
use Yabx\Telegram\BotApi;
/** @var LoggerInterface $logger e.g. Monolog, Symfony Bridge, … */
$bot = new BotApi(
$token,
[
RequestOptions::TIMEOUT => 30,
RequestOptions::VERIFY => true,
],
$logger,
'https://api.telegram.org', // optional custom API host
);
use Yabx\Telegram\Objects\InlineKeyboardButton;
use Yabx\Telegram\Objects\InlineKeyboardMarkup;
$keyboard = new InlineKeyboardMarkup([
[
new InlineKeyboardButton('Docs', url: 'https://core.telegram.org/bots/api'),
new InlineKeyboardButton('Tap me', callbackData: 'demo:1'),
],
]);
$bot->sendMessage($chatId, 'Pick an action:', replyMarkup: $keyboard);
use Yabx\Telegram\Objects\KeyboardButton;
use Yabx\Telegram\Objects\ReplyKeyboardMarkup;
$markup = new ReplyKeyboardMarkup(
keyboard: [
[new KeyboardButton('Yes'), new KeyboardButton('No')],
[new KeyboardButton('Cancel')],
],
resizeKeyboard: true,
oneTimeKeyboard: true,
);
$bot->sendMessage($chatId, 'Your choice?', replyMarkup: $markup);
use Yabx\Telegram\BotApi;
// Reads php://input and builds an Update (throws if body is empty / invalid JSON)
$update = BotApi::getUpdateFromRequest();
// Or parse a string you already have:
$update = BotApi::getUpdateFromJson($requestBody);
if ($message = $update->getMessage()) {
$chatId = $message->getChat()->getId();
$text = $message->getText();
$photos = $message->getPhotos();
$video = $message->getVideo();
$document = $message->getDocument();
// … same pattern for stickers, polls, forwarded messages, etc.
}
if ($callback = $update->getCallbackQuery()) {
$bot->answerCallbackQuery($callback->getId(), text: 'OK');
}