繁体   English   中英

电报内联键盘 PHP

[英]Telegram Inline keyboards PHP

我想知道如何在单击附加到它的按钮(内联键盘)时更改文本。 它在电报频道中。

事情是这样,但我的代码如下(不需要更多的选择)。

我现在拥有的代码:

$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. 记住 Telegram 返回的message_id
  2. 调用/getUpdates获取按下按钮的callback_data
  3. 使用/editMessageText更新第一条消息

例子;

<?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);
    }

在此处输入图片说明

笔记:

这个例子是基于 OP 的代码编写的,只是为了展示改变 inline_keyboard 的想法。 这段代码纯粹是一个例子,应该有更多的错误检查等......

根据评论,我添加了一段while true以继续检查新媒体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM