On this topic, many articles are written on
Habré and just on the Internet. And I will tell you about my experience of working with a telegram bot and moments that I couldn’t solve on the forehead.
Create bot
The first thing to start with is creating a bot. There is
official documentation and steps are detailed there.
If the described instructions do not understand anything, then here is a step by step instruction.- find BotFather in telegram bot and add a contact list to yourself
- see available bot commands with / help

- choose / newbot and further, following the instructions, perform the necessary actions (the following image is taken from google)
')

After creating the bot, you will receive a token, which you will need to use in the code of your bot, so the message with the token must be saved.
We associate the bot with the application \ website
The most interesting begins, and also here I faced the first problem.
First of all, choose a library in php to create a bot. I stopped my choice on
this library , since it seemed to me the most convenient.
After connecting the library, you need to organize the interaction of the bot with your site \ application. You can organize this interaction using
webhukov .
A webhook is a kind of repeater that will send all requests from the bot to the address specified when registering the webhook. It is very simple to register a webhuk, you just need to send a request like
https: //api.telegram.org/bot~token~/setWebhook?url=https: //example.com/path , where
https: //example.ru/ - this is a link to your website where the bot will be redirected to.
~ token ~ is the token you received when registering your bot.
The path is the part of the url that calls will be sent to.
And this is where the problem arises. It turns out that the webbook can only be registered if the site is on https. If your site is on http, then you will not be able to register a webhost.
But there is a solution if you do not want to buy a certificateYou can use the service
Let's EncryptGo to the section
getting startted and follow the instructions.
Writing bot code
Now we start programming. Once the relationship is organized, you can begin to write the logic of our bot.
The telegram developers, in order to make it easier for users to work with bots, ask all developers to implement support for the following commands:
/ start - starts communication with the user (for example, sends a greeting message). You can also pass additional arguments to this command.
/ help - displays a message using the command. It can be a short message about your bot and a list of available commands.
All we need to do is to write the following code in the controller of your application \ site:
$token = ""; $bot = new \TelegramBot\Api\Client($token);
Now if in the window of the telegrams of the bot write / help, the text will be displayed:
Commands:
/ help - help outputWell, we wrote a list of recommended commands. Next, we can implement the commands we need.
Since teams can take arguments, we use this opportunity. For example, we will make our bot, Vasya's hello command, respond with the message: Hi Vasya.
To do this, write the following code:
$bot->command('hello', function ($message) use ($bot) { $text = $message->getText(); $param = str_replace('/hello ', '', $text); $answer = ' '; if (!empty($param)) { $answer = ', ' . $param; } $bot->sendMessage($message->getChat()->getId(), $answer); });
Everything turns out very simple and fast.
List the bot commands
To enable the bot to issue online help

BotFather bot needs to report a list of commands.
You can do this with his / setcommands command.
Bike
The above option is not very convenient, because I do not want to drive the text after the command, because if you use the tips from the bot, it is completely unrealistic.
So you need to make sure that the bot remembers the command that you enter. This can be done using any storage (MySQL, memcached, redis, tarantool, Postgres, etc)
All you need is to remember the previous user input.
To do this, before sending the message to the user, put it in your repository, and before accepting the message - check if there is data in the repository. And if there is, then on the basis of these data to build further logic.
It was at this stage that I again had a difficulty, since I did not find an opportunity in the library to receive a command before calling the command method. Also, I didn’t like the fact that all the logic would be locked in one application controller.
It was decided to write its own data handler, with the ability to move each command to a separate application controller.
First, we describe the entry point to the controller.
function main() { $telegram = new Telegram();
Now consider one of the methods.
function getnewtext(Message $telegram, $text) {
So it all became more pleasant, more interactive and more convenient.

Thank you for reading the article to the end. You can play with a live bot that works in a dialogue mode
here .
UPD: bot added the ability to give photos to some types of requests. For example on
/ gettable - returns the resulting table of sports events
/ getevents - returns sport events