A minimal library to develop your new Telegram bot
composer require webpajooh/telebot
We start by creating an instance of TeleBot class:
try {
$tg = new TeleBot('YOUR_BOT_TOKEN');
} catch (Throwable $th) {...}There are short ways to access the update object and some important fields. I recommend you to read the official documentation to understand these objects well.
$tg->update
$tg->message
$tg->chat
$tg->user
You also can use the hasCallbackQuery() method, when you want to check if the update object has a callback_query field.
Thanks to magic methods, we can use API methods without implementing them, and just call them by name and pass an array as parameter:
$tg->editMessageText([...])You may define some routes to your bot features; define them by the listen() method:
$tg->listen('/start', function () use ($tg) {
$tg->sendMessage([
'chat_id' => $tg->user->id,
'text' => 'Hello, world!',
]);
}, false);The third parameter that is true by default, makes you able to terminate the script after running a command. In the previous example we passed false so script continues.
You can also get parameters and use them as variables:
$tg->listen('set_age_%d', function ($age) use ($tg) {
// TODO
});TeleBot translates them to regex, so it will be good to take a look at this table to know how to use them properly:
| Type | TeleBot | Regex |
|---|---|---|
| Digits | %d | (\d+) |
| String (Anything but a whitespace) | %s | (\S+) |
| Character | %c | (\S) |
| Everything including an empty string | %p | (.*) |
You may need to log something into a log.txt file:
Logger::log($tg->user->id);
tl($tg->user->id); // Does the same thingTeleBot includes two classes for making keyboards; InlineKeyboard and ReplyKeyboard. Here you see an example:
$keyboard = (new InlineKeyboard())
->addCallbackButton('π Help', 'help_callback')
->addUrlButton('π± Share', 'https://t.me/share/url?url=https://t.me/your_awesome_bot&text=Some text')
->chunk(1)
->rightToLeft()
->get();Then you can use it as bellow:
$tg->sendMessage([
// Other parameters
'reply_markup' => $keyboard,
]);Consider that the chunk() method supports more complex orders, just pass an array like [1, 3, 2] to build such a keyboard:
[ 1 ] [ 2 ] [ 3οΈ ] [ 4 ] [ 5 ] [ 6 ]
Sometimes you do not want to repeat yourself by passing a parameter everywhere, so you can define default parameters for each method. Here are three example that makes it clear how to use it:
$tg->setDefaults('sendMessage', ['parse_mode' => 'html']); // You will not need passing parse_mode anymore
$tg->setDefaults(['sendMessage', 'banChatMember'], ['chat_id' => $chatId]);
$tg->setDefaults('*', ['chat_id' => $chatId]); // a default parameter for all methodsYou may want to add some methods to TeleBot class to improve your code readability and avoid duplication. Look at this simple example as an inspiration:
TeleBot::extend('isReply', function () {
return property_exists($this->message, 'reply_to_message');
});
if ($tg->isReply()) { ... }Create an issue and explain your problem!