2

I would like to know how to change the text when clicked button attached to it (Inline keyboards). It's in a telegram channel.

Something like this but with my code below (no need for more options).

The code I have now:

$data = [
            'text' => 'choose options yes or no', 
            'chat_id' => '-100234234234'
          ];

$keyboard = array(
    "inline_keyboard" => array(
        array(
            array(
                "text" => "Yes",
                "callback_data" => "myCallbackData"
            ),
            array(
                "text" => "No",
                "callback_data" => "myCallbackData"
            )
        )
    )

file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) . "&parse_mode=html&reply_markup=$keyboard");
Tod
  • 187
  • 3
  • 13

1 Answers1

5

After sending the message;

  1. Remember the message_id returned by Telegram
  2. Call /getUpdates to get the callback_data of pressed button
  3. Use /editMessageText to update the first message

Example;

<?php

    // Create data
    $data = http_build_query([
        'text' => 'Yes - No - Stop?',
        'chat_id' => '1234567890'
    ]);

    // Create keyboard
    $keyboard = json_encode([
        "inline_keyboard" => [
            [
                [
                    "text" => "Yes",
                    "callback_data" => "yes"
                ],
                [
                    "text" => "No",
                    "callback_data" => "no"
                ],
                [
                    "text" => "Stop",
                    "callback_data" => "stop"
                ]
            ]
        ]
    ]);

    // Send keyboard
    $url = "https://api.telegram.org/bot$token/sendMessage?{$data}&reply_markup={$keyboard}";
    $res = @file_get_contents($url);

    // Get message_id to alter later
    $message_id = json_decode($res)->result->message_id;

    // Continually check for a 'press'
    while (true) {

        // Call /getUpdates
        $updates = @file_get_contents("https://api.telegram.org/bot$token/getUpdates");
        $updates = json_decode($updates);

        // Check if we've got a button press
        if (count($updates->result) > 0 && isset(end($updates->result)->callback_query->data)) {

            // Get callback data
            $callBackData = end($updates->result)->callback_query->data;

            // Check for 'stop'
            if ($callBackData === 'stop') {

                // Say goodbye and remove keyboard
                $data = http_build_query([
                    'text' => 'Bye!',
                    'chat_id' => '1234567890',
                    'message_id' => $message_id
                ]);
                $alter_res = @file_get_contents("https://api.telegram.org/bot$token/editMessageText?{$data}");

                // End while
                break;
            }

            // Alter text with callback_data
            $data = http_build_query([
                'text' => 'Selected: ' . $callBackData,
                'chat_id' => '1234567890',
                'message_id' => $message_id
            ]);
            $alter_res = @file_get_contents("https://api.telegram.org/bot$token/editMessageText?{$data}&reply_markup={$keyboard}");
        }

        // Sleep for a second, and check again
        sleep(1);
    }

enter image description here

Note:

This example is written based on OP's code, just to show the idea of altering an inline_keyboard. This code is purely as an example, there should be a lot more error checking etc...

Based on the comment, I've included a while true to keep on checking for new press.

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • thanks for your solution but I can't make it with sleep(), is there any other way to do that without sleep()? – Tod Mar 04 '20 at 20:27
  • @Arny, I've updated the example with a `while true` to act more like your example. Take a look at [daemonizing](https://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process) the script if you really want on ongoing bot that does not dies after catching the 'stop' button. – 0stone0 Mar 04 '20 at 21:48
  • thank you so much my friend for you help! – Tod Jul 09 '21 at 16:07