E
E
Evgeny2022-04-15 15:21:11
PHP
Evgeny, 2022-04-15 15:21:11

Why doesn't the language change in the bot?

I have a file where two arrays are stored - texts in Russian and English. Changing the language is done via inline buttons. The bot saves the selected language for each user in the database (the default is Russian), it is stored there in a separate column with the boolean data type and gets it every time it sends a message (takes out either 0 or 1).

For normal commands, the text is easily changed to the selected one and displayed, but there were difficulties with pop-up windows in the inline mode

. The code itself:

// Поменять язык на Английский
function english_language($user_id)
{
    global $pdo;
    $sql = "UPDATE users
        SET language = 1
        WHERE user_id = :user_id";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
    $stmt->execute();
}

// Поменять язык на Русский
function russian_language($user_id)
{
    global $pdo;
    $sql = "UPDATE users
        SET language = 0
        WHERE user_id = :user_id";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
    $stmt->execute();
}

// Достать id языка, используемого юзером
function get_language($user_id)
{
    global $pdo;
    $sql = "SELECT language
        FROM users
        WHERE user_id = :user_id";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
    $stmt->execute();
    return $stmt->fetch();
}

include 'secret_texts.php'; // Здесь находятся массивы с текстом на разных языках

$language = get_language($user_id);

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

$inline_query = $data['inline_query'];
$inline_query_id = $data['inline_query']['id'];

if (isset($inline_query)) { // Обработка инлайн запроса
        $answer_inline_query = [
            "inline_query_id" => $inline_query_id,
            "results" => '',
        ];

        if (preg_match('/@\w{5,}/', $inline_query_msg)) {

            if ($language['language'] == 0) {
                $response = sendRequest('answerInlineQuery?inline_query_id=' . $inline_query_id . '&cache_time=' . null . '&results=' . json_encode(
                        [
                            [
                                "type" => "article",
                                "id" => "2",
                                "title" => $russian_language['send_title'],
                                "description" => $russian_language['send_description'],
                                $secret_button = [],
                                "reply_markup" => json_decode(inline_keyboard($secret_button)),
                                "input_message_content" => [
                                    "message_text" => $russian_language['send_text_1'] . $inline_receiver_username . $russian_language['send_text_2']
                                ],
                                "thumb_url" => "jpg",
                                "photo_width" => "512",
                                "photo_height" => "512"
                            ]
                        ]
                    ));
            } elseif ($language['language'] == 1) {
                $response = sendRequest('answerInlineQuery?inline_query_id=' . $inline_query_id . '&cache_time=' . null . '&results=' . json_encode(
                        [
                            [
                                "type" => "article",
                                "id" => "2",
                                "title" => $english_language['send_title'],
                                "description" => $english_language['send_description'],
                                $secret_button = [],
                                "reply_markup" => json_decode(inline_keyboard($secret_button)),
                                "input_message_content" => [
                                    "message_text" => $english_language['send_text_1'] . $inline_receiver_username . $english_language['send_text_2']
                                ],
                                "thumb_url" => "jpg",
                                "photo_width" => "512",
                                "photo_height" => "512"
                            ]
                        ]
                    ));
            }
        }
    }


In any case, the bot displays the text only from the first condition (in my case, in Russian, if I put English first, then it will be displayed accordingly)

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