【问题标题】:Telegram does not show keyboard sent via BotmanTelegram 不显示通过 Botman 发送的键盘
【发布时间】:2020-05-01 06:01:12
【问题描述】:

我正在通过 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()
    );

但我没有看到这些按钮。问题出在哪里?

【问题讨论】:

    标签: php telegram telegram-bot botman


    【解决方案1】:

    在阅读了 botman telegram 驱动程序的源代码后,我发现 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); 
    

    【讨论】:

      猜你喜欢
      • 2019-02-08
      • 2017-06-02
      • 2017-06-18
      • 2016-11-24
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多