Telegram bot framework written in PHP with support for the latest Bot API features
- Business Account Support - Handle business connections and messages
- Gifts & Premium Features - Support for Telegram gifts and premium subscriptions
- Enhanced Media Support - Video notes, animations, paid media
- Modern Interactions - Polls, dice games, reactions, boosts
- Forum Management - Complete forum topic handling
- Web Apps Integration - Full Web App support
- Advanced Keyboards - Request users, chats, contacts, and locations
- Giveaways & Contests - Handle Telegram giveaways
- Video Chat Events - Monitor video chat activities
- Star Payments - Handle Telegram Star transactions
- Simple, easy to use.
- Support Long Polling and Webhook.
- Support for latest Telegram Bot API 9.0+ features
- Business account integration
- Comprehensive event handling
- Modern inline keyboards and Web Apps
- cURL
- PHP 5.4+
- Telegram Bot API Token - Talk to @BotFather and generate one.
Using Composer
To install PHPTelebot with Composer, just add the following to your composer.json
file:
{
"require": {
"radyakaze/phptelebot": "^2.0"
}
}
or by running the following command:
composer require radyakaze/phptelebot
Composer installs autoloader at ./vendor/autoloader.php
. to include the library in your script, add:
require_once 'vendor/autoload.php';
Download the PHP library from Github, then include PHPTelebot.php
in your script:
require_once '/path/to/phptelebot/src/PHPTelebot.php';
<?php
require_once './src/PHPTelebot.php';
$bot = new PHPTelebot('TOKEN', 'BOT_USERNAME'); // Bot username is optional
// Simple command
$bot->cmd('*', 'Hi, human! I am a bot with latest Telegram features!');
// Simple echo command
$bot->cmd('/echo|/say', function ($text) {
if ($text == '') {
$text = 'Command usage: /echo [text] or /say [text]';
}
return Bot::sendMessage($text);
});
// Send a poll
$bot->cmd('/poll', function () {
$question = 'What is your favorite programming language?';
$options = ['PHP', 'Python', 'JavaScript', 'Java', 'C++'];
return Bot::sendPoll($question, [
'options' => json_encode($options),
'is_anonymous' => false,
'allows_multiple_answers' => true
]);
});
// Send dice games
$bot->cmd('/dice', function () {
return Bot::sendDice('π²'); // Dice
});
$bot->cmd('/dart', function () {
return Bot::sendDice('π―'); // Dart
});
$bot->cmd('/basketball', function () {
return Bot::sendDice('π'); // Basketball
});
$bot->run();
// Handle business connections
$bot->on('business_connection', function ($connection) {
return Bot::sendMessage('Business connection established!', [
'business_connection_id' => $connection['id']
]);
});
// Handle business messages
$bot->on('business_message', function ($message) {
return Bot::sendMessage('Received: ' . $message['text'], [
'business_connection_id' => $message['business_connection_id']
]);
});
// Handle gifts
$bot->on('gift', function ($gift) {
$giftType = isset($gift['sticker']) ? 'regular gift' : 'unique gift';
return Bot::sendMessage("Thank you for the $giftType! π");
});
// Handle paid media
$bot->on('paid_media', function () {
return Bot::sendMessage('Thank you for purchasing paid media! β');
});
// Send gifts (requires appropriate permissions)
$bot->cmd('/sendgift', function () {
return Bot::sendGift('gift_id_here');
});
$bot->cmd('/keyboard', function () {
$keyboard[] = [
['text' => 'Web App', 'web_app' => ['url' => 'https://example.com/webapp']],
['text' => 'Request Contact', 'request_contact' => true],
];
$keyboard[] = [
['text' => 'Request Users', 'request_users' => [
'request_id' => 1,
'user_is_bot' => false
]],
['text' => 'Request Chat', 'request_chat' => [
'request_id' => 2,
'chat_is_channel' => false
]],
];
return Bot::sendMessage('Modern keyboard features', [
'reply_markup' => ['inline_keyboard' => $keyboard]
]);
});
// Send animations/GIFs
$bot->cmd('/gif', function () {
return Bot::sendAnimation('https://example.com/animation.gif', [
'caption' => 'Cool animation!'
]);
});
// Send video notes (circle videos)
$bot->cmd('/videonote', function () {
return Bot::sendVideoNote('/path/to/video_note.mp4');
});
// Send paid media
$bot->cmd('/paidmedia', function () {
return Bot::sendPaidMedia(100, [ // 100 stars
'media' => json_encode([
['type' => 'photo', 'media' => 'photo_url_here']
])
]);
});
// Handle forum topics
$bot->on('forum_topic_created', function ($topic) {
return Bot::sendMessage("New topic: " . $topic['name']);
});
// Handle chat boosts
$bot->on('chat_boost', function ($boost) {
$booster = $boost['source']['user']['first_name'] ?? 'Anonymous';
return Bot::sendMessage("Thanks for boosting, $booster! π");
});
// Handle message reactions
$bot->on('message_reaction', function ($reaction) {
$userId = $reaction['user']['id'] ?? 'Unknown';
return Bot::sendMessage("User $userId reacted to a message");
});
// Handle giveaways
$bot->on('giveaway', function ($giveaway) {
$prizeCount = $giveaway['winner_count'];
return Bot::sendMessage("Giveaway with $prizeCount prizes! π");
});
$bot->on('giveaway_completed', function ($completed) {
return Bot::sendMessage("Giveaway completed! π");
});
Use $bot->cmd(<command>, <function>)
to handle command.
// 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.
Use $bot->on(<event>, <function>)
to handle all possible PHPTelebot events.
- * - any type of message
- text β text message
- audio β audio file
- voice β voice message
- document β document file (any kind)
- photo β photo
- sticker β sticker
- video β video file
- video_note β video note (circle video)
- animation β animation/GIF
- contact β contact data
- location β location data
- venue β venue data
- poll β poll
- dice β dice result
- game β game
- paid_media β paid media content
- gift β gift (regular or unique)
- paid_message_price_changed β paid message price change
- business_connection β business account connection
- business_message β business account message
- edited_business_message β edited business message
- deleted_business_messages β deleted business messages
- new_chat_member β new member was added
- left_chat_member β member was removed
- new_chat_title β new chat title
- new_chat_photo β new chat photo
- delete_chat_photo β chat photo was deleted
- group_chat_created β group has been created
- channel_chat_created β channel has been created
- supergroup_chat_created β supergroup has been created
- migrate_to_chat_id β group has been migrated to a supergroup
- migrate_from_chat_id β supergroup has been migrated from a group
- pinned_message β message was pinned
- invoice β invoice for payment
- successful_payment β successful payment
- refunded_payment β refunded payment
- users_shared β users shared
- chat_shared β chat shared
- connected_website β website connected
- write_access_allowed β write access allowed
- passport_data β Telegram Passport data
- proximity_alert_triggered β proximity alert triggered
- boost_added β boost added to chat
- chat_background_set β chat background set
- forum_topic_created β forum topic created
- forum_topic_edited β forum topic edited
- forum_topic_closed β forum topic closed
- forum_topic_reopened β forum topic reopened
- general_forum_topic_hidden β general forum topic hidden
- general_forum_topic_unhidden β general forum topic unhidden
- giveaway_created β giveaway created
- giveaway β giveaway message
- giveaway_winners β giveaway winners selected
- giveaway_completed β giveaway completed
- video_chat_scheduled β video chat scheduled
- video_chat_started β video chat started
- video_chat_ended β video chat ended
- video_chat_participants_invited β participants invited to video chat
- edited β edited message
- inline - inline message
- chosen_inline_result - chosen inline result
- callback - callback message
- shipping_query - shipping query
- pre_checkout_query - pre-checkout query
- poll_update - poll state update
- poll_answer - poll answer
- my_chat_member - bot's chat member status update
- chat_member - chat member status update
- chat_join_request - chat join request
- chat_boost - chat boost
- removed_chat_boost - removed chat boost
- message_reaction - message reaction
- message_reaction_count - message reaction count update
- channel - channel post
- edited_channel - edited channel post
- web_app_data - web app data
Create a command: /regex string number
$bot->regex('/^\/regex (.*) ([0-9])$/i', function($matches) {
// Do something here.
});
Handle a command.
Handle an event.
Handle a custom regex pattern.
Start the bot (Long Polling or Webhook mode).
All Telegram Bot API methods are supported through magic methods:
Bot::sendMessage($text, $options)
Bot::sendPhoto($photo, $options)
Bot::sendVideo($video, $options)
Bot::sendVideoNote($videoNote, $options)
Bot::sendAnimation($animation, $options)
Bot::sendAudio($audio, $options)
Bot::sendVoice($voice, $options)
Bot::sendDocument($document, $options)
Bot::sendSticker($sticker, $options)
Bot::sendLocation($latitude, $longitude, $options)
Bot::sendVenue($latitude, $longitude, $title, $address, $options)
Bot::sendContact($phoneNumber, $firstName, $options)
Bot::sendPoll($question, $options)
Bot::sendDice($emoji, $options)
Bot::sendPaidMedia($starCount, $options)
Bot::sendGift($giftId, $options)
Bot::getChat($chatId)
Bot::getChatMember($chatId, $userId)
Bot::getChatMemberCount($chatId)
Bot::banChatMember($chatId, $userId, $options)
Bot::unbanChatMember($chatId, $userId, $options)
Bot::restrictChatMember($chatId, $userId, $permissions, $options)
Bot::promoteChatMember($chatId, $userId, $options)
Bot::createForumTopic($chatId, $name, $options)
Bot::editForumTopic($chatId, $messageThreadId, $options)
Bot::closeForumTopic($chatId, $messageThreadId)
Bot::reopenForumTopic($chatId, $messageThreadId)
Bot::deleteForumTopic($chatId, $messageThreadId)
Bot::getBusinessConnection($businessConnectionId)
Bot::refundStarPayment($userId, $telegramPaymentChargeId)
Bot::getStarTransactions($options)
And many more! All Bot API methods are available through the magic __callStatic
method.
- Version updated to 2.0
- Enhanced event handling for new message types
- Business message support requires handling business_connection_id
- Some method signatures may have changed for consistency
- Business Integration: Add business event handlers
- Enhanced Media: Use new media types (video_note, animation, paid_media)
- Modern Keyboards: Implement Web Apps and request buttons
- Community Features: Handle reactions, boosts, and forum topics
- Giveaways: Implement giveaway event handling
This project is licensed under the GPL-3.0 License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
If you find this project helpful, please give it a β on GitHub!