TelegramText
in package
The parts of turning a message into text that are Telegram's rules rather than anybody's.
Everything shared — resolving Discord's mention markup, truncating on a character boundary, naming a file — lives in MessageText and is used from here. What is left is Telegram's HTML mode, its limits, and the several shapes a chat can be referred to by.
Tags
Table of Contents
Constants
- CAPTION_LIMIT : mixed = 1024
- And in a caption attached to a photo or file, which is far shorter.
- LIMIT : mixed = 4096
- What Telegram accepts in one message.
Methods
- compose() : string|null
- Renders a Discord message for Telegram, as HTML, or `null` when there is nothing worth relaying.
- escapeHtml() : string
- Escapes the three characters Telegram's HTML mode reserves.
- normalizeChatId() : string|null
- Normalises however somebody referred to a Telegram chat into the form the Bot API takes: a numeric id, or an `@username`.
- redact() : string
- Removes the bot token from text that is about to be logged or shown.
- redacted() : RuntimeException
- An exception safe to hand onwards: the message redacted, and no `previous`, since a logger that renders the chain would print the original unredacted.
Constants
CAPTION_LIMIT
And in a caption attached to a photo or file, which is far shorter.
public
mixed
CAPTION_LIMIT
= 1024
LIMIT
What Telegram accepts in one message.
public
mixed
LIMIT
= 4096
Methods
compose()
Renders a Discord message for Telegram, as HTML, or `null` when there is nothing worth relaying.
public
static compose(Outgoing $message[, int $limit = self::LIMIT ]) : string|null
The author is bolded into the text rather than sent as a separate message, because Telegram has no per-message identity to borrow the way a Discord webhook does — one bot account says everything the bridge relays, so the name has to be in the message or it is nowhere.
Parameters
- $message : Outgoing
- $limit : int = self::LIMIT
Return values
string|nullescapeHtml()
Escapes the three characters Telegram's HTML mode reserves.
public
static escapeHtml(string $text) : string
Only these three. Escaping more — ", ' — shows up as literal entities
in the chat, because Telegram does not decode them.
Parameters
- $text : string
Tags
Return values
stringnormalizeChatId()
Normalises however somebody referred to a Telegram chat into the form the Bot API takes: a numeric id, or an `@username`.
public
static normalizeChatId(string $input) : string|null
Accepts a raw id (-1001234567890), a username with or without the @,
and a public t.me/name link. Rejects private invite links
(t.me/+hash, t.me/joinchat/hash) with null: those are joins, not
chat ids, and there is no way to turn one into an id from outside. The
caller is expected to say so, rather than storing a link that can never
resolve.
What this returns is what gets stored, so it has to be stable: normalising one way today and another tomorrow orphans every bridge made before the change.
Parameters
- $input : string
Return values
string|nullredact()
Removes the bot token from text that is about to be logged or shown.
public
static redact(string $text) : string
Every Bot API URL has the token in its path — /bot<id>:<secret>/… and
/file/bot<id>:<secret>/… — so any error that quotes one quotes the
credential. TelegramPHP is careful not to, but the transport underneath
it makes no such promise, and the cost of trusting it once is the whole
bot. Anything Telegram-side that reaches a log line or a reply passes
through here first.
Parameters
- $text : string
Return values
stringredacted()
An exception safe to hand onwards: the message redacted, and no `previous`, since a logger that renders the chain would print the original unredacted.
public
static redacted(Throwable $e) : RuntimeException
Parameters
- $e : Throwable