Telegram bots make your life easier

Telegram Logo

Calling people is out. Messaging them is in. WhatsApp started a trend and now there are lots of Messangers out there – the best one, I believe, is Telegram (yes, Threema is also great but there are still some features missing).

Telegram has this cool feature calles “Bots”. Bots allow you to automate stuff and do, well, cool things.
Now I have always been working with PHP so why not build my Telegram Bot in PHP too? The good news: Telegram has an API for this. The bad news: There is not library – sorry, there was no library for PHP. Now there is: Find my PHP Telegram Library on GitHub.

But this library is probably not really useful to you, since it doesn’t really tell you how to use it, right? So let me show you here.

Ask the BotFather

First off we will create a new Bot using the Bot of all Bots: The BotFather. Open Telegram, find the BotFather (or simply click the name here) and create a new Bot.

Chat with the BotFather

Get your server connected

After you have downloaded the Bot Library and uploaded to a place which is reachable from the internet, you should open the index.php and change line 28:

Type the URL of where you uploaded the Library to:
echo $bot->setWebhook('');

And then you will also need to change line 3 and insert the API Token, the BotFather gave to you:
define('TOKEN', '000000000:ABCDEFGHIJKLMNOP');

Now upload your index.php if you have not edited this directly on the server (what I do not recommend). Then go to the URL you uploaded the Library by opening it in your browser.

Now you should see a blank page.

If you append ?setHook=true to the URL, the Bot will register itself and you should receive following message:
{"ok":true,"result":true,"description":"Webhook was set"}

Interact with the world

Now we want our bot to communicate with the outside, don’t we? So let’s have him do that.

In the index.php you have probably noticed the part with
$request = json_decode(file_get_contents('php://input'), true);
if ($request['update_id']) {
// ...

You guest it! This is exactly where the fun happens. Whenever your bot receives a message index.php is called. Whenever index.php is called it checks if it has received an update_id in the input. Pretty straight forward.

For practise, let’s reply to the message we have just received by adding following line, where the comment BOT CODE HERE is:
$bot->sendMessage($request['message']['chat']['id'], 'Hello World!');

You have did it! But let me tell you what you just did ;).

$bot is an object of the Telegram API Library a.k.a. PHP Telegram class. It has a method called sendMessage which – well – sends a message via Telegram. The method needs the ID of the chat you want to send the message to, the content and some optional parameters, but more to them later.
Where do we get the ID of the chat from? Exactly – from the update message we have received. In the Telegram API Documentation we can see that the update contains the message we have received and the message the chat – yes, right! – the message has the ID we are looking for :).

A few words to the library

Why do we access the $request like an array – it’s not very elegant, right? – but it works and it is easy. The PHP Library is supposed to communicate with the Telegram API. This is why the Library basically consists out of the API Methods.

This method is almost everything you need – but Telegram also has another awesome feature, other call it annoying feature. Stickers.

$bot->sendSticker($request['message']['chat']['id'], 'BQADAgADawEAAvR7GQABuAEiUvxH-TUC');

The sendSticker method works exactly the same as the send message only that instead of the text, you have to specify a sticker ID, you want to send. It is sort of hard to find a sticker ID, the way you might want to go is to write a bot which returns the sticker ID of the sticker you just sent to him. That’s exactly what I did for you: Chat with the bot (just send him the Sticker and he will reply with the ID) or look at the source code!

You have probably been wondering, what the other parameters in the sendMessage method are. The third parameter takes a message id of the message the bot should reply to. The third parameter is a bit more interesting: You can specify a keyboard. The keyboard is a simple array with the structure like in the API Docs.

$keyboard = array(
'keyboard' => [
// Buttons...
'one_time_keyboard' => true,
'selective' => true

This is already everything the Telegram class can do. The good new is though, that there is a session Class, so we can save data for a chat as if it was a session! But more on that next week…

Open to all

This super simple PHP Library is open source and you may use it and improve it. If you are like me, you don’t think that this library is finish despite the fact that by far not all methods have been implemented. The way the keyboards are being implemented is also not very nice… But if you make any improvements on the library, I am more than happy to list your contribution and merge your Pull Request on GitHub!

View Repository

Telegram Image Source: The Logo is directly from the Telegram website.

Comments are closed.