-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
maule edited this page Aug 16, 2026
·
1 revision
Learn how to set up TGbotPHP and create your first bot.
- PHP 7.0 or higher (PHP 8.4+ recommended)
- cURL extension enabled
- A Telegram Bot Account
- A web server with HTTPS support
- Open Telegram and search for @BotFather
- Send
/newbotcommand - Follow the prompts to name your bot
- Copy the API Token (you'll need this)
Example token: 123456789:ABCdefGHIjklmnoPQRstuvWXyz_ABCDE
- Download
botlib.phpfrom GitHub - Place it in your project directory
- Include it in your PHP file:
<?php
require_once "botlib.php";composer require lightyagami28/tgbotphpCreate a .env file or set environment variables:
export TELEGRAM_BOT_TOKEN="your_token_here"
export DEBUG_MODE="false"Create webhook.php:
<?php
declare(strict_types=1);
use TGbotPHP\botTG;
require_once "botlib.php";
// Get token from environment
$token = getenv('TELEGRAM_BOT_TOKEN');
if (!$token) {
http_response_code(500);
error_log('TELEGRAM_BOT_TOKEN not configured');
exit;
}
// Enforce HTTPS
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') {
http_response_code(403);
exit('HTTPS required');
}
// Initialize bot
$updates = file_get_contents("php://input");
$bot = new botTG(
token: $token,
updates: $updates,
debug: (bool) getenv('DEBUG_MODE')
);
// Your bot logic here...
http_response_code(200);You need an HTTPS URL (HTTP won't work with Telegram):
https://your-domain.com/webhook.php
Using cURL:
curl -X POST "https://api.telegram.org/bot<YOUR_TOKEN>/setWebhook" \
-d "url=https://your-domain.com/webhook.php"Using PHP:
<?php
$token = "your_token_here";
$url = "https://your-domain.com/webhook.php";
$ch = curl_init("https://api.telegram.org/bot{$token}/setWebhook");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => ['url' => $url],
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
echo $response;curl "https://api.telegram.org/bot<YOUR_TOKEN>/getWebhookInfo"Expected response:
{
"ok": true,
"result": {
"url": "https://your-domain.com/webhook.php",
"has_custom_certificate": false,
"pending_update_count": 0
}
}<?php
declare(strict_types=1);
use TGbotPHP\botTG;
require_once "botlib.php";
$token = getenv('TELEGRAM_BOT_TOKEN');
if (!$token) {
http_response_code(500);
exit;
}
// Enforce HTTPS
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') {
http_response_code(403);
exit;
}
// Create bot instance
$updates = file_get_contents("php://input");
$bot = new botTG(token: $token, updates: $updates);
// Only in private chats
if ($bot->isPrivate()) {
$message = $bot->getTextMessage();
if ($message) {
$bot->sendMessage(
chatId: $bot->getChatId(),
text: "You said: $message"
);
}
}
http_response_code(200);<?php
declare(strict_types=1);
use TGbotPHP\botTG;
require_once "botlib.php";
$token = getenv('TELEGRAM_BOT_TOKEN');
$updates = file_get_contents("php://input");
$bot = new botTG(token: $token, updates: $updates);
// Handle /start command
$bot->commandSimple("/start", [
"text" => "Welcome {{message from first_name}}! 👋",
"keyboard" => $bot->buildKeyboardOfInline([
"Click me" => "button1",
"Help" => "help",
]),
]);
// Handle /help command
$bot->commandSimple("/help", "This bot echoes your messages!");
http_response_code(200);- Open Telegram
- Search for your bot's username
- Send
/startcommand - You should see your response
If nothing happens:
- Check webhook info:
https://api.telegram.org/bot<TOKEN>/getWebhookInfo - Check server logs
- See Troubleshooting guide
Before going live:
- Bot token in environment variable (not hardcoded)
- HTTPS enforcement enabled
- Webhook registered with Telegram
- Error logging configured
- Security headers added
- Tested with real users
- Backup and recovery plan
- Learn about Keyboards and Callbacks
- Explore API Reference
- Check out Examples
- Read Security Guide
Having issues? Check Troubleshooting →