Core: Keyboard issue in 0.37

Created on 16 Dec 2016  路  6Comments  路  Source: php-telegram-bot/core

So I migrated my bot to 0.37 from 0.35 following all changes and it seems like something is wrong with Keyboard/InlineKeyboard entity.

Assuming we have buttons array like this (shows buttons under each other):

$inline_keyboard = [
    [
        [
            'text' => $text['button_text'],
            'url' => $text['button_url']
        ]
    ],
    [
        [
            'text' => 'Clear',
            'callback_data' => 'clear'
        ]
    ]
];

$data['reply_markup'] = new InlineKeyboardMarkup(['inline_keyboard' => $inline_keyboard]);

in 0.35 worked fine, migrating to 0.37 with following change doesn't work:

$data['reply_markup'] = new InlineKeyboard($inline_keyboard);

Telegram API replies Bad Request: REPLY_MARKUP_INVALID

Changing code to:

$inline_keyboard = [
    [
        'text' => $text['button_text'],
        'url' => $text['button_url']
    ],
    [
        'text' => 'Clear',
        'callback_data' => 'clear'
    ]
];

Works, but will make the buttons show up next to each other, not under each other!

All 6 comments

You're right, the plain array/text keyboards don't work any more, that's my bad, sorry.

My initial idea was to make keyboards more intuitive, but totally failed to keep the most basic structure possible, argh.

Here is the example for your keyboard, 2 buttons under each other:

$inline_keyboard = new InlineKeyboard(
    new InlineKeyboardButton([
        'text' => $text['button_text'],
        'url'  => $text['button_url'],
    ]),
    new InlineKeyboardButton([
        'text'          => 'Clear',
        'callback_data' => 'clear',
    ])
);

Basically, each button passed to the new InlineKeyboard() constructor gets a new row. If you want multiple ones on the same row, just pack them in an array, like so:

$inline_keyboard = new InlineKeyboard([
    new InlineKeyboardButton([
        'text' => $text['button_text'],
        'url'  => $text['button_url'],
    ]),
    new InlineKeyboardButton([
        'text'          => 'Clear',
        'callback_data' => 'clear',
    ])
]);

Will look into getting the other structure working too 馃憤

Same system applies for normal Keyboards.

I guess I'll stick with 0.35 for a little longer then, it would take a lot of time to rewrite all my keyboard code...

https://github.com/akalongman/php-telegram-bot/wiki/New-Keyboard-structure-and-how-to-pass-dynamic-arguments

$inline_keyboard[] = [
    [
        [
            'text' => $text['button_text'],
            'url' => $text['button_url']
        ]
    ],
    [
        [
            'text' => 'Clear',
            'callback_data' => 'clear'
        ]
    ]
];

$data['reply_markup'] = new InlineKeyboard(...$inline_keyboard);

@nullcookies Thanks for posting that, totally forgot about that guide.
@jacklul Check out that wiki entry, I had my syntax wrong before.

I knew that it should be possible with the plain static syntax...

@jacklul Does the instructions from the wiki entry do it for you?
It's just a minor change you need to make to the keyboards, the keyboard code itself can stay the same.

Everything is working now, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NabiKAZ picture NabiKAZ  路  3Comments

NabiKAZ picture NabiKAZ  路  4Comments

tchibomann picture tchibomann  路  3Comments

Recouse picture Recouse  路  3Comments

irmmr picture irmmr  路  3Comments