Telegram bot in PHP: from scratch to the first greeting
\n\nWhen it comes to creating bots for Telegram, the first thing that comes to mind is Python. And that makes sense: there is an elegant library python-telegram-bot, tons of tutorials, and a huge community. But what if your main stack is PHP? Or you want to quickly put together a simple bot for notifications without deploying a separate server on Python?
The good news: Telegram Bot API is not tied to a language. It is a regular HTTP interface, and PHP handles it just as well. In this article, we will break down how to create a Telegram bot in PHP from scratch: from registration with BotFather to a full-fledged bot that responds to commands, sends messages, and can handle callback queries. All with real code examples, no fluff.
\n\nWhy PHP for a Telegram bot is perfectly fine?
\n\nMany developers are skeptical about PHP in the context of bots. They say it's a language for "quick and dirty websites," not for real API work. In reality, PHP handles the role of a server-side handler perfectly: it is simple, predictable, and works on any hosting. For a bot that doesn't require super performance (and most bots are like that), PHP is an ideal choice.
\n\nPlus, if you already have a website on PHP, you can host the bot on the same hosting. No additional workers, daemons, or complex configurations. Just a bot.php file that sits on a webhook and processes requests from Telegram.
What you need to get started
\n\n- \n
- Hosting with PHP 7.4+ (any hosting will do, even the cheapest shared hosting). \n
- HTTPS access — Telegram requires the webhook to work over a secure protocol. You can use Let's Encrypt. \n
- A Telegram account — obvious, but you can't do without it.
- Basic knowledge of PHP — enough to understand how functions, arrays, and cURL work.
If you have all of this — let's go.
\n\nStep 1. Registering the bot with BotFather
\n\nStart a dialog in Telegram with @BotFather — this is the main "bot breeder". Send the command:
\n\n/newbot\n\nYou will be asked to enter the bot's name (it will be displayed in contacts) and a username (must end with bot). After that, you will receive a token — a long string like:
1234567890:ABCdefGhIJKlmNoPQRsTUVwXYZ\n\nThis is the access key to your bot. Do not show it to anyone — anyone who knows the token can control the bot.
\n\nI also recommend immediately setting an avatar and description using the commands /setuserpic and /setdescription. This is not required, but it makes the bot more friendly.
Step 2. Choosing a method for receiving updates
\n\nTelegram Bot API offers two options:
\n\n- \n
- Polling (long polling) — you constantly ping the Telegram server asking "Any new messages?". It works, but requires the script to run continuously (e.g., via cron). \n
- Webhook — Telegram itself sends a POST request to your server when a new message arrives. This is more convenient and faster.
We will use webhook. First, let's check that your server is accessible from the internet via HTTPS. If you don't have an SSL certificate, use Cloudflare (they have a free SSL) or buy a certificate for pennies.
\n\nSetting up the webhook
\n\nSend a GET request to the URL (you can simply open it in a browser):
\n\nhttps://api.telegram.org/bot<TOKEN>/setWebhook?url=https://your_site/bot.php\n\nReplace <TOKEN> with your actual token, and https://your_site/bot.php with the real path to your script. In response, you should receive JSON:
{"ok": true, "result": true, "description": "Webhook was set"}