E
E
Evgeny2022-02-01 19:33:32
PHP
Evgeny, 2022-02-01 19:33:32

How can I check when getting the required user_id from the database in Telegram?

I have a telegram bot that retrieves data from gitlab, to do this it has files connected to the gitlab webhook, respectively, telegram variables like user_id, username, etc., do not see these files. Since I need to send messages from these files to telegrams, it was decided to write functions for the database that save the user_id and send messages using it (It used to work if you substitute the user's direct ID)

The problem is that I don't know how to do the checks when receiving a user_id, so that each user receives their own messages If, when receiving the same user_id in the file with the telegram webhook, I checked the user_id in the database and in the chat with the user, then in this case I don’t know what to do

// Регистрация новых пользователей
function notification_user_login($user_id, $username)
{
    global $pdo;
    $sql = "INSERT INTO `notification_users` (user_id, username)
    VALUES (:user_id, :username)";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
    $stmt->bindParam(':username', $username, PDO::PARAM_STR);
    $stmt->execute();
    return true;
}

// Получение user_id
function select_notification_user()
{
    global $pdo;
    $sql = "SELECT user_id
        FROM notification_users
        ORDER BY id DESC LIMIT 1";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    return $stmt->fetch();
}


In the second function, I get the last id of the user_id string just to check the functionality of the function

include 'database.php'; // Файл с БД

$input = file_get_contents("php://input");
$json = json_decode($input);

$user = select_notification_user();

// То, что отправляет GitLab
if ($json->object_kind == 'push') { // Если пришел Push event

    $ref = $json->ref;
    $project_name = $json->project->name;
    $name = $json->user_name;
    $username = $json->user_username;

    $post = [
        'chat_id' => $user['user_id'],
        'text' => ' <b>Webhook caught push event!</b>' . "\n\n" . '<b>Ref:</b> ' . $ref . "\n"
            . '<b>Name:</b> ' . $name . "\n" . '<b>Username:</b> ' . $username . "\n" . '<b>Project name:</b> ' . $project_name,
        'parse_mode' => 'html'
    ];
    sendRequest('sendMessage', $post);
}

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