Telegram bot framework written in PHP.
Telegram bot framework written in PHP, (*1)
To install PHPTelebot with Composer, just add the following to your composer.json
file:, (*2)
{ "require": { "radyakaze/phptelebot": "*" } }
or by running the following command:, (*3)
composer require radyakaze/phptelebot
Composer installs autoloader at ./vendor/autoloader.php
. to include the library in your script, add:, (*4)
require_once 'vendor/autoload.php';
Download the PHP library from Github, then include PHPTelebot.php
in your script:, (*5)
require_once '/path/to/phptelebot/src/PHPTelebot.php';
<?php require_once './src/PHPTelebot.php'; $bot = new PHPTelebot('TOKEN', 'BOT_USERNAME'); // Bot username is optional, its required for handle command that contain username (/command@username) like on a group. // Simple command $bot->cmd('*', 'Hi, human! I am a bot.'); // Simple echo command $bot->cmd('/echo|/say', function ($text) { if ($text == '') { $text = 'Command usage: /echo [text] or /say [text]'; } return Bot::sendMessage($text); }); // Simple whoami command $bot->cmd('/whoami|!whoami', function () { // Get message properties $message = Bot::message(); $name = $message['from']['first_name']; $userId = $message['from']['id']; $text = 'You are <b>'.$name.'</b> and your ID is <code>'.$userId.'</code>'; $options = [ 'parse_mode' => 'html', 'reply' => true ]; return Bot::sendMessage($text, $options); }); $bot->run();
Then run, (*6)
php file.php
You can also see my other sample., (*7)
NOTE: - If function parameters is more than one, PHPTelebot will split text by space. - If you don't set chat_id on options bot will send message to current chat. - If you add option reply => true, bot will reply current message (Only work if you don't set custom chat_id and reply_to_mesage_id)., (*8)
Use $bot->cmd(<command>, <function>)
to handle command., (*9)
// simple answer $bot->cmd('*', 'I am a bot'); // catch multiple commands $bot->cmd('/start|/help', function () { // Do something here. }); // call a function name function googleSearch($search) { // Do something here. } $bot->cmd('/google', 'googleSearch');
Use * to catch any command., (*10)
This code will send a photo to users when type command /upload., (*11)
// Simple photo upload $bot->cmd('/upload', function () { $file = '/path/to/photo.png'; // File path, file id, or url. return Bot::sendPhoto($file); });
Use $bot->on(<event>, <function>)
to handle all possible PHPTelebot events., (*12)
To handle inline message, just add:, (*13)
$bot->on('inline', function($text) { $results[] = [ 'type' => 'article', 'id' => 'unique_id1', 'title' => $text, 'message_text' => 'Lorem ipsum dolor sit amet', ]; $options = [ 'cache_time' => 3600, ]; return Bot::answerInlineQuery($results, $options); });
Also, you can catch multiple events:, (*14)
$bot->on('sticker|photo|document', function() { // Do something here. });
Create a command: /regex string number, (*15)
$bot->regex('/^\/regex (.*) ([0-9])$/i', function($matches) { // Do something here. });
cmd(<command>, <answer>)
Handle a command., (*16)
on(<event>, <answer>)
Handles events., (*17)
regex(<regex>, <answer>)
Create custom regex for command., (*18)
Bot::type()
Return message event type., (*19)
Bot::message()
Get message properties., (*20)
PHPTelebot use standard Telegram Bot API method names., (*21)
Bot::getMe()
?
A simple method for testing your bot's auth token., (*22)
Bot::sendMessage(<text>, <options>)
?
Use this method to send text messages., (*23)
Bot::forwardMessage(<options>)
?
Use this method to forward messages of any kind., (*24)
Bot::sendPhoto(<file path | file id | url>, <options>)
?
Use this method to send a photo., (*25)
Bot::sendVideo(<file path | file id | url>, <options>)
?
Use this method to send a video., (*26)
Bot::sendAudio(<file path | file id | url>, <options>)
?
Use this method to send a audio., (*27)
Bot::sendVoice(<file path | file id | url>, <options>)
?
Use this method to send a voice message., (*28)
Bot::sendDocument(<file path | file id | url>, <options>)
?
Use this method to send a document., (*29)
Bot::sendSticker(<file path | file id | url>, <options>)
?
Use this method to send a sticker., (*30)
Bot::sendLocation(<options>)
?
Use this method to send point on the map., (*31)
Bot::sendVenue(<options>)
?
Use this method to send information about a venue., (*32)
Bot::sendContact(<options>)
?
Use this method to send phone contacts., (*33)
Bot::sendChatAction(<action>, <options>)
?
Use this method when you need to tell the user that something is happening on the bot's side., (*34)
Bot::getUserProfilePhotos(<user id>, <options>)
?
Use this method to get a list of profile pictures for a user., (*35)
Bot::getFile(<file id>)
?
Use this method to get basic info about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size., (*36)
Bot::answerInlineQuery(<array of results>, <options>)
?
Use this method to send answers to an inline query., (*37)
Bot::answerCallbackQuery(<text>, <options>)
?
Use this method to send answers to callback queries sent from inline keyboards., (*38)
Bot::getChat(<chat_id>)
?
Use this method to get up to date information about the chat., (*39)
Bot::leaveChat(<chat_id>)
?
Use this method for your bot to leave a group, supergroup or channel., (*40)
Bot::getChatAdministrators(<chat_id>)
?
Use this method to get a list of administrators in a chat., (*41)
Bot::getChatMembersCount(<chat_id>)
?
Use this method to get the number of members in a chat., (*42)
Bot::getChatMember(<options>)
?
Use this method to get information about a member of a chat., (*43)
Bot::kickChatMember(<options>)
?
Use this method to kick a user from a group or a supergroup., (*44)
Bot::unbanChatMember(<options>)
?
Use this method to unban a previously kicked user in a supergroup., (*45)
Bot::editMessageText(<options>)
?
Use this method to edit text messages sent by the bot or via the bot (for inline bots)., (*46)
Bot::editMessageCaption(<options>)
?
Use this method to edit captions of messages sent by the bot or via the bot (for inline bots)., (*47)
Bot::editMessageReplyMarkup(<options>)
?
Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots)., (*48)
Bot::sendGame(<game short name>, <options>)
?
Use this method to send a game., (*49)
Bot::setGameScore(<options>)
?
Use this method to set the score of the specified user in a game., (*50)
Bot::getGameHighScores(<user id>, <options>)
?
Use this method to get data for high score tables., (*51)
Open via browser https://api.telegram.org/bot<BOT TOKEN>/setWebhook?url=https://yourdomain.com/your_bot.php
, (*52)