Language: English | Русский | Español | Português | Türkçe | Deutsch | Français | Bahasa Indonesia | العربية | فارسی | Oʻzbek | हिन्दी | 中文 | اردو | Tagalog | አማርኛ

← Home

PHP Telegram bot: get chat_id

In PHP Telegram bots, you get chat_id and user_id from the update. Here are examples.

Using telegram-bot/api (raw)

$update = json_decode(file_get_contents('php://input'), true);

if (isset($update['message'])) {
    $chatId = $update['message']['chat']['id'];
    $userId = $update['message']['from']['id'];
    // Send response...
}

Using longman/telegram-bot

$telegram->on('message', function ($message) {
    $chatId = $message->getChat()->getId();
    $userId = $message->getFrom()->getId();
    $telegram->sendMessage([
        'chat_id' => $chatId,
        'text' => "Chat ID: $chatId, User ID: $userId",
    ]);
});

From callback query

if (isset($update['callback_query'])) {
    $chatId = $update['callback_query']['message']['chat']['id'];
    $userId = $update['callback_query']['from']['id'];
    // ...
}

From channel_post

if (isset($update['channel_post'])) {
    $chatId = $update['channel_post']['chat']['id'];  // channel ID
    // ...
}

Debugging with @print_id_bot

Use @print_id_bot to inspect the raw update:

Forward a message to the bot and send /dump to see the exact format your webhook receives.

FAQ

How do I get chat_id when the bot is added to a group?

When a user sends a message, the webhook receives message.chat.id. Or add @print_id_bot and send /id to get it manually.

Can I use $_POST for update?

Telegram sends updates as JSON in the request body. Use file_get_contents('php://input') or your framework’s input handler.

Where is the update structure documented?

The Telegram Bot API documents the Update object. Use /dump in @print_id_bot for real examples.

What if from is missing?

For channel_post, there is no from. Check isset($update['message']['from']) before accessing.

Is chat_id always an integer?

Yes. In Telegram’s JSON it is a number. PHP may decode it as int or float; ensure you use it correctly in API calls.


Use @print_id_bot /dump to inspect update structure for PHP bots.

Open @print_id_bot


Related pages