Skip to content

Getting Started

maule edited this page Aug 16, 2026 · 1 revision

Getting Started

Learn how to set up TGbotPHP and create your first bot.

Prerequisites

  • PHP 7.0 or higher (PHP 8.4+ recommended)
  • cURL extension enabled
  • A Telegram Bot Account
  • A web server with HTTPS support

Step 1: Create Your Bot

  1. Open Telegram and search for @BotFather
  2. Send /newbot command
  3. Follow the prompts to name your bot
  4. Copy the API Token (you'll need this)

Example token: 123456789:ABCdefGHIjklmnoPQRstuvWXyz_ABCDE

Step 2: Installation

Option A: Download Directly

  1. Download botlib.php from GitHub
  2. Place it in your project directory
  3. Include it in your PHP file:
<?php
require_once "botlib.php";

Option B: Using Composer

composer require lightyagami28/tgbotphp

Step 3: Configuration

Environment Setup

Create a .env file or set environment variables:

export TELEGRAM_BOT_TOKEN="your_token_here"
export DEBUG_MODE="false"

PHP Configuration

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);

Step 4: Set Up Webhook

Get Your Domain

You need an HTTPS URL (HTTP won't work with Telegram):

https://your-domain.com/webhook.php

Register Webhook with Telegram

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;

Verify Webhook

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
  }
}

Step 5: Create Your First Bot

Simple Echo Bot

<?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);

Bot with Commands

<?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);

Step 6: Test Your Bot

  1. Open Telegram
  2. Search for your bot's username
  3. Send /start command
  4. You should see your response

If nothing happens:

  1. Check webhook info: https://api.telegram.org/bot<TOKEN>/getWebhookInfo
  2. Check server logs
  3. See Troubleshooting guide

Deployment Checklist

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

Next Steps


Having issues? Check Troubleshooting

Clone this wiki locally