DiscordPHP-BridgeBot Documentation

Media
in package

FinalYes

Works out what a Telegram message is carrying besides text, and whether the bridge can mirror it into Discord.

Operates on the plain array a Message serialises to rather than on the part itself, so every decision here — which of the nine media fields won, which photo size to take, what the thing should be called, whether it is small enough to re-upload — is testable without a client, a token, or a network.

Why files are re-uploaded rather than linked

A Telegram file has a public download URL, but it is https://api.telegram.org/file/bot<TOKEN>/<path> — it contains the bot token. Posting one into Discord would hand the bot's full credentials to everyone who can read the channel, which is why the bridge downloads the bytes and attaches them instead, and why that URL is never logged.

Tags
author

Valithor Obsidion valithor@valgorithms.com

Table of Contents

Constants

DISCORD_UPLOAD_LIMIT  : mixed = 8 * 1024 * 1024
The largest file the bridge will re-upload to Discord.
TELEGRAM_DOWNLOAD_LIMIT  : mixed = 20 * 1024 * 1024
The largest file `getFile` will serve from the cloud Bot API at all.

Methods

caption()  : string|null
The caption that came with a media message, if any.
describe()  : array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}|null
What this message is carrying, or `null` when it is text only.
largestPhoto()  : array<string, mixed>
The largest of the photo sizes Telegram offers.
safeFilename()  : string
Strips a Telegram-supplied filename down to something safe to hand to Discord as an attachment name — no directories, no control characters, and never empty.
entry()  : array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}
personName()  : string
How a Telegram user is named in a service line.
service()  : array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}|null
Telegram's service messages — somebody joined, the group was renamed, a message was pinned.

Constants

DISCORD_UPLOAD_LIMIT

The largest file the bridge will re-upload to Discord.

public mixed DISCORD_UPLOAD_LIMIT = 8 * 1024 * 1024

Discord's own limit is 10 MiB for a server without boosts, and a rejected upload costs a wasted download of the whole file first; 8 MiB keeps a margin for the multipart overhead and for the smaller effective limits some endpoints apply.

TELEGRAM_DOWNLOAD_LIMIT

The largest file `getFile` will serve from the cloud Bot API at all.

public mixed TELEGRAM_DOWNLOAD_LIMIT = 20 * 1024 * 1024

A local Bot API server has no such limit, but the bridge cannot tell from a message which one it is talking to, so it declines early rather than issuing a request it expects to fail.

Tags
link
https://core.telegram.org/bots/api#getfile

Methods

caption()

The caption that came with a media message, if any.

public static caption(array<string, mixed> $message) : string|null
Parameters
$message : array<string, mixed>
Return values
string|null

describe()

What this message is carrying, or `null` when it is text only.

public static describe(array<string, mixed> $message) : array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}|null

The order matters: Telegram sets exactly one media field per message, but document is also set alongside animation for a GIF, so the more specific field has to win.

Parameters
$message : array<string, mixed>

A serialised Telegram message.

Return values
array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}|null

largestPhoto()

The largest of the photo sizes Telegram offers.

public static largestPhoto(array<int, array<string, mixed>> $sizes) : array<string, mixed>

Telegram sends a message's photo as several resolutions of the same image; the last is normally the biggest, but that is convention rather than a guarantee, so this compares them.

Parameters
$sizes : array<int, array<string, mixed>>
Return values
array<string, mixed>

safeFilename()

Strips a Telegram-supplied filename down to something safe to hand to Discord as an attachment name — no directories, no control characters, and never empty.

public static safeFilename(string $name) : string
Parameters
$name : string
Return values
string

entry()

private static entry(string $kind, string $label, mixed $fileId, mixed $size, string|null $filename) : array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}
Parameters
$kind : string
$label : string
$fileId : mixed
$size : mixed
$filename : string|null
Return values
array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}

personName()

How a Telegram user is named in a service line.

private static personName(array<string, mixed> $user) : string
Parameters
$user : array<string, mixed>
Return values
string

service()

Telegram's service messages — somebody joined, the group was renamed, a message was pinned.

private static service(array<string, mixed> $message) : array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}|null

These arrive as an ordinary message with no text at all, so without this the bridge would drop them silently and a Discord reader would never learn that the group they are reading has changed under them.

Parameters
$message : array<string, mixed>
Return values
array{kind: string, label: string, file_id: ?string, size: ?int, filename: ?string, mirrorable: bool}|null
On this page

Search results