Fede777
Fede777

Reputation: 21

PHP - How to read the text of a button that was clicked on ReplyKeyboardMarkup (Telegram bot)

I want to know hot to read and save into a variable the text of a button that was clicked by a user who chat with my telegram bot.

PROBLEM:

I have this piece of code that helps me to take the webhook response for my bot.

$getupdates = file_get_contents("php://input");
$updates = json_decode($getupdates, TRUE);

then i save into a variable ($chatID) the chat_id of the user that chat with my bot and another variable $text is the text of the message.

Then i add this swtich

switch($text)
{
    case "/aggiungi":
    sendMessageButtons($chatID, "Sample text", $keyboard);
}

$keyboard is my array of array which contains my ReplyKeyboardMarkup keyboard.

My function "sendMessageButtons" is the following:

function sendMessageButtons($chatID, $message, $keyboard)
{
    $url = $GLOBALS['website']."/sendMessage?chat_id=".$chatID."&text=".urlencode($message)."&reply_markup=".$keyboard;
    file_get_contents($url);
}

The result is this.

But how can I know which button was clicked and then save the text of the button into a variable?

I tried to add code under the case

case "/aggiungi":
sendMessageButtons($chatID, "Sample text", $keyboard);
**$ButtonText = $updates["message"]["text"];
sendMessage($chatID, $ButtonText);**

but the result is this.

The bot return to "/aggiungi" case, print the last text on the screen (which is "/aggiungi") and it doesn't wait my input from the keyboard.

QUESTION:

How can I save the text that came from the button that was clicked?

I hope you understand the question and thank you all for your time!

Upvotes: 2

Views: 1440

Answers (1)

Heisenberg
Heisenberg

Reputation: 46

use below code:

'inline_keyboard'=>[
        [
         ['text'=>"mt btn text",'callback_data'=>"bbv"]

        ]

then with if($data == "bbv"){do somthing} you can find which button is clicked.

Upvotes: 0

Related Questions