E
E
Evgeny2021-08-26 17:08:01
PHP
Evgeny, 2021-08-26 17:08:01

How to display messages after clicking on a button, callback_data?

I wrote a small bot that sends a message with buttons, when you click on the button, another message should be displayed

<?php

include 'config.php'; // Токен бота
include 'functions.php'; // Основные функции

$update = json_decode(file_get_contents('php://input'), JSON_OBJECT_AS_ARRAY);

$chat_id = $update['message']['chat']['id']; // Определяет ID чата
$message = $update['message']['text']; // Определяет текст сообщения
$message_id = ['callback_query']['message']['message_id']; // Определяет ID сообщения

$callback_query = $update['callback_query'];
$data = $callback_query['data'];

// Делается запрос
$method = 'setWebhook';
$url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/' . $method;
$options = [
    'url' => 'https://bot12345.ru/bot.php'
];

// То, что возвращается из запроса
$response = file_get_contents($url . '?' . http_build_query($options));
var_dump($response);

// Команда /start
if (strpos($message, "/start") === 0) {
    $post = [
        'chat_id' => $chat_id,
        'text' => 'Привет! Я еще нахожусь на этапе разработки!'
    ];
    sendRequest('sendMessage', $post);
} else {
// Начальное сообщение
    $post = [
        'chat_id' => $chat_id,
        'text' => 'Давай определимся с категорией:',
        $main_menu = [
            ,
            ,
            
        ],
        'reply_markup' => inline_keyboard($main_menu)
    ];
    sendRequest('sendMessage', $post);
}

switch ($data) {
    case 'theme_1':
        $post = [
            'chat_id' => $chat_id,
            'text' => 'aaa',
            $Inline = [
                ,
                
            ],
            'reply_markup' => inline_keyboard($Inline),
            sendRequest('sendMessage', $post)
        ];
        break;
}


When you click on the first button to which the answer is attached, nothing happens. I understand that I need to somehow get the callback_data and send a new message

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aruy137, 2021-08-28
@Aruy137

To process a click, you must use:
mesg = bot.send_message("ID", "TEXT", reply_markup=keyboard)) - the message itself with the buttons must be entered into the variable
msg = bot.register_next_step_handler(mesg, "FUNC") - then after the message you need to use mesq in the new message bot.register_next_step_handler (it starts a new function)
@bot.callback_query_handler(func=lambda message: True) - it reads button clicks
def choice_for_four(message):
message.data - the click text is contained in a new variable, and to access callback_data use data
Programming in python, so I tried to describe everything in words

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question