Files
api/classes/slack.php
T
Jepp9350 d3e9391160 Add new cron tasks and enhance user synchronization logic
Introduced cron tasks for syncing user economic customer details and discounts. Enhanced caching mechanisms for users and departments, added Slack messaging capabilities, and updated CLI/script routing for better flexibility. New routes and utility methods improve cron execution and logging.
2025-01-24 13:04:10 +01:00

139 lines
5.4 KiB
PHP

<?php
namespace classes;
use GuzzleHttp\Client;
use interfaces\notification_i;
use objects\departments_o;
use objects\users_o;
use traits\notification_t;
class slack implements notification_i
{
use notification_t;
/**
* @inheritdoc
* @throws \Exception
*/
public function send_department_booking_notification(int $department_id, $message): self
{
// Get the departments webhook
$webhook = self::get_department_webhook($department_id);
// Check if the webhook is empty
if (empty($webhook)) {
throw new \Exception('Department webhook is empty');
}
// Send the notification to the department
self::add_log(self::send_webhook_message($message, $webhook));
return $this;
}
private function get_department_webhook(int $department_id): string|null
{
// Check if the department webhook is cached
$webhook = redis->get_department_webhook($department_id);
// If the department webhook is not cached, get it from the database
if ($webhook === null) {
$department = new departments_o();
$department->id = $department_id;
$department->getObjectProperties();
$webhook = $department->slack_webhook->value();
// Cache the department webhook (If it is not empty or null)
if (!empty($webhook)) {
redis->cache_department_webhook($department_id, $webhook);
}
return null;
}
return redis->get_department_webhook($department_id);
}
/**
* Send a message to a slack webhook
* @param string $message
* @param string $webhook
* @return string The response from the webhook, unparsed
*/
protected function send_webhook_message(string $message, string $webhook): string
{
global $DEBUG;
try {
// Initialize Guzzle client
$client = new Client();
// Prepare the payload for the webhook
$payload = [
'text' => $message
];
// Send POST request to the webhook
$response = $client->post($webhook, [
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode($payload),
'verify' => ($DEBUG === false) // Disable SSL verification (Only in development)
]);
// Return success message
return 'Message sent successfully. Response: ' . $response->getBody();
} catch (\Exception $e) {
// Handle any errors that occur
return 'Failed to send message: ' . $e->getMessage();
}
}
public function format_new_booking($id, $customer_number, string $wash_type, string $contact_email, string $reference_number, string $regNrTraekker, string $regNrTrailer, string $washCertificateEmail, string $date, int $department, $pickup_bool, string $notes, string $washCertificateStatus, string $washCertificateUrl, string $status): string
{
// Get the department name
$department_name = (new departments_o())->getDepartmentName($department);
// Get the customer name
$customer_name = (new users_o())->getCustomerName($customer_number);
// Format the message
return "*New booking created* ( ID: " . $id . " )\n"
. "Customer: $customer_name\n"
. "Customer number: $customer_number\n"
. "Wash type: $wash_type\n"
. "Contact email: $contact_email\n"
. "Reference number: $reference_number\n"
. "RegNr Traekker: $regNrTraekker\n"
. "RegNr Trailer: $regNrTrailer\n"
. "Wash certificate email: $washCertificateEmail\n"
. "Date: $date\n"
. "Department: $department_name\n"
. "Pickup: $pickup_bool\n"
. "Notes: $notes\n"
. "Status: $status";
}
public function format_unfulfilled_booking($id, $customer_number, string $wash_type, string $contact_email, string $reference_number, string $regNrTraekker, string $regNrTrailer, string $washCertificateEmail, string $date, int $department, $pickup_bool, string $notes, string $washCertificateStatus, string $washCertificateUrl, string $status): string
{
// Get the department name
$department_name = (new departments_o())->getDepartmentName($department);
// Get the customer name
$customer_name = (new users_o())->getCustomerName($customer_number);
// Format the message
return "Unfulfilled booking from " . $date . "\n"
. "ID: $id\n"
. "Customer: $customer_name\n"
. "Customer number: $customer_number\n"
. "Wash type: $wash_type\n"
. "Contact email: $contact_email\n"
. "Reference number: $reference_number\n"
. "RegNr Traekker: $regNrTraekker\n"
. "RegNr Trailer: $regNrTrailer\n"
. "Wash certificate email: $washCertificateEmail\n"
. "Date: $date\n"
. "Department: $department_name\n"
. "Pickup: $pickup_bool\n"
. "Notes: $notes\n"
. "Status: $status";
}
public function send_message(string $string): void
{
global $SLACK_DEFAULT_WEBHOOK;
// Send the message to the slack webhook
self::add_log(self::send_webhook_message($string, $SLACK_DEFAULT_WEBHOOK));
}
}