Please tell me how to write a simple script for the bot, for example:
wrote the command "/ test",
bot asks: "Are you sure?"
write in response: "Yes"
The bot sends the following message according to the script, if you write "no" - the script ends.
It is important that my answer “Yes” is not processed by the bot until it asks “Are you sure?” (After the / test command)

<?php $access_token = '...'; $api = 'https://api.telegram.org/bot' . $access_token; $output = json_decode(file_get_contents('php://input'), TRUE); $chat_id = $output['message']['chat']['id']; $first_name = $output['message']['chat']['first_name']; $message = $output['message']['text']; switch($message) { case '/test': sendMessage($chat_id, "Вы уверены?"); break; default: $sorry_text = $first_name . ' , мне нечего ответить'; sendMessage($chat_id, $sorry_text); } function sendMessage($chat_id, $message, $encodedMarkup) { file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . $encodedMarkup); } 

I realized that this is done through ForceReply, but I have not figured it out yet.
UPDATE made an inline keybord, but for the time being did not understand how to display a message depending on the pressed case '/ test' button:

  $x1 = array("text"=>"First Button","callback_data"=>"test1"); $x2 = array("text"=>"Second Button","callback_data"=>"test2"); $opz = [[$x1,$x2]]; $keyboard=array("inline_keyboard"=>$opz); $keyboard = json_encode($keyboard); sendMessage($chat_id, "testt2", $keyboard); break; 

those. now when you press the button nothing is displayed

  • How do you get updates via webcam? - Anatol
  • Yes, via webbooks - batman
  • You can solve it via InlineKeyboardMarkup , without ForceReply . - Anatol
  • thought about it, but so far I just figured out with a regular keyboard - batman

1 answer 1

Here is the working code, if anyone needs it.

 <?php $access_token = 'xxx'; $api = 'https://api.telegram.org/bot' . $access_token; $output = json_decode(file_get_contents('php://input'), TRUE); $chat_id = $output['message']['chat']['id']; $message = $output['message']['text']; $callback_query = $output['callback_query']; $data = $callback_query['data']; $message_id = ['callback_query']['message']['message_id']; $chat_id_in = $callback_query['message']['chat']['id']; switch($message) { case '/test': $inline_button1 = array("text"=>"Google url","url"=>"http://google.com"); $inline_button2 = array("text"=>"work plz","callback_data"=>'/plz'); $inline_keyboard = [[$inline_button1,$inline_button2]]; $keyboard=array("inline_keyboard"=>$inline_keyboard); $replyMarkup = json_encode($keyboard); sendMessage($chat_id, "ok", $replyMarkup); break; } switch($data){ case '/plz': sendMessage($chat_id_in, "plz"); break; } function sendMessage($chat_id, $message, $replyMarkup) { file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup); } 
  • Respect Thank you very much, such as you and more would be in nete, not that smart people, that they put on themselves the importance and paint the abstruse code, where the devil himself will break his leg. - user228772
  • Tell me, please, what should happen when you click on plz? where the sendMessage ($ chat_id_in, "plz") message should go; - why is there $ chat_id_in, not $ chat_id? - moonvader
  • When you click on plz, the chat will write 'plz'. This will be equivalent to the command '/ plz'. $ Chat_id_in is used because the '/ plz' command should be executed after receiving responses from the '/ test' command ($ chat_id). This builds a chain of commands. If you immediately enter '/ plz', then nothing will happen (it will only happen after the '/ test' command. Something like that. - batman