Telegram-bot-sdk: Inline keyboards

Created on 2 May 2016  路  10Comments  路  Source: irazasyed/telegram-bot-sdk

Hello,
Meethod Inline Keyboards supported? If yes, show an example of how to use it
https://core.telegram.org/bots#inline-keyboards

Thanx!

Most helpful comment

Hi @dvdmarchetti Thanks for sharing the information.

Here's how you would do using the fluent API. It does support multiple buttons in single row. Example (Modified from your version):

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

$this->replyWithMessage(['text' => 'Start command', 'reply_markup' => $keyboard]);

This should show up 2 buttons in a row. Ofc you can add as many as you want, Since there are no limitations from the SDK end. Any limitations would be totally from Telegram itself.

Also, If you want the buttons to be on the next row, you simply chain it using the row() method and pass the button method.

All the other information you've provided is valid too.

Once V3 is released, these things will be documented with more examples. Thanks!

when do you release V3 documentation?

All 10 comments

Introduction

I had the same problem so I looked through the source code and found two ways to send inline keyboards.
I'll explain these here, but I'm not sure if these are the correct methods to do that.


First method

My command handler is the following (I'm currently using Laravel 5.2 and Telegram Bot SDK 3.0 dev):

<?php
namespace App\Commands;

use Telegram\Bot\Commands\Command;
use Telegram\Bot\Keyboard\Keyboard;

use Telegram;

/**
 * Class StartCommand.
 */
class StartCommand extends Command
{
    /**
     * @var string Command Name
     */
    protected $name = 'start';

    /**
     * @var string Command Description
     */
    protected $description = 'Join';

    /**
     * {@inheritdoc}
     */
    public function handle($arguments)
    {
        // Method One -> Create a layout just like a normal keyboard,
        // but use Telegram\Bot\Keyboard\Keyboard::inlineButton to create each button
        $inlineLayout = [
            [
                Keyboard::inlineButton(['text' => 'Test', 'callback_data' => 'data']),
                Keyboard::inlineButton(['text' => 'Btn 2', 'callback_data' => 'data_from_btn2'])
            ]
        ];

        $keyboard = Telegram::replyKeyboardMarkup([
            'inline_keyboard' => $inlineLayout
        ]);

        $this->replyWithMessage(['text' => 'Start command', 'reply_markup' => $keyboard]);
    }
}

The advantages of this approach is that you can have more than one button per line, but it's a bit confusing.

Second method

The second method uses the fluent API, and the command handler is the following:

    ### Controller stuffs like the previous one

    public function handle($arguments)
    {
        // Method Two -> Create each button and set it
        // but use Telegram\Bot\Keyboard\Keyboard::inlineButton to create each button

        $firstButton = Keyboard::inlineButton(['text' => 'Test', 'callback_data' => 'data']);
        $secondButton = Keyboard::inlineButton(['text' => 'Btn 2', 'callback_data' => 'data_from_btn2']);

        $keyboard = Telegram::replyKeyboardMarkup([])->inline()
            ->row($firstButton)
            ->row($secondButton);

        $this->replyWithMessage(['text' => 'Start command', 'reply_markup' => $keyboard]);
    }

The drawback of this method is that you can't set more than one button per line.


Anyway I still can't handle commands query coming from inline buttons (they give a CallbackQuery object instead of a Message), but you can still use them to open urls (refer to the Telegram's Bot API for more informations).
I'll keep up this issue if I figure out how to do it.

Hi @dvdmarchetti Thanks for sharing the information.

Here's how you would do using the fluent API. It does support multiple buttons in single row. Example (Modified from your version):

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

$this->replyWithMessage(['text' => 'Start command', 'reply_markup' => $keyboard]);

This should show up 2 buttons in a row. Ofc you can add as many as you want, Since there are no limitations from the SDK end. Any limitations would be totally from Telegram itself.

Also, If you want the buttons to be on the next row, you simply chain it using the row() method and pass the button method.

All the other information you've provided is valid too.

Once V3 is released, these things will be documented with more examples. Thanks!

Thanks for your correction. I didn't think to try with multiple parameters, just as an array of multiple keyboard buttons.
Anyway thanks for the info!

Thanks to all!

Hi, how can i handle "callback_data" event sended on press on inline button?

Hi.

How we can send another command by clicking on btn with callback_data?

An example
}elseif(preg_match('/^/([Pp]lay)/',$text1)){
$text = "do you know the answer? 95 x 95 select your answer";
bot('sendmessage',[
'chat_id'=>$chat_id,
'text'=>$text,
'parse_mode'=>'html',
'reply_markup'=>json_encode([
'inline_keyboard'=>[
[
['text'=>'9525','url'=>'false']
],
[
['text'=>'9025','url'=>'true to play more press /playnext thanks']
]
]
])
]);

How to hide keyboard by sending this:

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

I mean hide normal keyboard and send this inline keyboard.

@derakoola You can use Keyboard::hide() method. Send that as your keyboard markup first and then in your second request, you can send your new keyboard markup.

Hi @dvdmarchetti Thanks for sharing the information.

Here's how you would do using the fluent API. It does support multiple buttons in single row. Example (Modified from your version):

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

$this->replyWithMessage(['text' => 'Start command', 'reply_markup' => $keyboard]);

This should show up 2 buttons in a row. Ofc you can add as many as you want, Since there are no limitations from the SDK end. Any limitations would be totally from Telegram itself.

Also, If you want the buttons to be on the next row, you simply chain it using the row() method and pass the button method.

All the other information you've provided is valid too.

Once V3 is released, these things will be documented with more examples. Thanks!

when do you release V3 documentation?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

khalilst picture khalilst  路  3Comments

chaskayu picture chaskayu  路  4Comments

jahanzaibbahadur picture jahanzaibbahadur  路  4Comments

danilopinotti picture danilopinotti  路  3Comments

mccarlosen picture mccarlosen  路  3Comments