PHP code example of phenogram / bindings
1. Go to this page and download the library: Download phenogram/bindings 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/ */
phenogram / bindings example snippets
use Phenogram\Bindings\Serializer;
$serializer = new Serializer();
$inlineKeyboardMarkup = new InlineKeyboardMarkup(
inlineKeyboard: [[
new InlineKeyboardButton(text: 'Кнопка 1', callbackData: 'data1')
]],
);
$data = $serializer->serialize([
'reply_markup' => $inlineKeyboardMarkup,
]);
$arrayKeyboard = [
'reply_markup' => [
'inline_keyboard' => [[
['text' => 'Кнопка 1', 'callback_data' => 'data1']
]],
],
];
assert($arrayKeyboard === $data);
use Phenogram\Bindings\Serializer;
use Phenogram\Bindings\Types\Update;
use Phenogram\Bindings\Types\Message;
use Phenogram\Bindings\Types\Chat;
$updatesData = [[
'update_id' => 1,
'message' => [
'message_id' => 54321,
'chat' => [
'id' => 11223344,
'type' => 'private',
],
'date' => 1600000000,
],
]];
$serializer = new Serializer();
$updates = $serializer->deserialize(
data: json_encode($updatesData),
type: Update::class,
isArray: true,
);
assert($updates[0] instanceof Update::class);
assert($updates[0]->message instanceof Message::class);
assert($updates[0]->message->chat instanceof Chat::class);
use Phenogram\Bindings\ClientInterface;
use Phenogram\Bindings\Types;
final readonly class ReadmeClient implements ClientInterface
{
public function __construct(
private string $token,
private string $apiUrl = 'https://api.telegram.org',
) {
}
public function sendRequest(string $method, array $data): Types\Response
{
$ch = curl_init("{$this->apiUrl}/bot{$this->token}/{$method}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Проверка наличия InputFile объектов в $data и конвертация их в CURLFile
foreach ($data as $key => $value) {
if ($value instanceof Types\InputFile) {
if (file_exists($value->filePath)) {
$data[$key] = new \CURLFile($value->filePath);
} else {
throw new \RuntimeException("Файл не найден: {$value->filePath}");
}
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new \RuntimeException('Ошибка запроса: ' . curl_error($ch));
}
curl_close($ch);
$responseData = json_decode($response, true);
if (!isset($responseData['ok']) || !isset($responseData['result'])) {
return new Types\Response(
ok: false,
result: null,
errorCode: $responseData['error_code'] ?? null,
description: $responseData['description'] ?? null,
parameters: isset($responseData['parameters']) ? new Types\ResponseParameters(
migrateToChatId: $responseData['parameters']['migrate_to_chat_id'] ?? null,
retryAfter: $responseData['parameters']['retry_after'] ?? null,
) : null,
);
}
return new Types\Response(
ok: $responseData['ok'],
result: $responseData['result'],
errorCode: $responseData['error_code'] ?? null,
description: $responseData['description'] ?? null,
parameters: isset($responseData['parameters']) ? new Types\ResponseParameters(
migrateToChatId: $responseData['parameters']['migrate_to_chat_id'] ?? null,
retryAfter: $responseData['parameters']['retry_after'] ?? null,
) : null,
);
}
}
$api = new Api(
client: new TelegramBotApiClient($token),
serializer: new Serializer(),
);
$me = $api->getMe();
assert($me instanceof User::class);