connect_error) { die("Connection failed: " . $conn->connect_error); } // Handle incoming messages $bot->on(function($update) use ($bot, $conn) { // Get the message text and phone number $message = $update->getMessage(); $text = $message->getText(); $phone_number = $message->getContact()->getPhoneNumber(); // Check if the phone number is stored in the database $sql = "SELECT * FROM phone_numbers WHERE phone_number = '$phone_number'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // Save the message in the database $sql = "INSERT INTO messages (phone_number, message) VALUES ('$phone_number', '$text')"; if ($conn->query($sql) === TRUE) { $bot->sendMessage($message->getChat()->getId(), "Message saved successfully!"); } else { $bot->sendMessage($message->getChat()->getId(), "Error: " . $sql . "
" . $conn->error); } } else { $bot->sendMessage($message->getChat()->getId(), "Phone number not found in the database!"); } }, function($update) { return true; }); // Start the bot $bot->run();