PHP code example of botgate / sdk

1. Go to this page and download the library: Download botgate/sdk 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/ */

    

botgate / sdk example snippets


use BotGate\Client;

$client = Client::create('ВАШ_API_КЛЮЧ');

$response = $client->bot('public-bot-id')->call('sendMessage', [
    'chat_id' => 123456789,
    'text' => 'Привет из BotGate!',
]);

if ($response->ok) {
    // $response->result — содержимое поля "result" ответа Telegram
}

use BotGate\Client;
use BotGate\Config;

$config = new Config(
    apiKey: 'ВАШ_API_КЛЮЧ',
    baseUrl: 'https://bot-gate.ru',
    timeout: 30.0,
    maxRetries: 3,
    retryBaseDelayMs: 500,
);

$client = Client::fromConfig($config);

$client = new Client($config, $customHttpClient);

$bot = $client->bot('public-bot-id');

// JSON-запрос
$bot->call('sendMessage', ['chat_id' => 1, 'text' => 'hi']);

// Загрузка файлов (multipart/form-data)
use BotGate\Http\MultipartField;

$bot->callMultipart('sendPhoto', [
    new MultipartField('chat_id', '1'),
    MultipartField::file('photo', '/path/to/photo.jpg'),
]);

// Скачивание файла по его file_path
$stream = $bot->downloadFile('photos/file_0.jpg');

use BotGate\Webhook\SignatureValidator;

$validator = new SignatureValidator('webhook-секрет');

if (! $validator->isValid($rawBody, $signature)) {
    // 403
}

// либо строгий вариант с исключением
$validator->validate($rawBody, $signature); // InvalidSignatureException

use BotGate\DTO\Update;
use BotGate\DTO\UpdateType;

$update = Update::fromJson($rawBody);

$update->updateId;            // int
$update->type();              // UpdateType
$update->is(UpdateType::Message);

$message = $update->message();        // array|null
$callback = $update->callbackQuery(); // array|null

$response->ok;          // bool
$response->result;      // mixed
$response->description; // ?string
$response->errorCode;   // ?int
$response->raw;         // array — исходный ответ целиком

use BotGate\Exception\BotGateExceptionInterface;
use BotGate\Exception\RateLimitException;
use BotGate\Exception\TelegramException;
use BotGate\Exception\UnauthorizedException;

try {
    $client->bot('id')->call('sendMessage', $params);
} catch (RateLimitException $e) {
    $wait = $e->retryAfter();
    // Лимит BotGate. При известном $wait отложите задание в очереди.
} catch (TelegramException $e) {
    if ($e->telegramErrorCode() === 429) {
        $wait = $e->retryAfter();
        // Лимит Telegram. При известном $wait отложите задание в очереди.
    } else {
        // Исправьте запрос или права бота с учётом telegramErrorCode().
    }
} catch (UnauthorizedException $e) {
    // 401 — неверный API-ключ
} catch (BotGateExceptionInterface $e) {
    // любая другая ошибка SDK
}