Creating a Telegram bot in PHP is a great way to automate tasks, provide customer support, send notifications, or build interactive services. Telegram offers a powerful and simple Bot API, and PHP makes it easy to handle web requests and responses.
In this step-by-step guide, you will learn how to create a Telegram bot using PHP, set up a webhook, and build a simple working example.
Step 1: Create a Telegram Bot with BotFather
Before writing any PHP code, you need to create your bot inside Telegram.
- Open Telegram and search for BotFather.
- Start a chat and send the command:
/newbot - Follow the instructions and choose a bot name and username.
- After creation, you will receive a Bot Token.
Save this token — you will need it to connect your PHP script to the Telegram Bot API.
Step 2: Set Up Your PHP Environment
To create a Telegram bot in PHP, you need:
- A web server (Apache or Nginx)
- PHP 7.4 or higher
- HTTPS domain (Telegram requires SSL for webhooks)
Upload a PHP file (for example, bot.php) to your server.
Step 3: Basic Telegram Bot PHP Script
Below is a simple example of a Telegram bot written in PHP:
<?php
$token = "YOUR_BOT_TOKEN";
$apiURL = "https://api.telegram.org/bot" . $token . "/";
$content = file_get_contents("php://input");
$update = json_decode($content, true);
if (isset($update["message"])) {
$chatId = $update["message"]["chat"]["id"];
$text = $update["message"]["text"];
$response = "You said: " . $text;
file_get_contents($apiURL . "sendMessage?chat_id=" . $chatId . "&text=" . urlencode($response));
}
?>
Replace YOUR_BOT_TOKEN with the token provided by BotFather.
Step 4: Set Up a Webhook
Telegram needs to know where to send updates. You can set a webhook using this URL in your browser:
https://api.telegram.org/botYOUR_BOT_TOKEN/setWebhook?url=https://yourdomain.com/bot.php
If successful, Telegram will send messages directly to your PHP script whenever someone interacts with your bot.
Step 5: Testing Your Telegram PHP Bot
Now open your bot in Telegram and send a message. If everything is configured correctly, the bot will reply with:
You said: your message
Using cURL Instead of file_get_contents
For better performance and error handling, it is recommended to use cURL in PHP:
<?php
function sendMessage($chatId, $message, $token) {
$url = "https://api.telegram.org/bot$token/sendMessage";
$data = [
"chat_id" => $chatId,
"text" => $message
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
}
?>
Best Practices for Telegram Bots in PHP
- Always validate incoming data.
- Use a framework for larger projects (Laravel, Symfony).
- Store user data in a database (MySQL, PostgreSQL).
- Use logging for debugging.
- Secure your webhook endpoint.
Conclusion
Now you know how to create a Telegram bot in PHP using the Telegram Bot API and webhooks. With just a few lines of PHP code, you can build automated systems, chat assistants, and business tools.
Telegram bots are scalable, fast, and easy to maintain. As your project grows, you can expand your bot with inline keyboards, commands, databases, and third-party integrations.
Start building your PHP Telegram bot today and automate your workflow efficiently.
Source: AiBlockLab.com: How to Create a Telegram Bot in PHP – Step-by-Step Guide
