How to Build a Telegram Bot Using PHP

Telegram is a cloud-based instant messaging and voice over IP service. Telegram client apps are available for Android, iOS, Windows Phone, Windows, macOS, and Linux. Users can send messages and exchange photos, videos, stickers, audio, and files of any type.

Telegram has all of the features you would expect from a modern chat platform, including Chatbots: software-based agents that you can program to read and respond to other users’ messages.

What are bots?
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to our Bot API.

In this guide, I will guide you step by step on how to create your own Telegram bot with callback using PHP.

Step One: Setup Your Bot’s Telegram Profile

The first step in creating a Telegram bot is setting up the profile that the bot will ultimately stand behind. This is also how you get a token for the Telegram API.

To set up another bot profile –
a. Sign in to your Telegram account.
b. Search for a bot called “BotFather”.
c. Start a new conversation with Botfather (@BotFather).
d. Type and send “/newbot” command.
e. The bot will respond like this “Alright, a new bot. How are we going to call it? Please choose a name for your bot.“. This is the time you need to enter your Bot name. This name will be used as a display name of your bot. Like in the example, we have chosen “ViralStations” as the display name of our bot.
f. Now its time to create a username for your bot. You can do this two way whichever suits your need. Like this, for example: TetrisBot or tetris_bot.
In our example, we set the username as “ViralStationsBot”.

Steps to create Telegram bot using Botfather

Your bot is now created successfully. You will get congratulations message from Bot Father. For other settings (Like changing your bot’s name, setting bot profile photo, generate authorization token, and may other settings), you need to type respective commands. Type and send “/help” command. It will show you list of all possible commands.

List of commands to set/change your bot settings.

Step Two: Create a Webhook for Your Bot
The second step in making a Telegram bot is setting up the webhook that will speak with your bot. Webhooks are the manner by which APIs reveal to you that something has occurred and the server is ready to serve the request.
To setup Webhook you need to know two things –
a. Your bot API token – You will get this token after the successful creation of your bot with a congratulation message.
b. Webhook Url – Webhook URL should be always protected with SSL and hosted on your server. e.g. https://yourdomain.com/webhookurl.php. If webhook URL is not SSL enabled (with HTTPS prefix), Telegram won’t send the updates to your URL.

Now, in a web browser, open this URL with your correct details https://api.telegram.org/bot<yourbottoken>/setwebhook?url=https://yourdomain.com/webhookurl.php.

After opening this URL in the browser you will see a JSON response with a success message. Like this
{“ok”:true,”result”:true,”description”:”Webhook was set”}

This means your callback is set correctly and we are ready to move to the next step.

Step Three: Write the code for Your Bot

Now, we have all things set to start writing the code for bot to communicate with the created callback. 

Please note that you must have good PHP knowledge with Curl to write and understand code and move further from here.

First, we will store variables like this

We will use Curl with post method to send data to Telegram server.

This is a very basic way of receiving and sending response. You can modify your post data according to your need. Click here to get full list of all possible post parameters.

Step Four: Upload code on a server and do the testing
Now, this is the time when you are ready to upload file on the server. Upload file to your respective hosting and start using it.

Note:- Telegram keywords are case sensitive.