We use cookies
We use cookies to optimize our website. By continuing to browse the site, you agree to our use of cookies.
New design
Control panel
  • Русский
  • Українська
  • English
  • UAH
  • USD
  • RUB
  • EUR
  • 0-800-307-307 Hotline
  • +38 (044) 392-74-33 Kiev
  • +38 (057) 728-39-00 Kharkiv
  • +38 (056) 794-38-31 Dnipro
  • +38 (032) 229-58-93 Lviv
  • +38 (048) 738-57-70 Odessa
  • +38(093) 170-15-42  Life
  • +38 (067) 400-88-44 Kievstar
  • +1(888)393-24-51  USA, Toll free
  • +44(131)507-01-14  Great Britain
  • +7 (499) 348-28-61 Moscow

2.27.1. Telegram bot

To work with a Telegram bot, it is recommended to use the information update method Webhook... This method is preferable to Long Polling, due to several factors:

  • A script launched with Webhook is slightly faster than one that updates information at a specific interval.
  • Within the framework of virtual and business hosting services, there is a time limit for the execution of the process, and therefore the script cannot be constantly running and will be automatically terminated by the system. This limitation cannot be bypassed, therefore, the use of a constantly running script can lead to data loss if the script terminates during the processing of an event.

To create a bot, follow these steps:

  1. Write to Telegram bot @BotFather message /newbotto initialize the creation of a new bot.
  2. The bot will ask you to specify the name of the new bot - you just need to send a message, which will be accepted as the name of the bot.
  3. The next step is to specify the name of the bot. The name differs from the name in that it will be a unique identifier with which you can use invitations, create links to it, etc. The word must be indicated at the end of the name bot... For example, examplebot or example_bot.
  4. The new message will indicate the current that will need to be used for the bot to work. Save this token.

There are several ways to install Webhook:

To install Webhook in a browser, just go to a specific URLthat you need to change beforehand.

Substitute your values for the following parameters:

  • token - specify the token received when creating the bot. Note, change before token word bot not necessary.
  • domain.com - correctly specify the domain name that will work in the future. It is not recommended to specify an address with www or without www if a redirect to the return address is set.
  • path/to/file.php - indicate correct URL-address of the file itself that will process bot requests.

After opening such an address in the browser, a message should appear on the open page like “Webhook was set". If such a message appears, then the Webhook is installed correctly, otherwise double-check the used URL-address and SSL certificate operation.

To install Webhook in the script, it is enough at its beginning, after declaring variables with the data of the token and the bot name, specify the following code:

if(!json_decode(file_get_contents("https://api.telegram.org/name:token/setWebhook?url=https://domain.com/path/to/file.php"))->ok){die('webhook is not set');}

For an example of a simple bot, there are many implementation options. But this article will consider two of the simplest.

The simplest bot that will respond to a message /startlooks something like this:
define('token','XXXXXXXXXXXXXXXXXXXXXXXXX'); // вместо ''XXXXXXXXXXXXXXXXXXXXXXXXX'' specify your token
$result = json_decode(file_get_contents('php://input'), true); // передаём в переменную $result full information about the user's message
if ($result['message']['text'] == '/start') {
    file_get_contents("https://api.telegram.org/bot" . token . "/sendMessage?chat_id=" . $result['message']['chat']['id'] . "&text=" . urlencode('Hi')); // отправляем ответ пользователю, используя его уникальный идентификатор $result['message']['chat']['id'] as recipient
An example of the simplest bot using the library Telegram Bot SDK looks something like this:
include('vendor/autoload.php'); // подключаем библиотеку
use Telegram\Bot\Api; 
$telegram = new Api('XXXXXXXXXXXXXXXXXXXXXXXXX'); // вместо ''XXXXXXXXXXXXXXXXXXXXXXXXX'' specify your token
$result = $telegram -> getWebhookUpdates(); // передаём в переменную $result full information about the user's message
$text = $result["message"]["text"]; // текст сообщения
$chat_id = $result["message"]["chat"]["id"]; // уникальный идентификатор пользователя
$name = $result["message"]["from"]["username"]; // имя пользователя
if ($text) {
    if ($text == "/start") {
        $reply = "Hi!";
        $telegram->sendMessage(['chat_id' => $chat_id, 'text' => $reply]);