TelegramCommandClient
extends Telegram
in package
A {@see Telegram} client that routes slash commands - the counterpart of DiscordPHP's `DiscordCommandClient`.
$bot = new TelegramCommandClient(['token' => $token, 'description' => 'A helpful bot']);
$bot->registerCommand('ping', fn (Message $message) => 'pong');
$bot->registerCommand('echo', fn (Message $message, array $args) => implode(' ', $args), [
'description' => 'Repeats what you say',
]);
$bot->run();
A callback returning a string has it sent back as a reply; returning anything
else (a promise from a call you made yourself, say) leaves the client alone.
/command@thisbot is accepted as well as /command, which is how commands
addressed to one bot in a group are written, and a command addressed to a
different bot is ignored.
With register_commands left on, the listed commands are published to
Telegram with setMyCommands on Event::READY, so they show up in the
client's command menu.
Tags
Table of Contents
Constants
- BOT_API_VERSION : mixed = '10.3'
- The Bot API version the bundled spec was generated from.
- VERSION : mixed = '1.0.0'
Properties
- $chats : ChatRepository
- $users : UserRepository
- $botUser : ?User
- The bot's own account, once `getMe` has answered.
- $factory : Factory
- $http : HttpInterface
- $logger : LoggerInterface
- $loop : LoopInterface
- $options : array<string, mixed>
- $poller : ?Poller
- $ready : bool
- $webhook : ?Server
- $commands : array<string, Command>
- $registry : array<string, Command>
Methods
- __construct() : mixed
- __get() : mixed
- Lazily builds the repository caches.
- __isset() : bool
- downloadFile() : PromiseInterface<string|int, string>
- Downloads a file - either a {@see File} from `getFile`, or a `file_id` this resolves first.
- getBotUser() : ?User
- The bot's own account, once {@see start()} has resolved.
- getCommand() : ?Command
- getCommands() : array<string, Command>
- getFactory() : Factory
- getHttp() : HttpInterface
- getLogger() : LoggerInterface
- getLoop() : LoopInterface
- getOption() : mixed
- One client option.
- getPoller() : ?Poller
- getWebhookServer() : ?Server
- handleUpdate() : Update
- Turns one raw update into parts and emits it - as {@see Event::UPDATE} with the whole {@see Update}, then under its own type with just that payload.
- isReady() : bool
- publishCommands() : PromiseInterface<string|int, bool>
- Publishes the listed commands to Telegram, which is what fills the command menu in the client.
- registerCommand() : Command
- Registers a command.
- request() : PromiseInterface<string|int, mixed>
- Calls any Bot API method by name, including one this build has no generated method for - a new method on a Bot API server that is ahead of the bundled spec, for instance.
- run() : void
- Identifies the bot, starts receiving updates, and runs the event loop.
- shutdown() : void
- Stops receiving updates and stops the event loop.
- start() : PromiseInterface<string|int, User>
- Identifies the bot and starts receiving updates, without running the loop.
- stop() : void
- Stops receiving updates. The loop keeps running unless you stop it too.
- unregisterCommand() : void
- Forgets a command, aliases included.
- callApi() : PromiseInterface<string|int, mixed>
- Dispatches one Bot API call - the hook the generated {@see Methods} trait calls into.
- configureOptions() : void
- Adds the command-routing options on top of the client's own.
- handleMessage() : void
- Routes an incoming message to a command, if it is one.
- hydrate() : mixed
- Builds the result of a call into parts.
- resolveOptions() : array<string, mixed>
- startPolling() : void
- startWebhook() : void
- normalise() : string
- Commands are matched case-insensitively unless that is turned off.
- registerHelpCommand() : void
- The built-in `/help`, which lists what is registered.
Constants
BOT_API_VERSION
The Bot API version the bundled spec was generated from.
public
mixed
BOT_API_VERSION
= '10.3'
VERSION
public
mixed
VERSION
= '1.0.0'
Properties
$chats read-only
public
ChatRepository
$chats
The chats this client has seen or fetched.
$users read-only
public
UserRepository
$users
The users this client has seen.
$botUser
The bot's own account, once `getMe` has answered.
protected
?User
$botUser
= null
$factory
protected
Factory
$factory
$http
protected
HttpInterface
$http
$logger
protected
LoggerInterface
$logger
$loop
protected
LoopInterface
$loop
$options
protected
array<string, mixed>
$options
$poller
protected
?Poller
$poller
= null
$ready
protected
bool
$ready
= false
$webhook
protected
?Server
$webhook
= null
$commands
private
array<string, Command>
$commands
= []
Trigger => command, aliases included.
$registry
private
array<string, Command>
$registry
= []
Name => command, for listing.
Methods
__construct()
public
__construct([array<string, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string, mixed> = []
__get()
Lazily builds the repository caches.
public
__get(string $name) : mixed
Parameters
- $name : string
__isset()
public
__isset(string $name) : bool
Parameters
- $name : string
Return values
booldownloadFile()
Downloads a file - either a {@see File} from `getFile`, or a `file_id` this resolves first.
public
downloadFile(File|string $file) : PromiseInterface<string|int, string>
Parameters
- $file : File|string
Return values
PromiseInterface<string|int, string> —The file's bytes.
getBotUser()
The bot's own account, once {@see start()} has resolved.
public
getBotUser() : ?User
Return values
?UsergetCommand()
public
getCommand(string $name) : ?Command
Parameters
- $name : string
Return values
?CommandgetCommands()
public
getCommands() : array<string, Command>
Return values
array<string, Command> —Registered commands, keyed by name.
getFactory()
public
getFactory() : Factory
Return values
FactorygetHttp()
public
getHttp() : HttpInterface
Return values
HttpInterfacegetLogger()
public
getLogger() : LoggerInterface
Return values
LoggerInterfacegetLoop()
public
getLoop() : LoopInterface
Return values
LoopInterfacegetOption()
One client option.
public
getOption(string $name[, mixed $default = null ]) : mixed
Parameters
- $name : string
- $default : mixed = null
getPoller()
public
getPoller() : ?Poller
Return values
?PollergetWebhookServer()
public
getWebhookServer() : ?Server
Return values
?ServerhandleUpdate()
Turns one raw update into parts and emits it - as {@see Event::UPDATE} with the whole {@see Update}, then under its own type with just that payload.
public
handleUpdate(Update|array<string, mixed> $raw) : Update
Both the poller and the webhook listener funnel through here, so a handler never has to care which one is in use.
Parameters
- $raw : Update|array<string, mixed>
-
A hydrated update, or the payload a webhook delivery arrived as.
Return values
UpdateisReady()
public
isReady() : bool
Return values
boolpublishCommands()
Publishes the listed commands to Telegram, which is what fills the command menu in the client.
public
publishCommands() : PromiseInterface<string|int, bool>
Return values
PromiseInterface<string|int, bool>registerCommand()
Registers a command.
public
registerCommand(string $name, callable(Message, array<int, string>, self): mixed $callback[, array<string, mixed> $options = [] ]) : Command
Parameters
- $name : string
- $callback : callable(Message, array<int, string>, self): mixed
- $options : array<string, mixed> = []
-
description,aliases,listed.
Return values
Commandrequest()
Calls any Bot API method by name, including one this build has no generated method for - a new method on a Bot API server that is ahead of the bundled spec, for instance.
public
request(string $method[, array<string, mixed> $payload = [] ][, array<int, string> $returns = [] ]) : PromiseInterface<string|int, mixed>
Parameters
- $method : string
- $payload : array<string, mixed> = []
- $returns : array<int, string> = []
-
Telegram type tokens; empty leaves the result raw.
Return values
PromiseInterface<string|int, mixed>run()
Identifies the bot, starts receiving updates, and runs the event loop.
public
run([bool $runLoop = true ]) : void
Pass false to keep the loop under your own control - useful when the bot
shares a loop with other ReactPHP services.
Parameters
- $runLoop : bool = true
shutdown()
Stops receiving updates and stops the event loop.
public
shutdown() : void
Named shutdown rather than close because close is a Bot API method of
its own - it releases the bot from Telegram's server before moving it to a
local one - and the generated method for it must keep that name.
start()
Identifies the bot and starts receiving updates, without running the loop.
public
start() : PromiseInterface<string|int, User>
Return values
PromiseInterface<string|int, User> —The bot's own account.
stop()
Stops receiving updates. The loop keeps running unless you stop it too.
public
stop() : void
unregisterCommand()
Forgets a command, aliases included.
public
unregisterCommand(string $name) : void
Parameters
- $name : string
callApi()
Dispatches one Bot API call - the hook the generated {@see Methods} trait calls into.
protected
callApi(string $method, array<string, mixed> $arguments, array<int, string> $returns) : PromiseInterface<string|int, mixed>
Parameters
- $method : string
- $arguments : array<string, mixed>
-
The method's arguments, unset ones included.
- $returns : array<int, string>
-
Telegram type tokens the result hydrates as.
Return values
PromiseInterface<string|int, mixed>configureOptions()
Adds the command-routing options on top of the client's own.
protected
configureOptions(OptionsResolver $resolver) : void
Parameters
- $resolver : OptionsResolver
handleMessage()
Routes an incoming message to a command, if it is one.
protected
handleMessage(Message $message) : void
Parameters
- $message : Message
hydrate()
Builds the result of a call into parts.
protected
hydrate(array<int, string> $returns, mixed $result) : mixed
A few methods return one of two things - editMessageText answers with the
edited Message, or with true when the message is an
inline one the bot cannot read back - so the token that matches the shape
actually returned wins.
Parameters
- $returns : array<int, string>
- $result : mixed
resolveOptions()
protected
resolveOptions(array<string, mixed> $options) : array<string, mixed>
Parameters
- $options : array<string, mixed>
Return values
array<string, mixed>startPolling()
protected
startPolling() : void
startWebhook()
protected
startWebhook() : void
normalise()
Commands are matched case-insensitively unless that is turned off.
private
normalise(string $name) : string
Parameters
- $name : string
Return values
stringregisterHelpCommand()
The built-in `/help`, which lists what is registered.
private
registerHelpCommand(string $name) : void
Parameters
- $name : string