S
S
Sergei Gurdjiyan2017-10-05 08:43:38
Laravel
Sergei Gurdjiyan, 2017-10-05 08:43:38

How to assign a command to the Telegram bot's menu button?

I am writing a bot.
I'm using PHP / Laravel / telegram-bot-sdk
When I command /start, the bot displays a message and a menu to select the language.

$languages = config('app.locales_assoc');

        $keyboard = [];
        $keyboardArray = [];
        foreach ($languages as $language) {
            array_push($keyboardArray, $language);
        }
        $keyboard[] = $keyboardArray;

        $reply_markup = $this->telegram->replyKeyboardMarkup([
          'keyboard' => $keyboard,
          'resize_keyboard' => true,
          'one_time_keyboard' => true,
          'selective' => false
        ]);

        $this->replyWithMessage(
          [
            'text' => trans('bot.greeting_lang'),
            'reply_markup' => $reply_markup
          ]
        );

As a result, the user receives a simple menu of 3 buttons "Russian", "Ukrainian" and "English".
When you click on the button, a message with the same string goes into the chat, for example "Russian".
Is it possible to make it so that when the user clicks on the menu button, the user will go to the chat with a message like
/setLanguage Русский
Is it possible to assign commands to the menu buttons?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2017-10-05
@mrKorg

You need to use an inline keyboard

$button_en = array('text' => 'English', 'callback_data' => '/lang_english');
$button_ru = array('text' => 'Русский', 'callback_data' => '/lang_russian');
        
$keyboard = array('inline_keyboard' => array(array($button_en, $button_ru)));
$params['reply_markup'] = json_encode($keyboard, TRUE);

After clicking the button, you will need to accept a callback_query. For convenience, I write the value of callback_data through "_", that is, when sending / lang_russian, I break this value into segments, after which it becomes clear that I need to send one parameter " russian " to the method associated with setting the set_ lang () language .

N
nllm, 2017-10-05
@nllm

This can be done using inline buttons
Or leave it as it is, but work with states.
For example, after clicking on /start, the bot enters the "language selection" state. After clicking on one of the buttons "Russian", "Ukrainian" and "English", check that you have indicated (pressed) one of the buttons, and did not enter arbitrary text, and if everything is fine, then change the state to waiting for the next command, display the corresponding text.
There were many questions about working with states, they are all on toster.ru

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question