Hello! All developers once encounter a desire to provide something for themselves, and even the material value of a product becomes unimportant, the main thing is that it works on its own. Kayf.
So to me, by the lonely May holiday, I wanted to combine the knowledge of coding and any other, vital, in one project. It turned out, in my opinion, an interesting service that I want to give life and tell about it.
I am not a nutritionist, not a professional athlete and, of course, I do not pretend to be trusted without question. But, one day, I was very interested in the topic of proper nutrition, and I began to study articles, communicate with cool coaches in the halls that I attended, and noticed such a thing: people spend a fair amount of money for drawing up nutrition plans.
')
And I decided: and I will create a system that will automatically calculate people products in grams, depending on their goals and parameters.
Since I am a backend developer, I rarely come across the front, thought it over and decided to make a bot. The choice of the messenger was influenced by several factors: demand and availability of payment systems. She came social network Vkontakte.
For calculations, I used the Harris-Benedict formula. Took into account such input parameters (which the bot user enters):
- Weight
- Growth
- Goal (lose weight / gain weight / maintain)
- Physical activity (all information available and standard for calculations using the Harris-Benedict formula)
- Lactose tolerance
- Floor
Created a product base in which the product had fields: Proteins, Fats, Carbohydrates, Kcal, GI (glycemic index), and also lactose content. In general, I want to give a listing and comment on the course.
Vkontakte has an accessible and understandable
APIUsed PHP language.
First, I identified several necessary functions and constants:
header('Content-Type: text/html; charset=utf-8'); $request = file_get_contents("php://input"); $input = json_decode($request, true); define('VK_API_VERSION', '5.95');
In the settings of the bot, you specify a link to your script (webhook). By the way, in the VK API you can use two methods to receive messages. I used a webcam. It seems to me that it is more convenient, more familiar.
Further I process the received json.
$test_type=$input['type']; $test_group_id=$input['group_id']; $user_id=$input['object']['user_id']; $text=$input['object']['body']; $text_body=explode(" ", $text); $vk_pay=$text_body[0]; $caption=$input['object']['attachments'][0]['link']['caption']; $url=$input['object']['attachments'][0]['link']['url']; $str3=$input['object']['attachments'][0]['link']['title']; $str2 = explode(" ", $str3); $money =(int)$str2[0]; $money=strstr($str3, ' ', true); $money=(int)$money;
Since VK has payment systems, I provided payment via VK pay (VK servers send such requests to your webhost, but only if you have the public listed as a SHOP and have a GOODS that can be paid via VK pay), as well as a simple cash payment translation, which can be attached to the message.
Each user who writes something to the bot, I save in the database, and then all communication with the bot takes place depending on the payment. If there was no payment, the bot will say that you must first pay, if payment has occurred, it will tell you what to do next. For testing, you can enter any message to the bot to make a plan.
After payment, the bot will ask for the parameters required for the calculation, save it all in the database for a specific user, and after all the information received, I consider and issue a response. I send a message using the created function, specifying the user_id of the user:
vkApi_messagesSend($user_id, '! ?');
Also took into account the moment of incorrect input by the user of some information: in this case, you need to send the bot the number 404, and he will start asking first, resetting the data in the database.
For some messages, it was convenient for me to make a button, when clicked, a message with the text that was indicated on the button came to me on the webbook.
$buttons='{ "one_time":true, "buttons":[[{ "action":{ "type":"text", "payload":"{\"button\": \"1\"}", "label":" " }, "color":"primary" }, { "action":{ "type":"text", "payload":"{\"button\": \"2\"}", "label":" " }, "color":"primary"}]]}'; vkApi_buttonSend($user_id,' ',$buttons) ;
And then all the fun! After the collected data is the calculation itself. I do not invent anything myself, I only correctly formulate the necessary formulas, relying on the Harris-Benedict formula, as well as knowledge in the field of proper nutrition (the quantities of meals, the percentage distribution of proteins / fats / carbohydrates for these techniques, etc.) are important here.
In general, such things:
$BMR=88.363+(13.397*$weight)+(4.799*$height)-(5.677*$age); $BMR_AMR=$BMR*1.2-500; $b_kkal=0.35*$BMR_AMR; $b_gr=$b_kkal/4; $g_kkal=0.3*$BMR_AMR; $g_gr=$g_kkal/9; $u_kkal=0.35*$BMR_AMR; $u_gr=$u_kkal/4; $break_b=0.15*$b_gr; $break_u=0.25*$u_gr; $break_g=0.25*$g_gr; $break_kk=0.25*$BMR_AMR; $snack_b=0.1*$b_gr; $snack_u=0.1*$u_gr; $snack_g=0.15*$g_gr; $snack_kk=1*$BMR_AMR; $snack2_b=0.05*$b_gr; $snack2_u=0.05*$u_gr; $snack2_kk=0.05*$BMR_AMR; $lunch_b=0.45*$b_gr; $lunch_u=0.4*$u_gr; $lunch_g=0.4*$g_gr; $lunch_kk=0.4*$BMR_AMR; $dinner_b=0.25*$b_gr; $dinner_u=0.2*$u_gr; $dinner_g=0.2*$g_gr; $dinner_kk=0.2*$BMR_AMR;
Accordingly, for people with a sedentary lifestyle and people with hyperactivity there will be other factors, I gave a small example.
After the calculated BZHU and KKAL I collect a set of products. By the way, if after the received nutrition plan you enter the number 404 and start anew (you may lose weight / gain weight, lifestyle changes, etc.), the set of products will change randomly. And now you have a different nutrition plan!
The result was such a service (I officially posted it to VC for testing, I’m a beginner developer, and I myself can’t catch all my bugs, if they are there):
From the errors I noticed (needs some work):
- Buttons do not open immediately. You need to find the keyboard icon near the "send" button. This is very confusing to people, and they write incorrectly (if the button is pressed, then I process the text from the button, the data that the user enters himself is ignored).
- Not a very large base of products, no one will be faced with the fact that such a thing is.
For those who want to test and see the link to the
public .