timestamp = strtotime('+15 minutes'); parent::__construct(); } public function run() { /** * @var Chatbot[] $chatbots */ $chatbots = Chatbot::all(); foreach ($chatbots as $chatbot) { Log::info('Checking idle chats', [ 'chatbot' => $chatbot, ]); $enabled = Arr::get($chatbot->settings, 'notifications.after_idle.enabled', false); if (!$enabled) return; $idleTime = Arr::get($chatbot->settings, 'notifications.after_idle.timeout', 0); /** * @var ChatbotChat[]|Collection $chats */ $chats = $chatbot->chats() ->where('created_at', '>=', Carbon::now()->subMinutes(30)) ->where('notified', false) ->whereDoesntHave('messages', function ($query) use ($idleTime) { $query ->where('role', 'user') ->where('created_at', '>=', Carbon::now()->subMinutes($idleTime)); }) ->withCount('messages') ->get(); Log::info('Idle chats', [ 'chats' => $chats ]); foreach ($chats as $chat) { if ($chat->messages_count === 0) continue; $chat->notify(); } } } }