55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../telnyx/config.php';
|
|
|
|
// Set up Telnyx API
|
|
\Telnyx\Telnyx::setApiKey(TELNYX_API_KEY);
|
|
|
|
// Get all bookings for the next 24 hours
|
|
$stmt = db()->prepare("SELECT b.*, c.name as client_name, c.phone as client_phone FROM bookings b JOIN clients c ON b.client_id = c.id WHERE b.start_time BETWEEN NOW() AND NOW() + INTERVAL 24 HOUR AND b.status = 'confirmed'");
|
|
$stmt->execute();
|
|
$bookings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
foreach ($bookings as $booking) {
|
|
if (empty($booking['client_phone'])) {
|
|
continue;
|
|
}
|
|
|
|
$message = "Hi " . $booking['client_name'] . ", this is a reminder for your upcoming session tomorrow at " . date("g:i a", strtotime($booking['start_time'])) . ".";
|
|
|
|
try {
|
|
$response = \Telnyx\Message::create([
|
|
'from' => TELNYX_MESSAGING_PROFILE_ID,
|
|
'to' => $booking['client_phone'],
|
|
'text' => $message,
|
|
]);
|
|
|
|
// Log the message
|
|
$log_stmt = db()->prepare("INSERT INTO sms_logs (client_id, sender, recipient, message, status) VALUES (?, ?, ?, ?, ?, ?)");
|
|
$log_stmt->execute([
|
|
$booking['client_id'],
|
|
TELNYX_MESSAGING_PROFILE_ID,
|
|
$booking['client_phone'],
|
|
$message,
|
|
'sent'
|
|
]);
|
|
|
|
echo "Reminder sent to " . $booking['client_name'] . "\n";
|
|
|
|
} catch (\Telnyx\Exception\ApiErrorException $e) {
|
|
echo "Error sending reminder to " . $booking['client_name'] . ": " . $e->getMessage() . "\n";
|
|
|
|
// Log the failure
|
|
$log_stmt = db()->prepare("INSERT INTO sms_logs (client_id, sender, recipient, message, status) VALUES (?, ?, ?, ?, ?, ?)");
|
|
$log_stmt->execute([
|
|
$booking['client_id'],
|
|
TELNYX_MESSAGING_PROFILE_ID,
|
|
$booking['client_phone'],
|
|
$message,
|
|
'failed'
|
|
]);
|
|
}
|
|
}
|
|
|