File manager - Edit - /home/autoph/public_html/projects/aha-api/app/Jobs/SyncConversations.php
Back
<?php namespace App\Jobs; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Queue\Queueable; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Http; use Carbon\Carbon; use Illuminate\Support\Facades\Log; use GuzzleHttp\Client; use Google\Auth\CredentialsLoader; use Google\Auth\ApplicationDefaultCredentials; use DB; use App\Models\AiConversations; class SyncConversations implements ShouldQueue { use Dispatchable, InteractsWithQueue, SerializesModels; public $queue = 'sync_conversations'; // public $jwtToken = ''; protected ?string $pageToken = null; // Pagination token /** * Create a new job instance. */ public function __construct(?string $pageToken = null) { $this->pageToken = $pageToken ?? null; // Store the pagination token // $this->jwtToken = $this->jwtTocket ? env('JWT_SECRET'); } /** * Execute the job. */ public function handle(): void { // Log::info('[SyncConversations] Execution started at: ' . now()); Log::channel('chatbot_log')->info('[SyncChatbotLogsJob] Execution started at: ' . now()); $projectId = 'autohub-assist'; $accessToken = $this->getAccessToken(); if (!$accessToken) { Log::error('❌ Failed to retrieve access token.'); return; } $url = "https://logging.googleapis.com/v2/entries:list"; $dateNow = date('Y-m-d\TH:i:s\Z'); $payload = [ 'resourceNames' => ["projects/{$projectId}"], 'filter' => 'resource.labels.project_id="'.$projectId.'"', 'orderBy' => 'timestamp desc', 'pageSize' => 10000 ]; if ($this->pageToken ?? null) { unset($payload['filter']); $payload['pageToken'] = $this->pageToken; } $response = Http::withHeaders([ 'Authorization' => 'Bearer ' . $accessToken, 'Content-Type' => 'application/json' ])->post($url, $payload); $status = $response->status(); $responseData = $response->json(); // Automatically decodes JSON // Extract chat history $chatHistory = []; if (!empty($responseData['entries'])) { foreach ($responseData['entries'] as $entry) { $queryResult = $entry['jsonPayload']['queryResult'] ?? null; $timeStamp = $entry['timestamp'] ?? null; $recievedTimeStatmp = $entry['receiveTimestamp'] ?? null; $labelInfo = $entry['labels'] ?? null; if($labelInfo){ $location_id = $labelInfo['location_id'] ?? null; $sessionID = $labelInfo['session_id'] ?? null; $agentID = $labelInfo['agent_id'] ?? null; } if ($queryResult) { $userMessage = $queryResult['match']['resolvedInput'] ?? null; $botResponses = $queryResult['responseMessages'] ?? []; $botMessages = []; foreach ($botResponses as $response) { if (!empty($response['text']['text'])) { $botMessages[] = implode(" ", $response['text']['text']); // $botMessages = implode(" ", $response['text']['text']); } } $timeStamp= Carbon::parse($timeStamp)->format('Y-m-d H:i:s'); $data = [ 'user_message' => $userMessage, 'bot_response' => json_encode($botMessages), // 'info' => [$sessionID,$location_id,$agentID,$timeStamp,$recievedTimeStatmp], 'session_id' => $sessionID, 'location_id' => $location_id, 'agent_id' => $agentID, 'timestamp' => $timeStamp, 'received_timestamp' => $recievedTimeStatmp ]; // AiConversations::create( // $data // ); DB::transaction(function () use ($data, $sessionID, $timeStamp) { AiConversations::updateOrCreate( ['session_id' => $sessionID,'timestamp'=>$timeStamp], $data ); }); Log::channel('chatbot_log')->info($data); } } } DB::disconnect(); Log::channel('chatbot_log')->info('[SyncConversations] Execution Ended at: ' . now()); } private function getAccessToken(){ // $credentialsPath = storage_path('autohub-assist-ff9a5fe0ee23.json'); // Path to service account // $credentialsPath = env('GOOGLE_APPLICATION_CREDENTIALS'); $credentialsPath = base_path('storage/autohub-assist-ff9a5fe0ee23.json'); putenv("GOOGLE_APPLICATION_CREDENTIALS={$credentialsPath}"); $client = new Client(); $scope = 'https://www.googleapis.com/auth/cloud-platform'; $creds = ApplicationDefaultCredentials::getCredentials($scope); $token = $creds->fetchAuthToken(); return $token['access_token'] ?? null; } }
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0 |
proxy
|
phpinfo
|
Settings