Untitled
Anonymous
plain_text
03/01/2026 2:15 PM
6.9 KB
11
Indexable
AIAGentWSController
<?php
namespace Modules\AIAgent\App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\AIAgent\Models\ai_agent;
use Modules\AIAgent\Models\AiAgentWs;
use Modules\FacebookWhatsapp\Http\Models\WhatsappAccount;
use Modules\Whatsapp\Models\ws_accounts;
class AIAgentWsController extends Controller
{
public function toggleIntegration(Request $request)
{
$request->validate([
'agent_id' => 'required|integer',
'ws_account_id' => 'required|integer',
'type' => 'required|string|in:web,api',
'enabled' => 'required|boolean',
]);
$agent = ai_agent::find($request->agent_id);
// Use correct model based on type
if ($request->type === 'api') {
$account = WhatsappAccount::find($request->ws_account_id);
} else {
$account = ws_accounts::find($request->ws_account_id);
}
if (! $agent || ! $account) {
return response()->json([
'status' => 'error',
'message' => 'Invalid AI Agent or WhatsApp account ID.',
], 404);
}
if ($request->enabled) {
// Update account based on type
if ($request->type === 'api') {
$account->ai_auto_reply = 1;
} else {
$account->ai_auto_reply = 1;
}
$account->save();
} else {
// Disable integration
$account->ai_auto_reply = 0;
$account->save();
}
// Update or create profile integration
$profile = AiAgentWs::updateOrCreate(
[
'agent_id' => $agent->id,
'ws_account_id' => $account->id,
'type' => $request->type,
],
[
'status' => $request->enabled,
]
);
return response()->json([
'status' => 'success',
'message' => $request->enabled
? 'AI Agent successfully integrated with WhatsApp profile.'
: 'AI Agent integration disabled.',
]);
}
public function getIntegrations($agentId)
{
$profiles = AiAgentWs::where('agent_id', $agentId)
->get()
->map(function ($profile) {
// Load relationship based on type
if ($profile->type === 'api') {
$account = WhatsappAccount::select('id', 'display_name as name', 'phone_number', 'status', 'timezone')
->find($profile->ws_account_id);
} else {
$account = ws_accounts::select('id', 'name', 'username', 'status', 'avatar')
->find($profile->ws_account_id);
}
return [
'id' => $profile->id,
'agent_id' => $profile->agent_id,
'profile_id' => $profile->ws_account_id,
'type' => $profile->type,
'status' => $profile->status,
'whatsappAccount' => $account,
];
});
return response()->json([
'status' => 'success',
'data' => $profiles,
]);
}
public function updateIntegrations(Request $request)
{
$validated = $request->validate([
'agent_id' => 'required|integer',
'type' => 'required|string|in:web,api',
'profile_ids' => 'array',
]);
$agentId = $validated['agent_id'];
$type = $validated['type'];
$profileIds = $validated['profile_ids'] ?? [];
try {
if ($type === 'api') {
$validIds = WhatsappAccount::whereIn('id', $profileIds)->pluck('id')->toArray();
} else {
$validIds = ws_accounts::whereIn('id', $profileIds)->pluck('id')->toArray();
}
if (count($validIds) !== count($profileIds)) {
return response()->json([
'status' => 'error',
'message' => 'Some profile IDs are invalid for the specified type.',
], 422);
}
// Remove old integrations for this agent & type
AiAgentWs::where('agent_id', $agentId)
->where('type', $type)
->delete();
// Add new integrations
foreach ($profileIds as $pid) {
AiAgentWs::create([
'agent_id' => $agentId,
'ws_account_id' => $pid,
'type' => $type,
'status' => 1,
]);
}
return response()->json([
'status' => 'success',
'message' => 'Integrations updated successfully.',
]);
} catch (\Exception $e) {
return response()->json([
'status' => 'error',
'message' => 'Failed to update integrations.',
'error' => $e->getMessage(),
], 500);
}
}
}
Editor is loading...
Leave a Comment