Methods
The bot instance can execute all methods listed in the official Telegram Bot API documentation using the following syntax:
php
$bot = new TeleBot('<your bot token>');
// See docs for details: https://core.telegram.org/bots/api#sendmessage
$message = $bot->sendMessage(
chat_id: 1234567890,
text: 'Test message',
reply_markup: [
'inline_keyboard' => [[[
'text' => 'Google',
'url' => 'https://google.com/'
]]]
]
);
Guzzle Promises
You can comfigure TeleBot
methods to return Guzzle promises instead of dispatching requests immediately:
php
$promise = $bot->getMe(_promise: true);
$promise->then(fn (User $user) => var_dump($user))->wait();
Error handling and default values
Instead using try/catch blocks, you can set a default return value or handle exceptions using following syntax:
php
$result = $bot->getMe(_rescue: fn (\Throwable $e) => $e);
var_dump($result); // Will be return value of _rescue callback in case of exception
TeleBot Methods
The WeStacks\TeleBot\TeleBot
class supports all official Telegram API methods and adds some useful helper methods:
Method | Description |
---|---|
handle(Update $update): mixed | Handle a Telegram Update using registered handlers. |
handler($handler): self | Register a new update handler (closure or class-based). |
purge(): void | Recreate bot kernel (all handlers will be removed). |
setLocalCommands(): bool | Push locally registered bot commands to Telegram. |
deleteLocalCommands(): bool | Remove registered commands from Telegram. |
BotManager Methods
The WeStacks\TeleBot\BotManager
supports all TeleBot
methods and allows managing multiple bots:
Method | Description |
---|---|
bot(string|int $name): TeleBot | Get a specific TeleBot instance by name. |
bots(): Array<string|int> | Get a list of registered bot names. |
add(string|int $name, TeleBot|string|array $bot): self | Add a bot instance to the manager. |
remove(string|int $name): self | Remove a bot by name. |
default(string|int $name): self | Set the default bot. |