Skip to content

API Reference

maule edited this page Aug 16, 2026 · 1 revision

API Reference

Complete reference for all TGbotPHP methods.

Constructor

botTG::__construct()

Initialize a new bot instance.

$bot = new botTG(
    token: string,
    updates: string|false = false,
    debug: bool = false,
    debugFile: string|false = false
);

Parameters:

  • token (string) - Telegram Bot API token
  • updates (string|false) - JSON webhook update from Telegram (optional)
  • debug (bool) - Enable debug mode (optional)
  • debugFile (string|false) - Log errors to file (optional)

Example:

$bot = new botTG(
    token: getenv('TELEGRAM_BOT_TOKEN'),
    updates: file_get_contents('php://input'),
    debug: false
);

Message Methods

sendMessage()

Send a text message.

$bot->sendMessage(
    chatId: int|string,
    text: string = '',
    photo: string|null = null,
    parseMode: string = 'html',
    keyboard: array|null = null
): void

Parameters:

  • chatId - Chat ID to send message to
  • text - Message text (supports templates)
  • photo - Photo file path (optional)
  • parseMode - HTML or Markdown (optional)
  • keyboard - Keyboard markup (optional)

Example:

$bot->sendMessage(
    chatId: 123456789,
    text: "Hello {{message from first_name}}!",
    keyboard: $bot->buildKeyboardOfInline(['Click' => 'btn1'])
);

commandSimple()

Handle text commands.

$bot->commandSimple(
    command: string,
    output: array|string
): void

Parameters:

  • command - Command to match (e.g., "/start")
  • output - Response text or configuration array

Example:

$bot->commandSimple("/start", [
    "text" => "Welcome!",
    "keyboard" => $mainKeyboard,
    "photo" => "welcome.png"
]);

$bot->commandSimple("/help", "Use /start to begin");

simpleCallbackResponse()

Handle button clicks.

$bot->simpleCallbackResponse(
    callbackData: string,
    output: array|string,
    edit: bool = true
): void

Parameters:

  • callbackData - Callback data to match
  • output - Response configuration
  • edit - Edit existing message (true) or send new (false)

Example:

$bot->simpleCallbackResponse("button1", [
    "text" => "Button clicked!",
    "keyboard" => $backKeyboard
]);

// Send as new message instead of editing
$bot->simpleCallbackResponse("button2", "New message", edit: false);

Keyboard Methods

buildKeyboardOfInline()

Create inline keyboard with buttons.

$keyboard = $bot->buildKeyboardOfInline(
    buttons: array<string, string>
): array

Parameters:

  • buttons - Associative array of [text => callback_data]

Example:

$keyboard = $bot->buildKeyboardOfInline([
    "Button 1" => "btn1",
    "Button 2" => "btn2",
    "Button 3" => "btn3",
]);

buildKeyboardOfLinks()

Create keyboard with URL buttons.

$keyboard = $bot->buildKeyboardOfLinks(
    links: array<string, string>
): array

Parameters:

  • links - Associative array of [text => url]

Example:

$keyboard = $bot->buildKeyboardOfLinks([
    "GitHub" => "https://github.com",
    "Telegram" => "https://telegram.org",
]);

mergeKeyboards()

Merge two keyboards.

$merged = $bot->mergeKeyboards(
    keyboard1: array,
    keyboard2: array
): array

Example:

$keyboard = $bot->mergeKeyboards($inlineKeyboard, $linkKeyboard);

mergeMultipleKeyboards()

Merge multiple keyboards.

$merged = $bot->mergeMultipleKeyboards(
    keyboards: array
): array

Example:

$keyboard = $bot->mergeMultipleKeyboards([
    $keyboard1,
    $keyboard2,
    $keyboard3,
]);

Message Inspection

getTextMessage()

Get text from message.

$text = $bot->getTextMessage(): string|null

Example:

$userMessage = $bot->getTextMessage();
if ($userMessage) {
    echo "User said: $userMessage";
}

getData()

Get callback data.

$data = $bot->getData(): string|null

Example:

$buttonClicked = $bot->getData();

getChatId()

Get chat ID.

$chatId = $bot->getChatId(): int|string|null

getMessageId()

Get message ID.

$messageId = $bot->getMessageId(): int|null

Message Checking

checkTextMessage()

Check if message matches text.

$matches = $bot->checkTextMessage(
    messageToCheck: string
): bool

Example:

if ($bot->checkTextMessage("hello")) {
    $bot->sendMessage($bot->getChatId(), "Hi there!");
}

checkCallbackQueryData()

Check if callback data matches.

$matches = $bot->checkCallbackQueryData(
    dataToCheck: string
): bool

Chat Inspection

isPrivate()

Check if chat is private.

$isPrivate = $bot->isPrivate(): bool

Example:

if ($bot->isPrivate()) {
    // Only respond in private chats
}

Utility Methods

forwardMessage()

Forward message between chats.

$bot->forwardMessage(
    chatId: int|string,
    messageId: int,
    fromChatId: int|string
): void

forwardMessageFromReply()

Forward from reply to message.

$bot->forwardMessageFromReply(
    chatId: int|string,
    fromBot: bool = false
): void

editMessage()

Edit existing message.

$bot->editMessage(
    output: array|string
): void

checkIp() (Static)

Validate Telegram IP address.

$isValid = botTG::checkIp(
    ip: string
): bool

Example:

if (botTG::checkIp($_SERVER['REMOTE_ADDR'])) {
    // Request is from Telegram
}

Output Configuration

Most methods accept output as either:

String Format

$bot->sendMessage($chatId, "Simple text");

Array Format

$bot->sendMessage($chatId, [
    "text" => "Message text",
    "photo" => "photo.png",
    "keyboard" => $keyboard,
    "parse_mode" => "html" // or "markdown"
]);

Template Variables

Use these in message text:

Variable Replaced With
{{message_text}} User's message
{{message from first_name}} User's first name
{{callback_data}} Callback button data

Example:

$bot->sendMessage($chatId, "Hello {{message from first_name}}!");

Error Handling

All methods return void. Errors are logged based on debug configuration.

$bot = new botTG(
    token: $token,
    updates: $updates,
    debug: true,
    debugFile: '/var/log/telegram-bot.log'
);

Return Values

The $bot->update property contains the raw Telegram update:

if ($bot->update?->message?->text) {
    echo "Message: " . $bot->update->message->text;
}

See also: Examples, Security Guide