Telegram Messenger and the Bot API


Telegram is basically a messenger similar to Whatsapp or Threema , free and easy to use. I don't want to say that it is better or worse than other messengers, but I like the Telegram Bot API so much that I want to write a little tutorial here.

Table of contents:

Using Telegram Messenger and the Bot API

kraken l.jpg

But first, some marginal information. Telegram is completely free, and you can use the API without any costs. The Telegram Messenger encrypts all data according to the manufacturer's specifications. A combination of 256-bit symmetric AES encryption, 2048-bit RSA encryption and the secure Diffie-Hellman key exchange method is used. The app is available for all common operating systems: Windows, Mac, Linux, iOS (iPhone, iPad), Android and WindowsPhone. The Messenger can also be operated platform independent via a web browser.

The really nice thing about Telegram is the easy to use Telegram Bot API. So as a developer you are able to automate news, status updates or other information quickly and easily via Telegram directly to your smartphone smartphone or other users. The API is based on the HTTP protocol and can therefore be connected to any common platform and programming language.

I use multiple bots. Simple example: One of the bots sends me a notification if one of the servers is down. For this the servers "talk" constantly (or every 30 minutes) also via simple HTTP requests. If one server doesn't answer, the other one can send me a message and I can see what's going on. There are countless application examples, there are no limits to creativity! 😉

Step 1 - Setting up the Telegram Bot

First you should install the Telegram Messenger ( on your Smartphone.

Telegram Bot FatherIn the chat overview, tap on the "New Message" icon in the upper right corner and enter "Botfather" in the search field.

This is, as the name suggests, the telegram bot for the bots.

You simply write /start or /help to it and get a list of commands to create and configure your own bot.

Now enter /newbot as command and answer a few basic questions, e.g. what your bot should be called, i.e. which username it will be reachable with later.

Then you get an API access token, which is needed later for sending messages and all other functions. This must always be passed in the requests to the telegram server.

Step 2 - Setting up the recipient on the user side

Now we ask for the ID (unique number of your user account / USER, NOT of the bot!) of the user to whom we want to send messages later. This number is the recipient address of the messages.

For this we simply write /start to the user "userinfobot" from the Telegram app. The answer you get is his username, ID, and maybe the fore- and lastname (if given ;-)). The ID is important, so we write it down for later.

To make sure that your own bot can write to you later, just send /start to your new bot, as you did with the Botfather. (see step 1)

Step 3 - A small PHP script to send messages to ourselves

Now we build a small PHP script which sends a short message when called. A short look into the documentation of the Telegram-Bot-API and it becomes clear what the HTTP-Request for sending a message has to be. I have defined a constant for this. This looks like this in PHP:

define("TELEGRAM_URL", "<TOKEN>/sendMessage?chat_id=<ID>&text=<TEXT>");

In pointed brackets I have inserted 3 placeholders which are needed. The <Token> (your access token you got from the botfather, see step 1), the <ID> (the ID of the recipient, see step 2) and the <Text>. These are now filled with your values, either directly in the define statement, or a little more flexibly for later use simply via str_replace statement.

$request = TELEGRAM_URL;
$request = str_replace("<TOKEN>", "hier token eingeben", $request);
$request = str_replace("<ID>", "hier id des empfängers", $request);
$request = str_replace("<TEXT>", urlencode("Message here! Hello World!"), $request);

Now the variable $request contains the correct URL to send the test message to yourself. For testing you can easily send the message to the telegram server via file_get_contents. If necessary, set allow_url_fopen to true in the PHP configuration. - * not necessarily recommended for productive use *

$response = @file_get_contents($request);
$positive_answer = "{\"ok\":true";
$telegram_sent = (substr($response, 0, strlen($positive_answer)) == $positive_answer);

After file_get_contents we check whether the message was sent correctly by evaluating the return value, i.e. the response of the Telegram server. If the answer is "Ok:true", then the variable $telegram_sent is set to true. If an error has occurred, this is indicated by the content of the variable $response and can be evaluated further.

kraken.jpgReady! If you now load the script onto your server and call it out, it sends the desired message to the respective Telegram user. Now you have a simple basis, which you can extend by functionality.

If you want to talk to your bot, or if you want it to react to requests, you should have a look at the function setWebHook in the documentation. With this hook you can get the telegram servers to send a notification to any script on your server when a user writes to the bot.

What you make of it, everyone has to know for himself. How to answer the user you know now after this little tutorial... hope you like this. 😉 *happy coding*

Update: The continuation of this article on how to set up setWebhook and respond to messages from the user can be found here: Telegram Messenger and the Bot API - Part 2

Author: Sascha from

Author: Sascha

Some words about myself. My name is Sascha - i'm a software developer, trader and martial artist from germany. Besides programming, trading and martial arts some of my interests and hobbies are cooking, fitness and hearing loud heavy metal music. :D

This might also be interesting for you

Program your own Screen Recorder / Capture Software

While searching for an easy to use screen recorder tool, I stumbled across all kinds of free and paid software solutions. From my point of view, pretty much all of them were either completely overloaded, difficult or complicated to use. Others were just expensive in relation to the functionality. My conclusion: Program yourself! Table of … Continue reading "Program your own Screen Recorder / Capture Software"

Privacy Browser for more security and privacy

More data protection, security and above all more privacy on the Internet is demanded from all sides, and not only after the entry into force of the EU-wide data protection basic regulation (). In general one can understand this wish, because who would like to voluntarily allow large IT companies to pass on their own … Continue reading "Privacy Browser for more security and privacy"

Telegram Messenger and the Bot API - Part 2

In the first part - Telegram Messenger and the Bot API - I explained some basics how to set up a telegram bot and how to use it to send messages, states, informations and more to your . Since this tool has proven to be very practical, I decided to take up the previous article … Continue reading "Telegram Messenger and the Bot API - Part 2"

Trading-Journal: Crypto Trading Bot for Kraken

Every halfway professional trader should of course keep a trading journal to record his trades and to be able to evaluate them later. Some good trading software offers already integrated solutions, but that didn't stop me from programming my own software for our Project and connecting it to our trading system. In addition to … Continue reading "Trading-Journal: Crypto Trading Bot for Kraken"

Training equipment: Makiwara 2.0

A Makiwara is a piece of sports equipment originating from Japan, which in karate is known mainly as a wooden hitting post. In the past (and partly still today) a makiwara is made of a flexible and non-splintering wooden board. One end of the board is driven vertically into the ground, and the other end … Continue reading "Training equipment: Makiwara 2.0"

Leave a Reply

Your email address will not be published. Required fields are marked *

I have read and accepted the privacy policy!