2

I am creating a process for my telegram bot via Botman and php.

In the now case I need to make a message with buttons under a user message field like this:

enter image description here I try the next code:

    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()
    );

But I do not see these buttons. Where is the trouble?

Dilhan Nakandala
  • 301
  • 5
  • 24
demover123
  • 21
  • 2

1 Answers1

0

After reading source code for botman telegram driver i found that keyboard not supported for botman/driver-telegram ^1.6 . (or i not have idea how it work outofbox)

I solve this by override all driver code. Like this. Copy all original code to my own CustomTelegramDriver.php and load it

DriverManager::loadDriver(CustomTelegramDriver::class);

Then in buildServicePayload method i check reply_markup key in $additionalParameters

like this:

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

    }

then in my bot code in ask i passed this

$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();

And in my ask code

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