Sooner or later, in the life of the developer of telegram bots, a clear realization comes that
everything is perishable, you need to create more or less full menus for bots.
Because:
A) it is convenient
B) Convenient for customization *
')
Nobody, of course, canceled the "/ team", but still
The essence of the task
At first glance, the task is simple, but no matter how. The whole
problem is in the
API itself
, namely in the Update object .
A little more detail: Using the getUpdate method - you get the Update object, it’s logical! So, this object contains a lot of useful things, including information about the message, but only about the latter. That's the catch.
Proceeding from the above, the most pleasant conclusion follows: processing requests for a webhuk, without storing additional data, does not shine for us
Well, wash and get down to work.
Writing Learning Code
In his practice, especially for this task, a small class was developed.
NEW! Now on
githab )
If you can not wait, you can immediately go to the repository, there is a digression.
Continue
The class creates a
query requestA little about the structure: Go through the menu by creating a request for a request . You create it in the right place, and the next time the user accesses, bam, and he is in the "menu".
I agree, the concept, after that, did not become clear ... I will try to clarify. To understand, you need to go to a certain level of abstraction, that is, imagine that the whole structure of the bot consists of different menus between which the user switches (you can draw an analogy with the android activation). When a bot is activated, the user is in the “initial menu”, from which he can go, say, to the “Settings”, “Information” menu, and so on.

And so, the structure of the bot changes a little, if briefly:
First we check if the request exists:
YES) Run the "handler" requests
NO) We remain in the "initial menu", we verify the user's answer with the available commands.
* This is a very simplified version, because of this it knocks down the table. With examples it will become easier.
Continue ..?
Let's start with the installation. To install, use the
compser- th (create “composer.json”, then copy the lines below into it, read more in off-docks)
{ "require" : { "s0d3s/tg-long-req": ">=1" } }
and connect
include_once('path/to/autoload.php'); use TgLongReq\TgLongReq;
OR we connect manually (download to the folder with the project)
include_once('TgLongReq.php'); use TgLongReq\TgLongReq;
CREATE OBJECT function SOMTH($tg_res, $long_req_obj, $tab_key){ echo "EveryForEveryone"; return; } $BOT_FUNC_ASSO_TAB = arrray("ECHO_SOMETHING" => "SOMTH");
CREATE A REQUEST, CHECK HIS AVAILABILITY AND START A TREATMENT $tgreq -> ReqCreate('SOMTH');
HANDLING AND GETTING TIME DATAsave:
$tgreq -> SaveToTemp('HiHabr');
restore:
$tgreq -> GetFromTemp(false);
RESULT FUNCTIONIt should be noted that each [except for the GetFromTemp (), GetError (), ReqCheck () and constructor] functions, returns an array:
ERRORSEach function [with the exception of GetFromTemp (), GetError (), ReqCheck () and the constructor], set an error in case of failure, which can be checked with:
$error_arr = $tgreq -> GetError();
All gathered in the way!
We assume that you are minimally familiar with writing telegram bots, if not -%
article %, read up to the item "
Writing a bot code " - the first link in google by sabzh, you can search for yourself, the main thing is to find out how to create a bot in tg itself, and configure him to your server. If it is interesting, I will write an article on how to set up a webhost for working with a local server (~ NEW! Solution to bypass the “problem” with https).
Let's bring this example to life. We divide the space into two files “botmain.php” (main menu and processing) and “varfunc.php” (request processing functions). For ease of example, we will use
telegram-bot-sdk (a well-made and well-designed project, convenient for small projects, thanks to the creator;).
In general,
listing :
composer.json { "require" : { "irazasyed/telegram-bot-sdk": ">=3", "s0d3s/tg-long-req": ">=1", "php":">=7.1" } }
Next in the project folder, open the console and ...
>composer install ...
UPD : In the framework of Habr, the source code becomes unreadable, without global editing. Because of this, if you are interested in examples, I suggest you go to the githab.
Original |
Simplified version .
Conclusion
In the end I can only say that this is not the only possible solution to this problem, but from the point of view of optimality for ME it is a favorite.