Skip to content

The Telegram API

The typed methods cover everyday tasks. For everything else, invoke() calls any method of the Telegram API (MTProto) with your account, through the connection Telegram Web already has. There is no separate login, api_id, or MTProto library involved.

$contacts = $client->invoke('contacts.getContacts', ['hash' => 0]);

Pass the method name exactly as in Telegram’s documentation, such as messages.search or users.getFullUser, and its parameters as an array. Results come back as Telegram’s own objects, decoded into PHP arrays, with the type name in the _ key:

$result = $client->invoke('messages.search', [
'peer' => $client->inputPeer($chatId),
'q' => 'invoice',
'filter' => ['_' => 'inputMessagesFilterEmpty'],
'min_date' => 0,
'max_date' => 0,
'offset_id' => 0,
'add_offset' => 0,
'limit' => 20,
'max_id' => 0,
'min_id' => 0,
'hash' => 0,
]);
foreach ($result['messages'] ?? [] as $message) {
echo $message['message'] ?? '', PHP_EOL;
}

Constructors are written as arrays with a _ key, like ['_' => 'inputMessagesFilterEmpty']. Check each method’s page in the Telegram documentation for its required parameters.

Many methods take an InputPeer. inputPeer() builds it for a chat ID the account already knows:

$peer = $client->inputPeer($chatId);

If Telegram Web has not seen the chat yet, inputPeer() throws TelegramWebException asking you to load it with getChats() first. For a username, resolve it with resolveUsername() and then call inputPeer().

JSON has no binary strings or 64-bit integers that survive JavaScript. Wrap them:

Value Write it as
Bytes ['$bytes' => base64_encode($bytes)]
64-bit integer ['$long' => '1234567890123456789']

When Telegram rejects a call, invoke() throws Pam\TelegramWeb\Exception\TelegramWebException with Telegram’s error text, for example FLOOD_WAIT_30 or CHAT_ADMIN_REQUIRED. Each call can take up to requestTimeoutSeconds (30 seconds by default).