PHP code example of andisiahaan / gramsea-telegram-bot
1. Go to this page and download the library: Download andisiahaan/gramsea-telegram-bot 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/ */
andisiahaan / gramsea-telegram-bot example snippets
use AndiSiahaan\GramseaTelegramBot\Gramsea;
use AndiSiahaan\GramseaTelegramBot\Support\InlineKeyboard;
$bot = new Gramsea('YOUR_BOT_TOKEN');
// Text message dengan keyboard
$bot->message()
->to($chatId)
->text('Hello **world**!') // Markdown otomatis dikonversi ke HTML
->keyboard(InlineKeyboard::make()->callback('Click Me', 'action'))
->send();
// Photo dengan caption
$bot->media()
->to($chatId)
->photo('https://example.com/image.jpg')
->caption('Check this out!')
->silent()
->send();
// Media group (album)
$bot->mediaGroup()
->to($chatId)
->photo('photo1.jpg')
->photo('photo2.jpg')
->video('video.mp4')
->caption('My album!')
->send();
// Text dengan link preview options
$bot->text()
->to($chatId)
->text('Check this article!')
->previewUrl('https://example.com/article')
->largePreview()
->send();
$bot = new Gramsea('YOUR_BOT_TOKEN');
// Magic method - panggil API langsung
$bot->sendMessage([...]);
$bot->sendPhoto([...]);
$bot->sendDocument([...]);
// dll...
// MessageSender - auto-delegates based on content
$bot->message()->to($chatId)->text('Hello!')->send();
// TextSender - text dengan link preview options
$bot->text()->to($chatId)->text('Hello!')->noPreview()->send();
// MediaSender - single media
$bot->media()->to($chatId)->photo('url')->caption('Nice!')->send();
// MediaGroupSender - album (2-10 items)
$bot->mediaGroup()->to($chatId)->photo('a.jpg')->photo('b.jpg')->send();
use AndiSiahaan\GramseaTelegramBot\MessageSender;
// Text only
MessageSender::bot($bot)
->to($chatId)
->text('Hello **world**!')
->keyboard($keyboard)
->send();
// Single media
MessageSender::bot($bot)
->to($chatId)
->photo('https://example.com/image.jpg')
->text('Caption here')
->send();
// Media group
MessageSender::bot($bot)
->to($chatId)
->photo('photo1.jpg')
->photo('photo2.jpg')
->text('Album!')
->send();
use AndiSiahaan\GramseaTelegramBot\TextSender;
TextSender::bot($bot)
->to($chatId)
->text('Check this: https://example.com')
->noPreview() // Disable link preview
->send();
// Advanced link preview options
TextSender::bot($bot)
->to($chatId)
->text('Amazing article!')
->previewUrl('https://example.com/article')
->largePreview() // Prefer large media
->previewAboveText() // Show preview above text
->send();
use AndiSiahaan\GramseaTelegramBot\MediaSender;
MediaSender::bot($bot)
->to($chatId)
->photo('https://example.com/image.jpg')
->caption('Check this out!')
->keyboard($keyboard)
->send();
// Dengan auto-detect media type
MediaSender::bot($bot)
->to($chatId)
->media('https://example.com/video.mp4') // Auto-detect: video
->caption('My video')
->send();
$result->totalSent(); // Jumlah sukses
$result->totalBlocked(); // Jumlah blocked (403/404)
$result->totalFailed(); // Jumlah gagal lainnya
$result->totalProcessed(); // Total yang diproses
$result->successRate(); // Persentase sukses
$result->getSent(); // Array of chat_ids yang sukses
$result->getBlocked(); // Array of chat_ids yang blocked
$result->getFailed(); // Array of chat_ids yang failed
$result->isAllSuccess(); // True jika semua sukses
use AndiSiahaan\GramseaTelegramBot\Support\CallbackData;
// Encode callback
$data = CallbackData::encode('user', ['123', 'view']); // 'user:123:view'
// Decode callback
$parts = CallbackData::decode('user:123:view'); // ['user', '123', 'view']
// Parse dengan named params
$parsed = CallbackData::parse('user:123:view', ['id', 'action']);
// ['action' => 'user', 'id' => '123', 'action' => 'view']
// Check action
CallbackData::is($data, 'user'); // true
CallbackData::startsWith($data, 'user'); // true
CallbackData::getAction($data); // 'user'
CallbackData::getParam($data, 0); // '123'
// Compact encoding untuk ID besar (base62)
$compact = CallbackData::compact('u', [1234567890]); // 'u:1LY7VK' (lebih pendek!)
$parsed = CallbackData::parseCompact('u:1LY7VK', [0]); // decode balik ke integer
// Validasi
CallbackData::isValid($data); // true jika <= 64 bytes
CallbackData::remainingBytes($data); // sisa bytes yang tersedia
use AndiSiahaan\GramseaTelegramBot\Curl;
// Set timeout
Curl::setTimeout(60); // Request timeout
Curl::setConnectTimeout(15); // Connection timeout
// Enable retry
Curl::setRetry(3, 1000); // 3 attempts, 1 second delay
// Download file
Curl::download('https://example.com/file.pdf', '/path/to/save.pdf');
use AndiSiahaan\GramseaTelegramBot\Gramsea;
use AndiSiahaan\GramseaTelegramBot\Exception\ApiException;
use AndiSiahaan\GramseaTelegramBot\Exception\ForbiddenException;
use AndiSiahaan\GramseaTelegramBot\Exception\TooManyRequestsException;
use AndiSiahaan\GramseaTelegramBot\Exception\NetworkException;
try {
$bot->sendMessage([...]);
} catch (ForbiddenException $e) {
// Bot diblok atau tidak punya akses
if ($e->isBotBlocked()) {
echo "User telah memblokir bot";
} elseif ($e->isBotKicked()) {
echo "Bot telah di-kick dari group";
}
} catch (TooManyRequestsException $e) {
// Rate limited - tunggu dan retry
sleep($e->getWaitTime());
// retry...
} catch (ApiException $e) {
// Catch-all untuk API error lain
echo "Error: " . $e->getMessage();
echo "Code: " . $e->getErrorCode();
} catch (NetworkException $e) {
// Network error (timeout, connection, SSL)
if ($e->isTimeout()) {
echo "Request timed out";
}
}
$e->getMessage(); // Pesan error dari Telegram
$e->getErrorCode(); // HTTP error code (400, 403, 429, dll)
$e->getDescription(); // Description dari Telegram response
$e->getResponse(); // Full response array
$e->getRetryAfter(); // Waktu tunggu (untuk rate limit), null jika tidak ada
$e->getMigrateToChatId(); // Chat ID baru (untuk supergroup migration)
$e->isRetryable(); // True jika error bisa di-retry
$e->isBotBlocked(); // Bot diblok user
$e->isBotKicked(); // Bot di-kick dari group
$e->hasNoRightsToSend(); // Bot tidak punya hak kirim pesan
$e->isUserDeactivated(); // User sudah dihapus/nonaktif
$e->isChatNotFound(); // Chat tidak ditemukan
$e->isMessageNotFound(); // Message tidak ditemukan
$e->isWebhookConflict(); // Conflict karena webhook
$e->isGetUpdatesConflict(); // Conflict karena getUpdates aktif
$e->getWaitTime(); // Waktu tunggu dalam detik (default 30)
$e->isRetryable(); // Selalu true