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

PHP开发:如何让Telegram按钮点击后输出与按钮文本不同内容?

问题描述

通过for循环创建了若干Telegram按钮,代码如下:

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

需求:点击按钮(比如文本为6的按钮)时,Telegram聊天窗口输出Test_6,即输出内容与按钮文本不同,需要修改哪些代码?

解决方案

你当前使用的是Telegram的普通键盘(Reply Keyboard),这类按钮点击后会直接将按钮文本发送到聊天窗口,无法实现“点击后输出不同内容”的需求。要实现目标,需要改用内联键盘(Inline Keyboard),并添加回调事件处理逻辑,具体修改如下:

1. 修改按钮数组结构与发送逻辑

普通键盘的keyboard字段需要替换为内联键盘的inline_keyboard,同时调整按钮数组的结构(内联键盘的每行按钮是一个数组元素):

// 构建内联键盘数组
for ($i = 1; $i < 35; $i++) {
    // 每行一个按钮,若要多行可调整数组嵌套
    $arr[] = array(
        array(
            "text"          => $i,
            "callback_data" => "Test_".$i,
        )
    );
}
$bot->sendInlineKeyboard($chat_id, $reply, $arr);

// 修改原sendKeyboard为sendInlineKeyboard(或直接修改原方法)
public function sendInlineKeyboard($chat_id, $text, $keyboard = Array())
{
    $action = 'sendMessage';
    $param = array(
        'chat_id'       => $chat_id,
        // 将keyboard替换为inline_keyboard,移除one_time_keyboard(内联键盘无此参数)
        'reply_markup'  => json_encode(array(
            "inline_keyboard" => $keyboard,
            "resize_keyboard" => true
        )),
        'text'          => $text
    );
    
    $res = $this->send($action, $param);
    if (!$res['ok']) {
        return Array("success" => 0, "info"  =>  "Error: " . $res['description']);
    } else {
        return Array("success" => 1, "info"  =>  "Inline keyboard show");
    }
}

2. 添加回调事件处理逻辑

内联按钮点击后,Telegram会向你的bot服务器发送callback_query类型的更新,需要监听并处理该事件:

// 处理Telegram更新的方法(可通过getUpdates或webhook实现)
public function handleUpdates()
{
    $updates = $this->getUpdates(); // 调用API获取更新
    foreach ($updates as $update) {
        // 判断是否为回调查询
        if (isset($update['callback_query'])) {
            $callback = $update['callback_query'];
            $chat_id = $callback['message']['chat']['id'];
            $callback_data = $callback['data']; // 获取Test_6这类自定义值
            
            // 发送目标内容到聊天窗口
            $this->sendMessage($chat_id, $callback_data);
            
            // 标记回调为已处理,避免用户端显示加载状态
            $this->answerCallbackQuery($callback['id']);
        }
    }
}

// 新增基础发送消息方法(若未实现)
public function sendMessage($chat_id, $text)
{
    $action = 'sendMessage';
    $param = array(
        'chat_id' => $chat_id,
        'text' => $text
    );
    return $this->send($action, $param);
}

// 新增回调确认方法(若未实现)
public function answerCallbackQuery($callback_id)
{
    $action = 'answerCallbackQuery';
    $param = array(
        'callback_query_id' => $callback_id
    );
    return $this->send($action, $param);
}

关键说明

  • 普通键盘是让用户输入文本,内联键盘是触发后端回调、由bot主动发送消息,这是实现需求的核心区别。
  • callback_data长度不能超过64字节,你的Test_+数字的格式完全符合要求。
  • 必须处理callback_query更新,否则点击按钮后用户端会一直显示加载状态,且bot无法响应动作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 18:35:02