I
I
Ivan2016-07-20 13:24:16
PHP
Ivan, 2016-07-20 13:24:16

How to send commands to the Telegram bot when choosing in the menu?

There is a code that creates and sends text when a key is pressed.

$keyboard = array(array("test1","test2","test3"));
  $resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
  $reply = json_encode($resp);
  $url = $GLOBALS[website]."/sendmessage?chat_id=".$chatId."&text=/hi&reply_markup=".$reply;
    file_get_contents($url);

I don’t understand how to catch which of the 3 keys I pressed? and how to make the menu have one text, and when you click on it, for example, a command is sent?
Here is an example with Yandex bot.
Press Translation-> Enter text-> Send text-> Receive translation;
How, at the stage of sending the text, do we know that we need to translate the text?
d49whIQCpkc.jpg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
landergate, 2016-07-20
@trak_ivan

I don’t understand how to catch which of the 3 keys I pressed?
and how to make the menu have one text, and when you click on it, for example, a command is sent?

With Inline Keyboards introduced in Bot API 2.0.
https://core.telegram.org/bots/2-0-intro#new-inlin...
https://core.telegram.org/bots/api#inlinekeyboardmarkup
> Unlike with custom reply keyboards, pressing buttons on inline keyboards doesn't result in messages sent to the chat. Instead, inline keyboards support buttons that work behind the scenes: callback buttons, URL buttons and switch to inline buttons.
When receiving a message from user_id, the bot checks which step this user_id last stopped at and processes the message within the current step. If the step is "translate" - then in response to any message it will translate the string. At the "translate" stage, the bot simply does not show any menu, it just waits for a string.
To do this, the bot makes a session store. It can be just a database with `user_id` and `state` columns, where the current step will be indicated in the state. When processing a message, the bot looks at the step and acts according to what the user wanted in that step.
If the user requested another function, or did /cancel, change the step in the table.

A
Apcel, 2016-07-20
@Apcel

Actually, as it was found out [elsewhere], when the user clicks on different buttons, (in this case) "test1", "test2" and "test3" will come - that is, the text set by the bot for the button that the user clicked
Respectively , the second part of the question can be done by comparing the received text with the text of the buttons and then calling the desired command handler for the received command.

I
iBkm, 2016-11-11
@iBkm

also interested in this question. it turns out that if the button is named in Russian "How are you?", then you need to check the if($stroka == "How are you?") line, and then you can already return some result to the chat?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question