Send Telegram msg and save logs for alert response
This commit is contained in:
parent
3fa489c94b
commit
3194f22673
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class AlertClosed extends Exception
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class AlertResponseAlreadySet extends Exception
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
|
@ -4,9 +4,11 @@ namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\Alert;
|
use App\Models\Alert;
|
||||||
use App\Models\AlertCrew;
|
use App\Models\AlertCrew;
|
||||||
use Illuminate\Http\Request;
|
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Utils\Alerts;
|
||||||
|
use App\Exceptions\AlertClosed;
|
||||||
|
use App\Exceptions\AlertResponseAlreadySet;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class AlertController extends Controller
|
class AlertController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -160,26 +162,18 @@ class AlertController extends Controller
|
||||||
*/
|
*/
|
||||||
public function setResponse(Request $request, $id)
|
public function setResponse(Request $request, $id)
|
||||||
{
|
{
|
||||||
$alert = Alert::find($id);
|
try {
|
||||||
if($alert->closed) {
|
Alerts::updateAlertResponse($id, $request->input('response'));
|
||||||
|
} catch(AlertClosed $e) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => 'L\'allertamento è stata chiusa.',
|
'message' => 'La chiamata è stata chiusa.',
|
||||||
|
], 400);
|
||||||
|
} catch(AlertResponseAlreadySet $e) {
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => 'Hai già risposto a questa chiamata.',
|
||||||
], 400);
|
], 400);
|
||||||
}
|
|
||||||
|
|
||||||
foreach($alert->crew as $crew) {
|
|
||||||
if($crew->user->id == auth()->user()->id) {
|
|
||||||
if($crew->accepted != null) {
|
|
||||||
return response()->json([
|
|
||||||
'status' => 'error',
|
|
||||||
'message' => 'Hai già risposto a questo allertamento.',
|
|
||||||
], 400);
|
|
||||||
} else {
|
|
||||||
$crew->accepted = $request->input('response', $crew->accepted);
|
|
||||||
$crew->save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Utils;
|
||||||
|
|
||||||
|
use App\Models\Alert;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\TelegramBotNotifications;
|
||||||
|
use App\Models\TelegramBotLogins;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
$alert = Alert::find($alertId);
|
||||||
|
if($alert->closed) {
|
||||||
|
throw new AlertClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(is_null($userId)) {
|
||||||
|
$userId = auth()->user()->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($alert->crew as $crew) {
|
||||||
|
if($crew->user->id == $userId) {
|
||||||
|
if($crew->accepted != null) {
|
||||||
|
throw new AlertResponseAlreadySet();
|
||||||
|
} else {
|
||||||
|
$crew->accepted = $response;
|
||||||
|
$crew->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = User::find($userId);
|
||||||
|
|
||||||
|
//Add to logs
|
||||||
|
Logger::log(
|
||||||
|
"Risposta ad allertamento: ".($response ? "presente" : "non presente"),
|
||||||
|
$user,
|
||||||
|
$fromTelegram ? $user : null,
|
||||||
|
$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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue