You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP库开发Telegram Bot,如何隐藏行内文本输入框?

解决Telegram Bot仅显示按钮不显示行内输入框的问题

首先咱们得理清你代码里的核心问题:你用的是ReplyKeyboardMarkup,这是底部回复键盘,它的特性就是会出现在输入框上方;而你调用的editMessageReplyMarkup是用来修改消息内的内联键盘(InlineKeyboardMarkup)的,所以对回复键盘完全不起作用。

针对你的需求,分两种场景给出解决方案:

场景1:想在消息内显示按钮(不占用输入框空间)

这时候应该用InlineKeyboardMarkup代替ReplyKeyboardMarkup——它的按钮是嵌入在消息里的,不会影响输入框的正常显示。修改后的代码如下:

<?php
require_once __DIR__ ."/vendor/autoload.php";

try {
    $bot = new \TelegramBot\Api\Client('***');

    $bot->command('start', function ($message) use ($bot) {
        // 创建消息内的内联按钮
        $keyboard = new \TelegramBot\Api\Types\InlineKeyboardMarkup([
            [
                ['text' => 'One', 'callback_data' => "one"],
                ['text' => 'Two', 'callback_data' => "two"],
            ]
        ]);
        // 发送带内联按钮的消息,输入框不受任何影响
        $bot->sendMessage($message->getChat()->getId(), "Choose the number", null, false, null, $keyboard);
    });

    $bot->run();
} catch (\TelegramBot\Api\Exception $e) {
    echo $e->getMessage();
}

场景2:已发送回复键盘,想隐藏它让输入框恢复正常

如果你确实需要用底部回复键盘,但之后想隐藏它,应该使用ReplyKeyboardRemove,而不是editMessageReplyMarkup(因为回复键盘是和聊天会话绑定的,并非单个消息)。示例代码:

<?php
require_once __DIR__ ."/vendor/autoload.php";

try {
    $bot = new \TelegramBot\Api\Client('***');

    $bot->command('start', function ($message) use ($bot) {
        $keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup([
            [
                ['text' => 'One', 'callback_data' => "one"],
                ['text' => 'Two', 'callback_data' => "two"],
            ]
        ], true, true);
        
        $sentMessage = $bot->sendMessage($message->getChat()->getId(), "Choose the number", null, false, null, $keyboard);
        
        // 发送新消息移除底部回复键盘,让输入框恢复正常
        $removeKeyboard = new \TelegramBot\Api\Types\ReplyKeyboardRemove(true);
        $bot->sendMessage($message->getChat()->getId(), "Keyboard hidden", null, false, null, $removeKeyboard);
    });

    $bot->run();
} catch (\TelegramBot\Api\Exception $e) {
    echo $e->getMessage();
}

关键知识点总结

  • ReplyKeyboardMarkup:底部回复键盘,和聊天会话绑定,显示在输入框上方,需用ReplyKeyboardRemove隐藏。
  • InlineKeyboardMarkup:消息内联按钮,和单个消息绑定,不干扰输入框,修改/移除需用editMessageReplyMarkup。

你之前的错误在于用针对内联键盘的方法去操作回复键盘,所以完全没效果。换成对应的组件和方法就能解决问题啦!

内容的提问来源于stack exchange,提问作者Alexey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:41:36