PHP

Making A Simple Telegram Bot In PHP

telegram bot

The telegram app has this amazing feature called bots. Where you can extend telegram app with custom third-party features. For this, you need to access the Telegram Bot API and it has a pretty good documentation. So today in this article lets just have a look at how the telegram bot API works and how we can use it to create a simple bot in PHP.

I am going to break this down into steps and believe me making a bot is extremely easy if you follow all steps in this guide.

What we will learn

To create a simple PHP bot that smartly replies to your messages.

Pre Requisites To Creating Telegram Bot

A Telegram account, a website with SSL. Basic understanding of PHP and enough patience to read this full article.

1. Contacting the BotFather

telegram bot

 

The first thing you need to do is to contact the BotFather, The BotFather is the father or all bots on Telegram. You can use it to create a bot, change the various configuration of your bot and a lot more.

Search for botfather on telegram and you shall find him. He looks very much like GodFather from the movie, yes.

Just click start and the botfather will send you a long message about it can do with all of the commands etc. It’s too much to be read right now so Let’s focus on the important thing. So firstly we need to register out bot and to do that just send a  /newbot to botfather. It will then ask you a few more questions about the name of the bot and the bot’s username. Fill them out and your conversation with BotFather should be similar to this.

telegram bot

 

You can see that our Reverb Bot has been created and we have received an API key, this key will be used to access the telegram API.

2. Testing the bot API.

Just to test the bot API we will type in a URL endpoint and check the response we get. So type in the following URL in your web browser.

https://api.telegram.org/bot<your_bot_token>/getme

This should give you a JSON response like this.

{  

  "ok":true,

  "result":{  

     "id":612385587,

     "is_bot":true,

     "first_name":"Reverb",

     "username":"the_reverb_bot"

  }

}

We have received our bots information so we know it is working perfectly fine.

2 – Setting a WebHook.

We need to set a webhook now. A webhook is basically a URL that will automatically receive any new message that is sent to your bot. It is also the place where we will be writing our code.

So I wish to set a webhook on this URL called.https://rivilabs.com/the_reverb_bot.php

It is pretty simple. All you gotta do is visit a telegram API endpoint

It is important to note that you need an https-based server to set the webhook because telegram does not allow insecure webhooks.

https://api.telegram.org/bot<your_bot_token>/setwebhook?url=https://rivilabs.com/the_reverb_bot.php

This will set a webhook on the_reverb_bot.php file. Whenever our bot receives a message, the script will run.

3 – The Update Object.

Since we have already set the webhook, our PHP script will be notified whenever someone sends a message. This notification will be in the form of a JSON object called the update.

This object contains a lot of nested data elements. Here is an image from the official telegram API defining each major element of the bot.

telegram bot

 

The object has too much data but we will just be focusing on the data that we need as of now.

4. The PHP Code

So we have an API token, we have set a webhook to get the update, we know what data we will receive. It’s time to write some code.

Look at the following simple example.

define ('url',"https://api.telegram.org/bot<your_bot_code/");

$update = json_decode(file_get_contents('php://input') ,true);

$chat_id = $update['message']['chat']['id'];

$name = $update['message']['from']['first_name'];

$message = 'Hi '.$name;

file_get_contents(url."sendmessage?text=".$received_message."&chat_id=".$chat_id.");

This is a very small code that works as a very functional bot. I have first defined the bot URL on the first line, then since the update object is going to be JSON formatted, I have converted that to a PHP multidimensional array.

After that, you can see that we have extracted the name and chat_id from that array. Next, we format a message that simply says Hi and adds the name of the person to that message. Lastly, we will hit a request to the send message endpoint and pass our message and the chat id. This will send a message to the person who had messaged the bot.

telegram bot

 

Conclusion

So that was it. A pretty basic and simple guide on setting up your first telegram bot. You definitely should read the full API to get an idea of all the different things that you can do with bots in Telegram. You can read that here.

If you face any problems regarding this, just let me know in the comment section and I’ll be there for you.

Comments
To Top