Skip to content

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:

MethodDescription
handle(Update $update): mixedHandle a Telegram Update using registered handlers.
handler($handler): selfRegister a new update handler (closure or class-based).
purge(): voidRecreate bot kernel (all handlers will be removed).
setLocalCommands(): boolPush locally registered bot commands to Telegram.
deleteLocalCommands(): boolRemove registered commands from Telegram.

BotManager Methods

The WeStacks\TeleBot\BotManager supports all TeleBot methods and allows managing multiple bots:

MethodDescription
bot(string|int $name): TeleBotGet 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): selfAdd a bot instance to the manager.
remove(string|int $name): selfRemove a bot by name.
default(string|int $name): selfSet the default bot.