0

I have created some Telegram inline buttons with a for loop.

for ($i = 1; $i < 35; $i++) {
    $arr[] = array(array(
        "text"          => $i,
        "callback_data" => "Test_".$i,
    ));
}
$bot->sendKeyboard($chat_id, $reply, $arr);

public function sendKeyboard($chat_id, $text, $keyboard = Array())
{
    $action = 'sendMessage';
    $param = array(
        'chat_id'       => $chat_id,
        'reply_markup'  => json_encode(array("keyboard" => $keyboard, "one_time_keyboard" => true, "resize_keyboard" => true)),
        'text'          => $text
    );
    
    $res = $this->send($action, $param);
    if (!$res['ok']) {
        $result = Array("success" => 0, "info"  =>  "Error: " . $res['description']);
            } else {
        $result = Array("success" => 1, "info"  =>  "Keyboard show");
            }
    return $result;
}

E.g. the Button-Text is 6, I want Telegram to write Test_6 in chat.

What do I have to change so that a different text than that on the button is output?

0stone0
  • 34,288
  • 4
  • 39
  • 64
Outi77
  • 11
  • 2

1 Answers1

0

The text on a button is what the user sees in Telegram.

The callback_data is an internal value that you get back when somebody presses on the button, so you use that to handle the click event.


You'll need to create the buttons like so:

for ($i = 1; $i < 35; $i++) {
    $arr[] = array(array(
        "text"          => "Test_".$i,
        "callback_data" => $i
    ));
}

So that the users sees Test_1 until Test_35. And if somebody presses the button, you will get the number as data 1 to 35.

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • OK and how can I evaluate the callback_data? – Outi77 Apr 14 '23 at 11:48
  • Depends on how you receive updates from Telegram. [This example](https://stackoverflow.com/questions/60831910/how-to-get-callback-data-from-telegram-in-php) might help. – 0stone0 Apr 14 '23 at 11:49
  • OK thanks, that works so far. Please help me, is it possible to generate a keyboard in a loop? Something like... for ($i = 1; $i < 35; $i++) { $keyboard = json_encode([ "inline_keyboard" => [ [ [ "text" => "Test_".$i, "callback_data" => $i ] ] ] ]); } – Outi77 Apr 30 '23 at 19:57