Chats & Messages
Once the account is logged in, the client gives you the chat list, message history, and everything you can do in Telegram Web: send text and files, edit, delete, react, forward, and manage groups and channels. Chats and messages are typed objects; IDs are plain integers.
foreach ($client->getChats(50) as $chat) { // most recent first printf( "%d %s %s unread=%d%s\n", $chat->id, $chat->type->value, // private, bot, group, channel $chat->title, $chat->unreadCount, $chat->pinned ? ' (pinned)' : '', );
echo ' last: ', $chat->lastMessage?->text ?? '', PHP_EOL;}A Chat has id, type (ChatType), title, username, unreadCount, pinned, lastMessage, and raw, plus shortcuts: sendMessage(), sendFile(), fetchMessages(), markAsRead(), sendAction(), members(), and leave().
History
Section titled “History”$messages = $client->getMessages($chatId, 20); // newest first
foreach ($messages as $message) { printf("%s %s: %s\n", date('c', $message->date), $message->fromName ?? 'me', $message->text);}Sending text
Section titled “Sending text”$sent = $client->sendMessage($chatId, 'Hello from Acme!');$client->sendMessage($chatId, 'Quiet update', ['silent' => true, 'noWebpage' => true]);$client->sendMessage($chatId, 'Answering this one', ['replyTo' => $messageId]);
$chat->sendMessage('Hello!');$message->reply('Got it, thanks.');sendMessage() returns the sent Message, or null if Telegram took more than 15 seconds to confirm it. The supported options are replyTo (a message ID), silent, and noWebpage.
Sending files
Section titled “Sending files”$client->sendFile($chatId, __DIR__.'/invoice.pdf', 'Your invoice');$client->sendFile($chatId, __DIR__.'/photo.jpg', 'Look!'); // sent as a photo$client->sendFile($chatId, __DIR__.'/photo.jpg', '', ['asDocument' => true]); // sent as a file$client->sendFileContents($chatId, $pngBytes, 'chart.png');
$message->replyWithFile(__DIR__.'/report.csv');JPEG, PNG, and WebP images are sent as photos, and MP4 and WebM videos as videos, unless you pass asDocument. Telegram shows a video without an audio track as a GIF. Other options are replyTo, silent, fileName, and mimeType. The call returns once the upload has finished.
Downloading media
Section titled “Downloading media”use Pam\TelegramWeb\MediaType;
if ($message->hasMedia && in_array($message->mediaType, [MediaType::Photo, MediaType::Document], true)) { echo $message->fileName ?? 'photo', ' ', $message->mimeType, ' ', $message->fileSize, PHP_EOL;
$path = $message->download()->saveTo(__DIR__.'/downloads'); echo "Saved to {$path}\n";}mediaType is a MediaType: Photo, Video, VideoNote, Animation, Audio, Voice, Sticker, Document, Location, Contact, Poll, or Other.
download() (or $client->downloadMedia($chatId, $messageId)) returns a DownloadedFile with name, mimeType, contents, and size(). saveTo() accepts a file path, or a directory in which case it keeps the original, sanitized file name, and returns the path it wrote.
Files cross between the browser and PHP in 6 MB chunks. ClientOptions sets the time limit per file (fileTimeoutSeconds, default 300 seconds) and the largest download (maxDownloadBytes, default 100 MB).
Managing messages
Section titled “Managing messages”$message->edit('Fixed a typo');$message->react('👍'); // null removes your reaction$message->forwardTo($otherChatId);$message->delete(); // for everyone, when Telegram allows it
$client->editMessage($chatId, $messageId, 'New text');$client->deleteMessages($chatId, [101, 102], forEveryone: false);$client->forwardMessages($fromChatId, [101, 102], $toChatId, ['dropAuthor' => true]);$client->react($chatId, $messageId, '🔥');$client->markAsRead($chatId);forwardMessages() with dropAuthor sends the messages as copies, without the “Forwarded from” header; it also accepts silent.
Chat actions
Section titled “Chat actions”Show “typing…” or another status for a few seconds:
use Pam\TelegramWeb\ChatAction;
$client->sendChatAction($chatId, ChatAction::Typing);$chat->sendAction(ChatAction::UploadDocument);Available actions: Typing, UploadPhoto, UploadVideo, UploadDocument, RecordVoice, ChooseSticker, and Cancel.
Groups and channels
Section titled “Groups and channels”$groupId = $client->createGroup('Release team', 'Deploy talk', [$aliceId]); // a supergroup$channelId = $client->createChannel('Acme News', 'Product updates');$link = $client->inviteLink($groupId); // https://t.me/+...
$chatId = $client->join('@acme_news'); // @username, t.me link, or t.me/+ invite link$members = $client->getMembers($chatId, 50); // list of User$peerId = $client->resolveUsername('@alice'); // users > 0, groups and channels < 0
$client->leave($chatId);$client->deleteChat($groupId); // a group or channel you own, for everyonecreateGroup()creates a supergroup, so it has history, reactions, and invite links. The users you add must allow being added by you.join()with an invite link to a group you are already in returns its ID instead of failing.- A
Userhasid,firstName,lastName,username,phone,isBot, andname().