繁体   English   中英

电报不显示通过 Botman 发送的键盘

[英]Telegram does not show keyboard sent via Botman

我正在通过 Botman 和 php 为我的电报机器人创建一个进程。

在现在的情况下,我需要在用户消息字段下使用按钮制作消息,如下所示:

在此处输入图片说明 我尝试下一个代码:

    use BotMan\Drivers\Telegram\Extensions\Keyboard;
    use BotMan\Drivers\Telegram\Extensions\KeyboardButton;

    ...

    $this->ask($question, function (Answer $response) use ($action) {
        ...
    }, Keyboard::create(Keyboard::TYPE_KEYBOARD)->addRow(
            KeyboardButton::create('test 1')->requestLocation()->callbackData('test1'),
            KeyboardButton::create('test 2')->requestLocation()->callbackData('test2')
        )->toArray()
    );

但是我没有看到这些按钮。 麻烦在哪里?

阅读 botman 电报驱动程序的源代码后,我发现 botman/driver-telegram ^1.6 不支持键盘。 (或者我不知道它是如何开箱即用的)

我通过覆盖所有驱动程序代码来解决这个问题。 像这样。 将所有原始代码复制到我自己的 CustomTelegramDriver.php 并加载它

DriverManager::loadDriver(CustomTelegramDriver::class);

然后在 buildServicePayload 方法中,我检查 $additionalParameters 中的reply_markup

像这样:

if ($message instanceof Question) {
        self::getLogger()->info("message instanceoff Question", ["custom_telegram_driver"]);
        $parameters['text'] = $message->getText();

        // Where reply_markup passed from additionalParameters!
        // this line of code is my fix and it get to work keyboard
        if(isset($additionalParameters['reply_markup'])) {
            $parameters['reply_markup'] = $additionalParameters['reply_markup'];
        } else {
            $parameters['reply_markup'] = json_encode([
                'inline_keyboard' => $this->convertQuestion($message)
            ], true);
        }

    }

然后在我的机器人代码中,我通过了这个

$keyboard = Keyboard::create(Keyboard::TYPE_KEYBOARD)
                ->oneTimeKeyboard()
                ->addRow(KeyboardButton::create(OnboardingConversation::translate("btn_lang_en", "en"))->callbackData('en'))
                ->addRow(KeyboardButton::create(OnboardingConversation::translate("btn_lang_ru", "en"))->callbackData('ru'))
            ->toArray();

在我的询问代码中

$question = Question::create("test");
$this->ask($question, function (Answer $answer) {
   // some stuff
}, $keyboard); 

暂无
暂无

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

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