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

Laravel项目中telegram-bot-sdk无法添加InlineKeyboard组件的解决方案咨询

在 Laravel 的 telegram-bot-sdk 中添加 InlineKeyboardMarkup 和 InlineKeyboardButton 支持

别急,其实有几种优雅的解决方式,不需要硬改SDK包本身,或者你可能只是没注意到新版本的内置支持。我给你分情况说明:


方法一:直接构造数组(最快实现,无需额外代码)

很多时候,SDK的sendMessage等方法其实支持直接传递reply_markup的JSON或数组参数,哪怕没有封装专门的类。你可以手动构造Inline键盘的结构:

// 构造Inline键盘的按钮数组
$inlineKeyboard = [
    // 第一行按钮
    [
        ['text' => '打开官网', 'url' => 'https://your-site.com'],
        ['text' => '触发回调', 'callback_data' => 'user_action_1']
    ],
    // 第二行单个按钮
    [
        ['text' => '内联查询', 'switch_inline_query' => 'search_query']
    ]
];

// 发送消息时传入键盘
$response = $telegram->sendMessage([
    'chat_id' => $yourChatId,
    'text' => '请选择操作:',
    'reply_markup' => json_encode([
        'inline_keyboard' => $inlineKeyboard
    ])
]);

这种方式不需要修改任何代码,直接就能用,适合快速测试或简单场景。


方法二:扩展SDK,创建自己的键盘类(优雅且可复用)

如果你的项目需要频繁使用Inline键盘,建议封装自己的类,和SDK现有风格保持一致:

1. 创建 InlineKeyboardButton 类

在app/Telegram目录下新建InlineKeyboardButton.php:

namespace App\Telegram;

class InlineKeyboardButton
{
    protected $params = [];

    public function __construct(string $text)
    {
        $this->params['text'] = $text;
    }

    // 设置按钮跳转URL
    public function url(string $url): self
    {
        $this->params['url'] = $url;
        return $this;
    }

    // 设置回调数据
    public function callbackData(string $data): self
    {
        $this->params['callback_data'] = $data;
        return $this;
    }

    // 支持其他Telegram按钮属性,比如switch_inline_query等
    public function switchInlineQuery(string $query): self
    {
        $this->params['switch_inline_query'] = $query;
        return $this;
    }

    public function toArray(): array
    {
        return $this->params;
    }
}

2. 创建 InlineKeyboardMarkup 类

同样在app/Telegram目录下新建InlineKeyboardMarkup.php:

namespace App\Telegram;

class InlineKeyboardMarkup
{
    protected $inlineKeyboard = [];

    // 添加一行按钮
    public function addRow(array $buttons): self
    {
        $row = [];
        foreach ($buttons as $button) {
            if ($button instanceof InlineKeyboardButton) {
                $row[] = $button->toArray();
            }
        }
        $this->inlineKeyboard[] = $row;
        return $this;
    }

    // 转换为数组,供SDK使用
    public function toArray(): array
    {
        return ['inline_keyboard' => $this->inlineKeyboard];
    }

    // 转换为JSON字符串
    public function toJson(): string
    {
        return json_encode($this->toArray());
    }
}

3. 使用自定义键盘类

use App\Telegram\InlineKeyboardButton;
use App\Telegram\InlineKeyboardMarkup;

// 构建键盘
$keyboard = new InlineKeyboardMarkup();
$keyboard->addRow([
    (new InlineKeyboardButton('打开官网'))->url('https://your-site.com'),
    (new InlineKeyboardButton('触发回调'))->callbackData('user_action_1')
])->addRow([
    (new InlineKeyboardButton('搜索内容'))->switchInlineQuery('laravel')
]);

// 发送消息
$telegram->sendMessage([
    'chat_id' => $yourChatId,
    'text' => '请选择操作:',
    'reply_markup' => $keyboard->toJson()
]);

方法三:升级SDK到最新版本(推荐)

如果你使用的是旧版的telegram-bot-sdk,其实新版本已经内置了Inline键盘的支持!你可以先尝试升级SDK:

composer require irazasyed/telegram-bot-sdk

升级后,直接使用SDK内置的Keyboard类即可:

use Telegram\Bot\Keyboard\Keyboard;

// 构造Inline键盘
$inlineKeyboard = Keyboard::inline([
    [
        Keyboard::button(['text' => '按钮1', 'callback_data' => 'cb_1']),
        Keyboard::button(['text' => '打开官网', 'url' => 'https://your-site.com'])
    ],
    [
        Keyboard::button(['text' => '内联搜索', 'switch_inline_query' => 'telegram bot'])
    ]
]);

// 发送消息
$telegram->sendMessage([
    'chat_id' => $yourChatId,
    'text' => 'Hello!',
    'reply_markup' => $inlineKeyboard
]);

不推荐的方式:直接修改SDK源码

虽然可以直接在SDK的src/Keyboard目录下添加对应的类,但这种方式会导致后续SDK更新时丢失修改,除非你fork SDK并维护自己的分支,所以不建议普通项目这么做。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:59:58