Files
api/services/nginx/app/classes/slack.php
T
Jepp9350 56fa77be11 Remove unnecessary return statement in slack.php
The return statement was redundant as the function naturally concludes without it. Removing it improves code clarity and eliminates dead code.
2025-04-22 15:47:57 +02:00

131 lines
5.1 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())->select((int)$department_id);
$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 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 "*Ny booking oprettet* ( ID: " . $id . " )\n"
. "Kunde: $customer_name ($customer_number)\n"
. "Type: $wash_type\n"
. "Reference nummer: $reference_number\n"
. "RegNr Traekker: $regNrTraekker\n"
. "RegNr Trailer: $regNrTrailer\n"
. "Dato: $date\n"
. "Hentning: $pickup_bool\n"
. "Noter: $notes";
}
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));
}
}