E
E
Evgeny2022-04-19 22:43:40
PHP
Evgeny, 2022-04-19 22:43:40

Why does inline bot in telegram not work with different users?

The bot works in online mode, sends messages in groups to a specific user.

To view the message, you need to click on the button, where in advance, even when sending, the unique id and username of the specified person in the group is recorded in the callback_data

$secret_button = [
    
]


The bot writes these id and username of the recipient to the database, as well as the username of the person who sent and the message itself.

When you click on the "View" button, the bot gets this data and checks whether the person opens the message

$data = json_decode(file_get_contents('php://input'));
$username = $data->callback_query->from;->username;
$callback_data = $data->callback_query->data;

function message_get($username, $callback_data)
{
    global $pdo;
    $sql = "SELECT message, receiver_id, username, id
        FROM secret_messages
        WHERE receiver_id = :username
        AND id = :callback_data";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':username', $username, PDO::PARAM_STR);
    $stmt->bindParam(':callback_data', $callback_data, PDO::PARAM_STR);
    $stmt->execute();
    return $stmt->fetch();
}

$receiver_username = explode('-', $callback_data);

$messageData = message_get($username, $receiver_username[1]); // Получение запроса из бд

if ($messageData['receiver_id'] == $receiver_username[0]) { // Если username получателя совпал с тем, что был в запросе
    $alert_message = [
        'callback_query_id' => $callback_query_id,
        'text' => $messageData['message'],
        'show_alert' => TRUE
    ];
    sendRequest('answerCallbackQuery', $alert_message);
}


When a user who is registered in the bot (that is, was entered in the database) sends a message to someone, the recipient can easily open it, but if the user writes without registering in the database, the bot cannot get the recipient's username and unique id

For example, I I send a message to a user without registration:

625f10a7af7e4889799225.png

First, the id is shown, then the username specified in inline, the username when the button is clicked and, accordingly, the sender

The second and last one is not displayed for some reason

Then I receive a message from an unregistered user:

625f10bbe9b10884987319.png

1, 2 and 4 fields are missing

And finally, I send a message to the registrant:

625f10d2e4bd3934996613.png

All data is safely received

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question