-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Complete reference for all TGbotPHP methods.
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
);Send a text message.
$bot->sendMessage(
chatId: int|string,
text: string = '',
photo: string|null = null,
parseMode: string = 'html',
keyboard: array|null = null
): voidParameters:
-
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'])
);Handle text commands.
$bot->commandSimple(
command: string,
output: array|string
): voidParameters:
-
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");Handle button clicks.
$bot->simpleCallbackResponse(
callbackData: string,
output: array|string,
edit: bool = true
): voidParameters:
-
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);Create inline keyboard with buttons.
$keyboard = $bot->buildKeyboardOfInline(
buttons: array<string, string>
): arrayParameters:
-
buttons- Associative array of [text => callback_data]
Example:
$keyboard = $bot->buildKeyboardOfInline([
"Button 1" => "btn1",
"Button 2" => "btn2",
"Button 3" => "btn3",
]);Create keyboard with URL buttons.
$keyboard = $bot->buildKeyboardOfLinks(
links: array<string, string>
): arrayParameters:
-
links- Associative array of [text => url]
Example:
$keyboard = $bot->buildKeyboardOfLinks([
"GitHub" => "https://github.com",
"Telegram" => "https://telegram.org",
]);Merge two keyboards.
$merged = $bot->mergeKeyboards(
keyboard1: array,
keyboard2: array
): arrayExample:
$keyboard = $bot->mergeKeyboards($inlineKeyboard, $linkKeyboard);Merge multiple keyboards.
$merged = $bot->mergeMultipleKeyboards(
keyboards: array
): arrayExample:
$keyboard = $bot->mergeMultipleKeyboards([
$keyboard1,
$keyboard2,
$keyboard3,
]);Get text from message.
$text = $bot->getTextMessage(): string|nullExample:
$userMessage = $bot->getTextMessage();
if ($userMessage) {
echo "User said: $userMessage";
}Get callback data.
$data = $bot->getData(): string|nullExample:
$buttonClicked = $bot->getData();Get chat ID.
$chatId = $bot->getChatId(): int|string|nullGet message ID.
$messageId = $bot->getMessageId(): int|nullCheck if message matches text.
$matches = $bot->checkTextMessage(
messageToCheck: string
): boolExample:
if ($bot->checkTextMessage("hello")) {
$bot->sendMessage($bot->getChatId(), "Hi there!");
}Check if callback data matches.
$matches = $bot->checkCallbackQueryData(
dataToCheck: string
): boolCheck if chat is private.
$isPrivate = $bot->isPrivate(): boolExample:
if ($bot->isPrivate()) {
// Only respond in private chats
}Forward message between chats.
$bot->forwardMessage(
chatId: int|string,
messageId: int,
fromChatId: int|string
): voidForward from reply to message.
$bot->forwardMessageFromReply(
chatId: int|string,
fromBot: bool = false
): voidEdit existing message.
$bot->editMessage(
output: array|string
): voidValidate Telegram IP address.
$isValid = botTG::checkIp(
ip: string
): boolExample:
if (botTG::checkIp($_SERVER['REMOTE_ADDR'])) {
// Request is from Telegram
}Most methods accept output as either:
$bot->sendMessage($chatId, "Simple text");$bot->sendMessage($chatId, [
"text" => "Message text",
"photo" => "photo.png",
"keyboard" => $keyboard,
"parse_mode" => "html" // or "markdown"
]);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}}!");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'
);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