电报嵌入式键盘PHP

时间:2020-03-04 15:44:02

标签: php api telegram

我想知道当单击附加在文本上的按钮(嵌入式键盘)时如何更改文本。在电报频道中。

类似this,但下面有我的代码(不需要更多选项)。

我现在拥有的代码:

$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");

1 个答案:

答案 0 :(得分:1)

发送消息后;

  1. 记住电报返回的message_id
  2. 致电/getUpdates以获取按下按钮的callback_data
  3. 使用/editMessageTextupdate第一条消息

示例;

<?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

注意:

此示例是根据OP的代码编写的,仅用于说明更改inline_keyboard的想法。 这段代码仅作为示例,应该有更多的错误检查等。

根据评论,我加入了while true,以继续检查是否有新印刷机。