Sia Sia - 2 months ago 16
PHP Question

I want to make a telegram bot with php keyboards button not work properly

i want to make a telegram bot with php that ask some question one by one (keyboard buttons) and store details in object and finally answer something is exists or not!

i wrote this code

<?php
require_once ("iphone_class.php");

$message = file_get_contents("php://input");
$result = json_decode($message, true);

file_put_contents('test.txt', $result);

$token = "255676049:AAEv9GbV95Xg2UmzZR4D-0kXbIj73NKnl4o";
$base_url = "https://api.telegram.org/bot{$token}/SendMessage?chat_id=";
$chat_id = $result['message']['chat']['id'];
$message_from_user = $result["message"]["text"];

function AddtoKeyboard($names)
{
$ar = array(
'keyboard' => array(),
);
foreach ($names as $name) {
$ar2 = array($name);
array_push($ar["keyboard"], $ar2);

}
return $ar;
}

function get_new_message(){
$message = file_get_contents("php://input");
$result = json_decode($message, true);
$message_from_user = $result["message"]["text"];
return $message_from_user;
}

if ($message_from_user == "موبایل") {

$m = array("صفحه ی اصلی", "اندرویدی", "اپل");
$keyb = json_encode(AddtoKeyboard($m));
$rep = file_get_contents($base_url . $chat_id . "&text=" . urlencode("لطفا نوع موبایل مورد نظر را انتخاب کنید: ")
. "&reply_markup=" . $keyb);

$message_from_user = get_new_message();
file_get_contents($base_url . $chat_id . "&text=" . urlencode($message_from_user));

if ($message_from_user == "اپل") {

$iphone = new Iphone();

$m = array("صفحه ی اصلی", "5", "5c", "5s", "se", "6", "6 plus", "6s", "6s plus");
$keyb = json_encode(AddtoKeyboard($m));
$rep = file_get_contents($base_url . $chat_id . "&text=" . urlencode("لطفا مدل موبایل مورد نظر را انتخاب کنید: ")
. "&reply_markup=" . $keyb);
$message_from_user = get_new_message();
$iphone->setModel($message_from_user);

$m = array("صفحه ی اصلی", "16", "64", "128", "256");
$keyb = json_encode(AddtoKeyboard($m));
$rep = file_get_contents($base_url . $chat_id . "&text=" . urlencode("لطفا ظرفیت موبایل مورد نظر را انتخاب کنید: ")
. "&reply_markup=" . $keyb);
$message_from_user = get_new_message();
$iphone->setCapacity($message_from_user);

$m = array("صفحه ی اصلی", "silver", "gray", "gold", "roseGold");
$keyb = json_encode(AddtoKeyboard($m));
$rep = file_get_contents($base_url . $chat_id . "&text=" . urlencode("لطفا رنگ موبایل مورد نظر را انتخاب کنید: ")
. "&reply_markup=" . $keyb);
$message_from_user = get_new_message();
$iphone->setColor($message_from_user);

//TODO check ba database va message dadan quanity

}else if ($message_from_user == "اندروید"){

}


} else if ($message_from_user == "لپ تاپ") {


} else if ($message_from_user == "تبلت") {

$text = "ipad";

} else if ($message_from_user == "صفحه ی اصلی") {

$m = array("موبایل", "تبلت", "لپ تاپ");
$keyb = json_encode(AddtoKeyboard($m));
$rep = file_get_contents($base_url . $chat_id . "&text=" . urlencode("صفحه ی اصلی") . "&reply_markup=" . $keyb);

} else {

$text = "hichkodoom";
file_get_contents($base_url . $chat_id . "&text=" . urlencode($text));

}

?>


but after

if ($message_from_user == "موبایل") {

$m = array("صفحه ی اصلی", "اندرویدی", "اپل");
$keyb = json_encode(AddtoKeyboard($m));
$rep = file_get_contents($base_url . $chat_id . "&text=" . urlencode("لطفا نوع موبایل مورد نظر را انتخاب کنید: ")
. "&reply_markup=" . $keyb);

$message_from_user = get_new_message();


message wont update to last answer and my php file runs from begin and i lost my object iphone!

Answer Source

You can't get new messages like this.

if ($message_from_user == "موبایل") {
        ...

        $message_from_user = get_new_message();

For every message, the php callback is called again (as you have also observed the behavior that your php code is running from the beginning for new messages). If you want to store messages from your users and interact with them, you should store the messages inside a database (or store inside the session), and then when you receive a message, check the chat id to see if you had any interaction with that user (for example any interaction in the past 10 minutes) or want to start over from the beginning.