Move Telegram send message functions to util
This commit is contained in:
parent
a8e28d3679
commit
a41812b848
|
@ -10,9 +10,7 @@ use Illuminate\Queue\SerializesModels;
|
|||
|
||||
use App\Models\ScheduleSlots;
|
||||
use App\Models\User;
|
||||
use App\Models\TelegramBotNotifications;
|
||||
|
||||
use DefStudio\Telegraph\Facades\Telegraph;
|
||||
use App\Utils\TelegramBot;
|
||||
|
||||
class UpdateAvailabilityWithSchedulesJob implements ShouldQueue
|
||||
{
|
||||
|
@ -94,21 +92,7 @@ class UpdateAvailabilityWithSchedulesJob implements ShouldQueue
|
|||
} else if($available_users_count_before < 5 && $available_users_count_after >= 5) {
|
||||
$text = "🚒 Distaccamento operativo con squadra completa";
|
||||
}
|
||||
if(!is_null($text)) {
|
||||
//Find message hash
|
||||
$hash = md5($text);
|
||||
|
||||
$chat_ids = TelegramBotNotifications::where("type_team_state", true)
|
||||
->whereNot("last_message_hash", $hash)
|
||||
->get()->pluck('chat_id')->toArray();
|
||||
|
||||
foreach ($chat_ids as $chat_id) {
|
||||
$chat = Telegraph::chat($chat_id);
|
||||
$chat->message($text)->send();
|
||||
TelegramBotNotifications::where("chat_id", $chat_id)
|
||||
->update(["last_message_hash" => $hash]);
|
||||
}
|
||||
}
|
||||
TelegramBot::sendTeamMessage($text);
|
||||
}
|
||||
|
||||
public function failed(\Error|\TypeError $exception = null)
|
||||
|
|
|
@ -4,12 +4,10 @@ namespace App\Utils;
|
|||
|
||||
use App\Models\Alert;
|
||||
use App\Models\User;
|
||||
use App\Models\TelegramBotNotifications;
|
||||
use App\Models\TelegramBotLogins;
|
||||
use App\Utils\TelegramBot;
|
||||
use App\Utils\Logger;
|
||||
use App\Exceptions\AlertClosed;
|
||||
use App\Exceptions\AlertResponseAlreadySet;
|
||||
use DefStudio\Telegraph\Facades\Telegraph;
|
||||
|
||||
class Alerts {
|
||||
public static function updateAlertResponse($alertId, $response, $userId = null, $fromTelegram = false)
|
||||
|
@ -44,25 +42,11 @@ class Alerts {
|
|||
$fromTelegram ? "telegram" : "web"
|
||||
);
|
||||
|
||||
//Send message to the user via Telegram to notify the response
|
||||
$chatRows = TelegramBotLogins::join("users", "users.id", "=", "telegram_bot_logins.user")
|
||||
->select("users.id", "chat_id", "users.available")
|
||||
->where("users.id", $userId)
|
||||
->whereNotNull("chat_id")
|
||||
->get();
|
||||
|
||||
foreach ($chatRows as $chatRow) {
|
||||
//Get chat by id
|
||||
$chat = Telegraph::chat($chatRow["chat_id"]);
|
||||
|
||||
$chat
|
||||
->message(
|
||||
"La tua risposta all'allertamento è stata registrata.\n".
|
||||
"Sei <b>".($response ? "presente" : "assente")."</b>.\n".
|
||||
"Rimani in attesa di nuove istruzioni."
|
||||
)
|
||||
->send();
|
||||
}
|
||||
|
||||
TelegramBot::sendMessageToUser(
|
||||
$userId,
|
||||
"La tua risposta all'allertamento è stata registrata.\n".
|
||||
"Sei <b>".($response ? "presente" : "assente")."</b>.\n".
|
||||
"Rimani in attesa di nuove istruzioni."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
namespace App\Utils;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\TelegramBotNotifications;
|
||||
use DefStudio\Telegraph\Facades\Telegraph;
|
||||
use App\Utils\TelegramBot;
|
||||
use App\Utils\Logger;
|
||||
|
||||
class Availability {
|
||||
|
@ -43,21 +42,7 @@ class Availability {
|
|||
} else if($available_users_count_before == 1 && $available) {
|
||||
$text = "🧯 Distaccamento operativo per supporto";
|
||||
}
|
||||
if(!is_null($text)) {
|
||||
//Find message hash
|
||||
$hash = md5($text);
|
||||
|
||||
$chat_ids = TelegramBotNotifications::where("type_team_state", true)
|
||||
->whereNot("last_message_hash", $hash)
|
||||
->get()->pluck('chat_id')->toArray();
|
||||
|
||||
foreach ($chat_ids as $chat_id) {
|
||||
$chat = Telegraph::chat($chat_id);
|
||||
$chat->message($text)->send();
|
||||
TelegramBotNotifications::where("chat_id", $chat_id)
|
||||
->update(["last_message_hash" => $hash]);
|
||||
}
|
||||
}
|
||||
TelegramBot::sendTeamMessage($text);
|
||||
|
||||
Logger::log(
|
||||
"Disponibilità cambiata in ".($available ? "disponibile" : "non disponibile"),
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Models\TelegramBotNotifications;
|
||||
use App\Models\TelegramBotLogins;
|
||||
use DefStudio\Telegraph\Facades\Telegraph;
|
||||
|
||||
class TelegramBot {
|
||||
static public function sendMessageToUser($userId, callable|string $message) {
|
||||
$chatRows = TelegramBotLogins::join("users", "users.id", "=", "telegram_bot_logins.user")
|
||||
->select("users.id", "chat_id", "users.available")
|
||||
->where("users.id", $userId)
|
||||
->whereNotNull("chat_id")
|
||||
->get();
|
||||
|
||||
foreach ($chatRows as $chatRow) {
|
||||
//Get chat by id
|
||||
$chat = Telegraph::chat($chatRow["chat_id"]);
|
||||
|
||||
if(gettype($message) == "string") {
|
||||
$chat->message($message)->send();
|
||||
} else {
|
||||
$message($chat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static public function sendTeamMessage(callable|string|null $message) {
|
||||
$msgParamType = gettype($message);
|
||||
if($msgParamType == "string") {
|
||||
if($message == "") return;
|
||||
$hash = md5($message);
|
||||
$chat_ids = TelegramBotNotifications::where("type_team_state", true)
|
||||
->whereNot("last_message_hash", $hash)
|
||||
->get()->pluck('chat_id')->toArray();
|
||||
} else if($msgParamType == "NULL") {
|
||||
return;
|
||||
} else {
|
||||
$chat_ids = TelegramBotNotifications::where("type_team_state", true)
|
||||
->get()->pluck('chat_id')->toArray();
|
||||
}
|
||||
|
||||
foreach ($chat_ids as $chat_id) {
|
||||
$chat = Telegraph::chat($chat_id);
|
||||
|
||||
if(gettype($message) == "string") {
|
||||
$chat->message($message)->send();
|
||||
TelegramBotNotifications::where("chat_id", $chat_id)
|
||||
->update(["last_message_hash" => $hash]);
|
||||
} else {
|
||||
$message($chat);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue