Skip to content

Commit 15f4108

Browse files
Add WhatsApp Widget with logging and agent management
This commit introduces the WhatsApp Widget functionality, including support for logging interactions, managing agents, and API endpoints. Key features include model creation, database migrations, request validation, and backend controllers to handle widget-related operations seamlessly. Configuration options and secure request handling are also included.
1 parent f555df6 commit 15f4108

11 files changed

+237
-0
lines changed

config/whatsapp-widget.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
return [
3+
'disk' => env('FILESYSTEM_DISK', 'local'),
4+
'url' => env('APP_URL', 'http://localhost'),
5+
'name' => env('APP_NAME', 'Laravel App'),
6+
'key' => env('WHATSAPP_KEY'),
7+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
public function up()
9+
{
10+
Schema::create('whatsapp_agents', function (Blueprint $table) {
11+
$table->id();
12+
$table->boolean('active');
13+
$table->string('name');
14+
$table->string('phone');
15+
$table->string('text')->nullable();
16+
$table->string('image')->nullable();
17+
$table->timestamps();
18+
});
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
public function up()
9+
{
10+
Schema::create('whatsapp_logs', function (Blueprint $table) {
11+
$table->id();
12+
$table->json('agent')->nullable();
13+
$table->json('number')->nullable();
14+
$table->json('geo')->nullable();
15+
$table->string('type')->nullable();
16+
$table->string('ref')->nullable();
17+
$table->timestamps();
18+
});
19+
}
20+
};

routes/whatsapp-widget.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Route;
4+
use JeffersonGoncalves\WhatsappWidget\Http\Controllers\RedirectController;
5+
use JeffersonGoncalves\WhatsappWidget\Http\Controllers\WhatsappLogController;
6+
use JeffersonGoncalves\WhatsappWidget\Http\Controllers\WhatsappLogFrontendController;
7+
use JeffersonGoncalves\WhatsappWidget\Http\Controllers\WhatsappsController;
8+
9+
Route::get('/api/whatsapps', WhatsappsController::class);
10+
Route::post('/api/whatsapp-frontend-widget', WhatsappLogFrontendController::class)
11+
->name('whatsapp-widget.api-frontend');
12+
Route::post('/api/whatsapp-widget', WhatsappLogController::class)
13+
->name('whatsapp-widget.api');
14+
Route::get('/whatsapp-widget/redirect/{whatsapp_agent}', RedirectController::class)
15+
->name('whatsapp-widget.redirect');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace JeffersonGoncalves\WhatsappWidget\Http\Controllers;
4+
5+
use Illuminate\Contracts\View\View;
6+
use JeffersonGoncalves\WhatsappWidget\Models\WhatsappAgent;
7+
8+
class RedirectController
9+
{
10+
public function __invoke(string $whatsapp_agent): View
11+
{
12+
$whatsappAgent = WhatsappAgent::query()->where('id', $whatsapp_agent)->firstOrFail();
13+
return view('whatsapp-widget::whatsapp-redirect', compact('whatsappAgent'));
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace JeffersonGoncalves\WhatsappWidget\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Response;
7+
use JeffersonGoncalves\WhatsappWidget\Http\Requests\WhatsappLogRequest;
8+
use JeffersonGoncalves\WhatsappWidget\Models\WhatsappLog;
9+
10+
class WhatsappLogController
11+
{
12+
public function __invoke(WhatsappLogRequest $request): Response
13+
{
14+
WhatsappLog::createByRequest($request->validated());
15+
16+
return response()->noContent();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace JeffersonGoncalves\WhatsappWidget\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Response;
7+
use JeffersonGoncalves\WhatsappWidget\Http\Requests\WhatsappLogRequest;
8+
use JeffersonGoncalves\WhatsappWidget\Models\WhatsappLog;
9+
use function abort_if;
10+
use function config;
11+
12+
class WhatsappLogFrontendController
13+
{
14+
public function __invoke(WhatsappLogRequest $request): Response
15+
{
16+
abort_if($request->token !== config('whatsapp-widget.key'), 403);
17+
WhatsappLog::createByRequest($request->validated());
18+
19+
return response()->noContent();
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace JeffersonGoncalves\WhatsappWidget\Http\Controllers;
4+
5+
use Illuminate\Http\JsonResponse;
6+
use Illuminate\Http\Request;
7+
use JeffersonGoncalves\WhatsappWidget\Models\WhatsappAgent;
8+
9+
class WhatsappsController
10+
{
11+
public function __invoke(Request $request): JsonResponse
12+
{
13+
abort_if($request->token !== config('whatsapp-widget.key'), 403);
14+
$whatsappAgents = WhatsappAgent::query()->where('active', true)->get();
15+
$ref = config('whatsapp-widget.url');
16+
$data = [];
17+
foreach ($whatsappAgents as $whatsappAgent) {
18+
$data[] = [
19+
'id' => $whatsappAgent->id,
20+
'name' => $whatsappAgent->name,
21+
'image_url' => $whatsappAgent->image_url ?? null,
22+
'phone' => $whatsappAgent->phone,
23+
'link' => $whatsappAgent->getLinkByWhatsappAgent($whatsappAgent, $ref),
24+
];
25+
}
26+
return response()->json($data);
27+
}
28+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace JeffersonGoncalves\WhatsappWidget\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class WhatsappLogRequest extends FormRequest
8+
{
9+
public function rules(): array
10+
{
11+
return [
12+
'agent' => 'required',
13+
'number' => 'required',
14+
'type' => 'nullable',
15+
'ref' => 'nullable',
16+
'geo' => 'nullable',
17+
'token' => 'exclude',
18+
];
19+
}
20+
}

src/Models/WhatsappAgent.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace JeffersonGoncalves\WhatsappWidget\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Storage;
7+
use Illuminate\Support\Facades\URL;
8+
9+
class WhatsappAgent extends Model
10+
{
11+
protected $fillable = [
12+
'active',
13+
'name',
14+
'phone',
15+
'text',
16+
'image',
17+
];
18+
19+
protected $casts = [
20+
'active' => 'bool',
21+
];
22+
23+
public function getImageUrlAttribute(): ?string
24+
{
25+
if (empty($this->image)) {
26+
return null;
27+
}
28+
return Storage::disk(config('whatsapp-widget.disk'))->url($this->image);
29+
}
30+
31+
public function getLinkByWhatsappAgent($whatsappAgent, $ref): string
32+
{
33+
return URL::signedRoute('whatsapp-widget.redirect', [
34+
'whatsapp_agent' => $whatsappAgent->id,
35+
'agent' => $whatsappAgent->id,
36+
'number' => $whatsappAgent->phone,
37+
'ref' => $ref,
38+
]);
39+
}
40+
}

src/Models/WhatsappLog.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace JeffersonGoncalves\WhatsappWidget\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class WhatsappLog extends Model
8+
{
9+
protected $casts = [
10+
'agent' => 'json',
11+
'number' => 'json',
12+
'geo' => 'json',
13+
];
14+
protected $table = 'whatsapp_logs';
15+
protected $fillable = [
16+
'agent',
17+
'number',
18+
'type',
19+
'ref',
20+
'geo',
21+
];
22+
23+
public static function createByRequest(array $data): void
24+
{
25+
self::create([
26+
'agent' => [$data['agent']],
27+
'number' => [$data['number']],
28+
'type' => $data['type'],
29+
'ref' => $data['ref'],
30+
'geo' => $data['geo'] ?? [],
31+
]);
32+
}
33+
}

0 commit comments

Comments
 (0)