"weatherscript"); $arr2 = array('text' => "some other" /> "weatherscript"); $arr2 = array('text' => "some other" /> "weatherscript"); $arr2 = array('text' => "some other"/>

How can i process the users input after using keyboard in Telegram API

237 Views Asked by At

for now I have the following code

 $arr1 = array('text' => "weather forecast", 'callback_data' => "weatherscript");
 $arr2 = array('text' => "some other script", 'callback_data' => "script2");
 $keyboard = array(array($arr1, $arr2));
 sendInlineKeyboard($bot_id,$json_out->message->chat->id,"Wähle aus",$keyboard);

Depending on pushing button 1 or 2 I want to ask the bot user for another input. Once this input is done, how can I process the bot with a relevant reply.

The bot should do following:

  1. Ask which script should be executed. See above. Let's say a weather script.
  2. Ask for a manually typed reply. Let's say to provide the name of a specific city
  3. Bot should reply with the weather of the city name provided

So generally what I am struggeling with is how to process the reply of bot user after selecting by button

1

There are 1 best solutions below

1
TKKYTRS On

If you use callback query then you have to add some more lines of code

$update = json_decode(file_get_contents('php://input'), TRUE);

$botToken = "<MY-TOKEN>";
$botAPI = "https://api.telegram.org/bot" . $botToken;

// Check if callback is set
if (isset($update['callback_query'])) {

    // Reply with callback_query data
    $data = http_build_query([
        'text' => 'Selected option: ' . $update['callback_query']['data'],
        'chat_id' => $update['callback_query']['from']['id']
    ]);
    file_get_contents($botAPI . "/sendMessage?{$data}");
}

Something like that