Telegram-bot-sdk: Inline Keyboard API

Created on 18 Sep 2017  Â·  6Comments  Â·  Source: irazasyed/telegram-bot-sdk

Current :

$keyboard = Keyboard::make()
                ->inline()
                ->row(
                    Keyboard::inlineButton([
                        'text' => 'Test Btn',
                        'callback_data' => 'callback_from_testbtn'
                    ])
                );

Do you Agree with :

$keyboard = InlineKeyboard::make()
                  ->addBtn([                   
                        'text' => 'Test 1 Btn',
                        'callback_data' => 'callback_from_testbtn 1'
                   ])
                  ->addBtn([                   
                        'text' => 'Test 2 Btn',
                        'callback_data' => 'callback_from_testbtn2'
                   ]);

I can help you coding it

Most helpful comment

I think this will be helpful

$keyword_data=Array
(
    [0] => Array
        (
            [callback_data] => offer__d8d6b320-8142-11ea-80a6-25ccb7716701
            [text] => ✅ Add to cart $100.00--20/item
        )

    [1] => Array
        (
            [callback_data] => offer__dcd61880-8142-11ea-bfcb-8b974ad3c577
            [text] => ✅ Add to cart $90.00--30/item
        )

    [2] => Array
        (
            [callback_data] => offer__e1ecdc50-8142-11ea-a0a9-35320b861756
            [text] => ✅ Add to cart $80.00--35/item
        )

)


public function inlineKeyword($chat_id, $keyword_data, $msg = '') { if (empty($msg)) { $msg = "Select"; } $inline_keyboard = array(); $row = 0; $prev_value = ''; foreach ($keyword_data as $value) { if (isset($prev_value['text']) && strlen($prev_value['text']) < 10 && strlen($value['text']) < 10) { $inline_keyboard[$row - 1][] = $value; } else { $inline_keyboard[$row][] = $value; } $prev_value = $value; $row++; } // $inline_keyboard[] = $keyword_data; $reply_markup = $this->telegram->replyKeyboardMarkup([ 'inline_keyboard' => $inline_keyboard, 'resize_keyboard' => true ]); $response = $this->telegram->sendMessage([ 'text' => $msg, 'reply_markup' => $reply_markup, 'chat_id' => $chat_id ]); }

Screenshot 2020-04-19 at 11 25 39 AM

All 6 comments

Does your method support rows with multiple buttons?

Currently, it seems like single button in each row.

What about :

$keyboard = InlineKeyboard::make()
                  ->addBtnRow(                   
                      [  'text' => 'Test 111',  'callback_data' => value111'],
                      [  'text' => 'Test 222',  'callback_data' => 'value222'],
                   )
                  ->addBtnRow(              
                       [ 'text' => 'Test 333', 'url' => 'http://www.example.com']
                   );
$keyboard = InlineKeyboard::make()
                  ->nextRow()  ->cbBtn( 'Test 111', 'value111') ->cbBtn( 'Test 222', 'value222')     
                  ->nextRow()  ->cbBtn( 'Test 444', 'value444)
                  ->nextRow()  ->urlBtn( 'My site', 'http://www.example.com')      
                   ;

or something like that...

I think the current implementation does a good job already. Look at this:

$keyboard = Keyboard::make()
            ->inline()
            ->row(
                Keyboard::inlineButton([
                    'text' => 'Test Btn',
                    'callback_data' => 'callback_from_testbtn'
                ]),
                Keyboard::inlineButton([
                    'text' => 'Test Btn 2',
                    'callback_data' => 'callback_from_testbtn'
                ])
            );

I'm thinking the idea proposed by @imanghafoori1 works. I'm looking right now to do something like this

foreach( $answers as $answer ) {
    $keyboard->addButton([
        'text' => $answer,
        'callback_data' => 'callback'
    ]);
}

Are you still developing this SDK?

I think this will be helpful

$keyword_data=Array
(
    [0] => Array
        (
            [callback_data] => offer__d8d6b320-8142-11ea-80a6-25ccb7716701
            [text] => ✅ Add to cart $100.00--20/item
        )

    [1] => Array
        (
            [callback_data] => offer__dcd61880-8142-11ea-bfcb-8b974ad3c577
            [text] => ✅ Add to cart $90.00--30/item
        )

    [2] => Array
        (
            [callback_data] => offer__e1ecdc50-8142-11ea-a0a9-35320b861756
            [text] => ✅ Add to cart $80.00--35/item
        )

)


public function inlineKeyword($chat_id, $keyword_data, $msg = '') { if (empty($msg)) { $msg = "Select"; } $inline_keyboard = array(); $row = 0; $prev_value = ''; foreach ($keyword_data as $value) { if (isset($prev_value['text']) && strlen($prev_value['text']) < 10 && strlen($value['text']) < 10) { $inline_keyboard[$row - 1][] = $value; } else { $inline_keyboard[$row][] = $value; } $prev_value = $value; $row++; } // $inline_keyboard[] = $keyword_data; $reply_markup = $this->telegram->replyKeyboardMarkup([ 'inline_keyboard' => $inline_keyboard, 'resize_keyboard' => true ]); $response = $this->telegram->sendMessage([ 'text' => $msg, 'reply_markup' => $reply_markup, 'chat_id' => $chat_id ]); }

Screenshot 2020-04-19 at 11 25 39 AM

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mccarlosen picture mccarlosen  Â·  3Comments

behnamazimi picture behnamazimi  Â·  3Comments

realtebo picture realtebo  Â·  4Comments

corneyl picture corneyl  Â·  3Comments

chaskayu picture chaskayu  Â·  4Comments