Build a bot using the Client class
masterThe Client class provides a high-level interface for handling updates, commands, and running the bot loop. You can register command handlers with $bot->command() and general update handlers with $bot->on().
require_once "vendor/autoload.php";
try {
$bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN');
// Handle /ping command
$bot->command('ping', function ($message) use ($bot) {
$bot->sendMessage($message->getChat()->getId(), 'pong!');
});
// Handle all text messages
$bot->on(function (\TelegramBot\Api\Types\Update $update) use ($bot) {
$message = $update->getMessage();
$id = $message->getChat()->getId();
$bot->sendMessage($id, 'Your message: ' . $message->getText());
}, function () {
return true;
});
$bot->run();
} catch (\TelegramBot\Api\Exception $e) {
$e->getMessage();
}