tg-file-decoder/README.md

138 lines
4.9 KiB
Markdown
Raw Normal View History

2020-02-03 23:47:37 +01:00
# tg-file-decoder
2020-08-24 18:31:01 +02:00
![build](https://github.com/danog/tg-file-decoder/workflows/build/badge.svg)
2020-02-04 00:02:42 +01:00
2024-04-01 18:03:39 +02:00
Decode and encode [Telegram bot API file IDs](https://core.telegram.org)!
2020-02-03 23:47:37 +01:00
## Install
```bash
composer require danog/tg-file-decoder
```
## Examples:
### Decoding bot API file IDs
```php
use danog\Decoder\FileId;
use danog\Decoder\UniqueFileId;
$fileId = FileId::fromBotAPI('CAACAgQAAxkDAAJEsl44nl3yxPZ8biI8uhaA7rbQceOSAAKtAQACsTisUXvMEbVnTuQkGAQ');
2024-04-01 18:03:39 +02:00
$version = $fileId->version; // bot API file ID version, usually 4
$subVersion = $fileId->subVersion; // bot API file ID subversion, equivalent to a specific tdlib version
2020-02-03 23:47:37 +01:00
2024-04-01 18:03:39 +02:00
$dcId = $fileId->dcId; // On which datacenter is this file stored
2020-02-03 23:47:37 +01:00
2024-04-01 18:03:39 +02:00
$type = $fileId->type; // File type
2020-02-03 23:47:37 +01:00
2024-04-01 18:03:39 +02:00
$id = $fileId->id;
$accessHash = $fileId->accessHash;
2020-02-03 23:47:37 +01:00
2024-04-01 18:03:39 +02:00
$fileReference = $fileId->fileReference; // File reference, https://core.telegram.org/api/file_reference
$url = $fileId->url; // URL, file IDs with encoded webLocation
2020-02-03 23:47:37 +01:00
// You can also use hasFileReference and hasUrl
$fileIdReencoded = $fileId->getBotAPI(); // CAACAgQAAxkDAAJEsl44nl3yxPZ8biI8uhaA7rbQceOSAAKtAQACsTisUXvMEbVnTuQkGAQ
$fileIdReencoded = (string) $fileId; // CAACAgQAAxkDAAJEsl44nl3yxPZ8biI8uhaA7rbQceOSAAKtAQACsTisUXvMEbVnTuQkGAQ
2024-04-01 18:03:39 +02:00
// For photos, thumbnails if ($fileId->getType() <= FileIdType::PHOTO->value)
$volumeId = $fileId->volumeId;
$localId = $fileId->localId;
2020-02-03 23:47:37 +01:00
2024-04-01 18:03:39 +02:00
$photoSizeSource = $fileId->photoSizeSource; // PhotoSizeSource object
$photoSizeSource->dialogId;
$photoSizeSource->stickerSetId;
2020-02-03 23:47:37 +01:00
// And more, depending on photosize source
```
The bot API subversion, present since file IDs v4, is equivalent to the [version of tdlib](https://github.com/tdlib/td/blob/master/td/telegram/Version.h#L13) used server-side in the bot API.
For file types, see [file types](#bot-api-file-id-types).
For photosize source, see [here](#photosize-source).
### Decoding bot API unique file IDs
```php
$uniqueFileId = UniqueFileId::fromUniqueBotAPI('AgADrQEAArE4rFE');
2024-04-01 18:03:39 +02:00
$type = $fileId->type; // Unique file type
2020-02-03 23:47:37 +01:00
2024-04-01 18:03:39 +02:00
$id = $uniqueFileId->id;
$url = $uniqueFileId->url; // URL, for unique file IDs with encoded webLocation
2020-02-03 23:47:37 +01:00
// For photos
2024-04-01 18:03:39 +02:00
$volumeId = $uniqueFileId->volumeId;
$localId = $uniqueFileId->localId;
2020-02-03 23:47:37 +01:00
```
For unique file types, see [unique types](#bot-api-unique-file-id-types).
### Extracting unique file IDs from full file IDs
```php
$full = 'CAACAgQAAxkDAAJEsl44nl3yxPZ8biI8uhaA7rbQceOSAAKtAQACsTisUXvMEbVnTuQkGAQ';
$unique = 'AgADrQEAArE4rFE';
$fileId = FileId::fromBotAPI($full);
$uniqueFileId = UniqueFileId::fromUniqueBotAPI($unique);
$uniqueFileIdExtracted1 = UniqueFileId::fromBotAPI($full);
$uniqueFileIdExtracted2 = $fileId->getUniqueBotAPI();
var_dump(((string) $uniqueFileId) === ((string) $uniqueFileIdExtracted1)); // true, always AgADrQEAArE4rFE!
var_dump(((string) $uniqueFileId) === ((string) $uniqueFileIdExtracted2)); // true, always AgADrQEAArE4rFE!
```
### Photosize source
```php
$fileId = FileId::fromString('CAACAgQAAxkDAAJEsl44nl3yxPZ8biI8uhaA7rbQceOSAAKtAQACsTisUXvMEbVnTuQkGAQ');
2024-04-01 18:03:39 +02:00
$photoSizeSource = $fileId->photoSizeSource; // PhotoSizeSource object
2020-02-03 23:47:37 +01:00
2024-04-01 18:03:39 +02:00
$sourceType = $photoSizeSource->type;
2020-02-03 23:47:37 +01:00
2020-02-03 23:58:17 +01:00
// If $sourceType === PHOTOSIZE_SOURCE_DIALOGPHOTO_SMALL|PHOTOSIZE_SOURCE_DIALOGPHOTO_SMALL or
// If $photoSizeSource instanceof PhotoSizeSourceDialogPhoto
2024-04-01 18:03:39 +02:00
$dialogId = $photoSizeSource->dialogId;
$dialogId = $photoSizeSource->sticketSetId;
2020-02-03 23:47:37 +01:00
```
2020-02-03 23:58:17 +01:00
The `PhotoSizeSource` abstract base class indicates the source of a specific photosize from a chat photo, photo, stickerset thumbnail, file thumbnail.
2024-04-01 18:03:39 +02:00
Click [here &raquo;](https://github.com/danog/tg-file-decoder/blob/master/docs/index.md) to view the full list of `PhotoSizeSource` types.
2020-02-03 23:58:17 +01:00
2020-02-03 23:47:37 +01:00
### Building custom file IDs
```php
2024-04-01 18:04:53 +02:00
$fileId = new FileId(
type: FileIdType::STICKER,
id: $id,
accessHash: $accessHash,
// and so on...
);
2020-02-03 23:47:37 +01:00
$encoded = (string) $fileId; // CAACAgQAAxkDAAJEsl44nl3yxPZ8biI8uhaA7rbQceOSAAKtAQACsTisUXvMEbVnTuQkGAQ, or something
```
2020-02-03 23:58:17 +01:00
### Bot API file ID types
2020-02-03 23:47:37 +01:00
2024-04-01 18:20:14 +02:00
The file type is a PHP enum indicating the type of file, [danog\Decoder\FileIdType](https://github.com/danog/tg-file-decoder/blob/master/docs/danog/Decoder/FileIdType.md).
2020-02-03 23:47:37 +01:00
2024-04-01 18:20:14 +02:00
Click [here &raquo;](https://github.com/danog/tg-file-decoder/blob/master/docs/danog/Decoder/FileIdType.md) to view the full list of file ID types.
The enum also offers a `FileIdType::from` method that can be used to obtain the correct case, from a string version of the file type, typically the one used in bot API file objects.
2020-02-03 23:58:17 +01:00
2024-04-01 18:20:14 +02:00
### Bot API unique file ID types
2020-02-03 23:58:17 +01:00
2024-04-01 18:20:14 +02:00
The unique file type is a PHP enum uniquely indicating the unique file, [danog\Decoder\UniqueFileIdType](https://github.com/danog/tg-file-decoder/blob/master/docs/danog/Decoder/UniqueFileIdType.md).
2020-02-03 23:58:17 +01:00
2024-04-01 18:20:14 +02:00
Click [here &raquo;](https://github.com/danog/tg-file-decoder/blob/master/docs/danog/Decoder/UniqueFileIdType.md) to view the full list of file ID types.
2020-02-03 23:58:17 +01:00
2020-02-03 23:47:37 +01:00
## Full API documentation
2024-04-01 18:03:39 +02:00
Click [here &raquo;](https://github.com/danog/tg-file-decoder/blob/master/docs/index.md) to view the full API documentation.