Hey there, fellow developer! Ready to dive into the world of Telegram bots? You're in for a treat. We'll be using the awesome php-telegram-bot package to make our lives easier. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get our project structure in order:
mkdir telegram-bot cd telegram-bot composer init composer require longman/php-telegram-bot
Easy peasy, right? Now we've got our project set up with the php-telegram-bot package ready to roll.
Time to create our bot! Head over to Telegram and chat with the BotFather. He's a cool dude who'll help you set up your bot and give you an API token. Keep that token safe – it's the key to your bot's kingdom!
Let's create our main PHP file and get the basics going:
<?php require __DIR__ . '/vendor/autoload.php'; use Longman\TelegramBot\Telegram; $bot = new Telegram('YOUR_BOT_API_TOKEN');
Replace 'YOUR_BOT_API_TOKEN' with the token BotFather gave you. You're now ready to start building your bot's brain!
Now for the fun part – making your bot do stuff:
// Set up webhook $bot->setWebhook('https://your-domain.com/bot.php'); // Handle incoming commands $bot->handle(); // Create a command class StartCommand extends \Longman\TelegramBot\Commands\UserCommand { protected $name = 'start'; protected $description = 'Start command'; public function execute() { $message = $this->getMessage(); $chat_id = $message->getChat()->getId(); $text = 'Hello! I'm your new bot. How can I help you?'; $data = [ 'chat_id' => $chat_id, 'text' => $text, ]; return Request::sendMessage($data); } }
This sets up a webhook (so Telegram knows where to send updates), handles incoming commands, and creates a basic /start command. Cool, huh?
Want to level up? Try handling user input, sending media, or creating custom keyboards:
// Sending a photo Request::sendPhoto([ 'chat_id' => $chat_id, 'photo' => 'path/to/photo.jpg', 'caption' => 'Check out this awesome photo!', ]); // Creating an inline keyboard $inline_keyboard = [ [ ['text' => 'Button 1', 'callback_data' => 'btn1'], ['text' => 'Button 2', 'callback_data' => 'btn2'], ], ]; $data = [ 'chat_id' => $chat_id, 'text' => 'Choose an option:', 'reply_markup' => new InlineKeyboard($inline_keyboard), ]; Request::sendMessage($data);
The possibilities are endless! Get creative and see what you can come up with.
Don't forget to add some error handling and logging. Your future self will thank you:
try { $bot->handle(); } catch (Exception $e) { // Log the error error_log($e->getMessage()); }
Time to see your creation come to life! Test locally first, then deploy to a server. Remember to update your webhook URL when you go live.
A few quick tips to keep your bot running smoothly:
And there you have it! You've just built a Telegram bot using PHP. Pretty cool, right? Remember, this is just the beginning. The Telegram Bot API has tons of features to explore, so keep experimenting and building awesome stuff!
Want to learn more? Check out the official Telegram Bot API docs and the php-telegram-bot GitHub repo.
Now go forth and create some amazing bots! 🚀