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

PHP开发Telegram机器人时inline_keyboard功能不生效问题求助

问题原因

你的inline键盘按钮配置不符合Telegram Bot API要求,每个按钮仅填写text属性是无效的,必须同时配置至少一个动作属性(比如callback_data、url等),否则API会直接忽略无效的reply_markup参数,导致按钮不展示。

修复方案

修改业务代码中sendMessage的reply_markup配置,给按钮补充必填的动作属性,示例如下:

<?php
require_once('core.php');
$content = file_get_contents("php://input");
$update = json_decode($content , true);
$chat_id = $update["message"]['chat']['id'];
$text = $update["message"]['text'];
$message_id = $update["message"]['message_id'];
if($text == '/start'){
    $matn = 'Welcome';
    MessageRequestJson('sendMessage' , array(
        'chat_id'=>$chat_id,
        'text'=>$matn,
        'reply_markup'=>array(
            "inline_keyboard" => array(
                array(
                    // 回调按钮,点击后会把callback_data的值回传给机器人,值可自定义
                    array(
                        "text" => "My Button Text",
                        "callback_data" => "custom_action_1"
                    ),
                    // 如果需要跳转按钮,替换为url属性即可
                    // array("text" => "跳转按钮", "url" => "https://示例域名.com")
                )
            )
        )
    ));
}
?>

调试建议

如果修改后仍有问题,可以打印MessageRequestJson的返回结果,查看Telegram API返回的具体报错信息,方便快速定位问题:

// 调用方法后加一行打印返回值即可
$res = MessageRequestJson(/* 你原本的传参 */);
var_dump($res);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 20:45:03