⚠️ This package is abandoned and no longer maintained.
Please use the nutgram/nutgram package instead.
, (*1)
TelegramBot-PHP
, (*2)
, (*3)
A very simple PHP Telegram Bot API for sending messages., (*4)
Requirements
- PHP ≥ 7.2
- Curl for PHP must be enabled.
- Telegram API Key, you can get one simply with @BotFather with simple commands right after creating your bot.
For the WebHook:
* An SSL certificate (Telegram API requires this). You can use Cloudflare's Free Flexible SSL which crypts the web traffic from end user to their proxies if you're using CloudFlare DNS.
Since the August 29 update you can use a self-signed ssl certificate., (*5)
For the GetUpdates:
* Some way to execute the script in order to serve messages (for example cronjob), (*6)
Installation
You can install this library with composer:, (*7)
composer require lukasss93/telegrambot-php
, (*8)
Configuration (WebHook)
Navigate to
https://api.telegram.org/bot(TOKEN)/setWebhook?url=https://yoursite.com/your_update.php
Or use the Telegram class setWebhook method., (*9)
This simple framework is object-based, all methods return a Telegram Object contained in TelegramBot/Types namespace., (*10)
Examples
use TelegramBot\TelegramBot;
$bot = new TelegramBot($token);
$update=$bot->getWebhookUpdate();
$bot->sendMessage([
'chat_id' => $update->message->chat->id,
'text' => 'Hello world!'
]);
If you want to get some specific parameter from the Telegram webhook, simply call parameter name in the object:, (*11)
use TelegramBot\TelegramBot;
$bot = new TelegramBot($token);
$update=$bot->getWebhookUpdate();
$text=$update->message->text;
To upload a Photo or some other files, you need to load it with CurlFile:, (*12)
// Load a local file to upload. If is already on Telegram's Servers just pass the resource id
$img = curl_file_create('test.png','image/png');
$bot->sendPhoto([
'chat_id' => $chat_id,
'photo' => $img
]);
To download a file on the Telegram's servers, (*13)
$file = $bot->getFile($file_id);
$bot->downloadFile($file->file_path, "./my_downloaded_file_on_local_server.png");
If you want to use getUpdates instead of the WebHook you need a for cycle., (*14)
$updates=$bot->getUpdated();
for ($i = 0; $i < count($updates); $i++) {
//single update
$update=$updates[$i];
if($update->message->getCommand()=='/start'){
//working!
}
}
Message Object Methods
Message object mainly provide 2 methods:
* getCommand(), (*15)
```php
//$update->message->text => '/test@ExampleBot my args'
$command=$update->message->getCommand();
//$command => '/text'
```
* getArgs(bool $asString=false), (*16)
```php
//$update->message->text => '/test@ExampleBot my args'
$args_array=$update->message->getArgs();
//$args_array[0] => 'my'
//$args_array[1] => 'args'
$args_string=$update->message->getArgs(true);
//$args_string => 'my args'
```
Build keyboard parameters
Telegram's bots can have two different kind of keyboards: Inline and Reply.
The InlineKeyboard is linked to a particular message, while the ReplyKeyboard is linked to the whole chat.
They are both an array of array of buttons, which rapresent the rows and columns.
For instance you can arrange a ReplyKeyboard using this code:, (*17)
$buttons = [
//First row
[
$bot->buildKeyboardButton("Button 1"),
$bot->buildKeyboardButton("Button 2")
],
//Second row
[
$bot->buildKeyboardButton("Button 3"),
$bot->buildKeyboardButton("Button 4"),
$bot->buildKeyboardButton("Button 5")],
//Third row
[
$bot->buildKeyboardButton("Button 6")]
];
$bot->sendMessage([
'chat_id' => $chat_id,
'reply_markup' => $bot->buildKeyBoard($buttons),
'text' => 'This is a Keyboard Test'
]);
When a user click on the button, the button text is send back to the bot.
For an InlineKeyboard it's pretty much the same (but you need to provide a valid URL or a Callback data), (*18)
$buttons = [
//First row
[
$bot->buildInlineKeyBoardButton("Button 1", $url="http://link1.com"),
$bot->buildInlineKeyBoardButton("Button 2", $url="http://link2.com")
],
//Second row
[
$bot->buildInlineKeyBoardButton("Button 3", $url="http://link3.com"),
$bot->buildInlineKeyBoardButton("Button 4", $url="http://link4.com"),
$bot->buildInlineKeyBoardButton("Button 5", $url="http://link5.com")
],
//Third row
[
$bot->buildInlineKeyBoardButton("Button 6", $url="http://link6.com")
]
];
$bot->sendMessage([
'chat_id' => $chat_id,
'reply_markup' => $bot->buildInlineKeyBoard($buttons),
'text' => 'This is a Keyboard Test'
]);
This is the list of all the helper functions to make keyboards easily:, (*19)
$bot->buildKeyBoard(array $options, $onetime=true, $resize=true, $selective=true);
Send a custom keyboard. $option is an array of array KeyboardButton.
Check ReplyKeyBoardMarkUp for more info., (*20)
$bot->buildInlineKeyBoard(array $inline_keyboard);
Send a custom keyboard. $inline_keyboard is an array of array InlineKeyboardButton.
Check InlineKeyboardMarkup for more info., (*21)
$bot->buildInlineKeyBoardButton($text, $url, $callback_data, $switch_inline_query);
Create an InlineKeyboardButton.
Check InlineKeyBoardButton for more info., (*22)
$bot->buildKeyBoardButton($text, $url, $request_contact, $request_location);
Create a KeyboardButton.
Check KeyBoardButton for more info., (*23)
$bot->buildKeyBoardHide($selective=true);
Hide a custom keyboard.
Check ReplyKeyBoarHide for more info., (*24)
$bot->buildForceReply($selective=true);
Show a Reply interface to the user.
Check ForceReply for more info., (*25)
You can contact me via Telegram but if you have an issue
please open one., (*26)
To-Do list
- Optional predictive parameters in methods
- Optimize keyboards
- Chat conversations
- Commands listener with callback + events
Changelog
All notable changes to this project will be documented here., (*27)