Skip to content

Commit 459bc04

Browse files
Implemented the functionality to enable typebot by API
1 parent 828b0f6 commit 459bc04

File tree

13 files changed

+780
-346
lines changed

13 files changed

+780
-346
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Enums\Evolution\Typebot;
4+
5+
use Filament\Support\Contracts\{HasLabel};
6+
7+
enum TriggerOperatorEnum: string implements HasLabel
8+
{
9+
case CONTAINS = 'contains';
10+
case EQUALS = 'equals';
11+
case STARTWITH = 'startsWith';
12+
case ENDSWITH = 'endsWith';
13+
case REGEX = 'regex';
14+
15+
public function getLabel(): string
16+
{
17+
return match ($this) {
18+
self::CONTAINS => 'Contem',
19+
self::EQUALS => 'Igual',
20+
self::STARTWITH => 'Começa com',
21+
self::ENDSWITH => 'Termina com',
22+
self::REGEX => 'Regex',
23+
};
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Enums\Evolution\Typebot;
4+
5+
use Filament\Support\Contracts\{HasLabel};
6+
7+
enum TriggerTypeEnum: string implements HasLabel
8+
{
9+
case ALL = 'all';
10+
case NONE = 'none';
11+
case ADVANCED = 'advanced';
12+
case KEYWORD = 'keyword';
13+
14+
public function getLabel(): string
15+
{
16+
return match ($this) {
17+
self::ALL => 'Todos',
18+
self::NONE => 'Nenhum',
19+
self::ADVANCED => 'Começa com',
20+
self::KEYWORD => 'Palavra Chave',
21+
22+
};
23+
}
24+
25+
}

app/Filament/Admin/Resources/CouponResource.php

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use App\Filament\Admin\Resources\CouponResource\{Pages};
77
use App\Models\Coupon;
88
use App\Services\Stripe\Discount\{DeleteStripeCouponService};
9-
109
use Filament\Forms\Components\DateTimePicker;
1110
use Filament\Forms\Components\{Fieldset, Select, TextInput};
1211
use Filament\Forms\Form;
@@ -33,7 +32,6 @@ class CouponResource extends Resource
3332
protected static ?int $navigationSort = 2;
3433

3534
protected static bool $isScopedToTenant = false;
36-
3735
public static function form(Form $form): Form
3836
{
3937
return $form

app/Filament/Admin/Resources/OrganizationResource/RelationManagers/WhatsappInstanceRelationManager.php

+19-19
Original file line numberDiff line numberDiff line change
@@ -309,32 +309,32 @@ public function table(Table $table): Table
309309
})
310310
->icon('fab-whatsapp')
311311
->color('success'),
312-
])
312+
])
313313
->icon('fab-whatsapp')
314314
->color('success'),
315315

316316
ActionGroup::make([
317-
ViewAction::make()
318-
->color('primary'),
319-
EditAction::make()
320-
->color('secondary'),
321-
DeleteAction::make()
322-
->action(function ($record, $livewire) {
323-
$service = new DeleteEvolutionInstanceService();
324-
$response = $service->deleteInstance($record->name);
325-
326-
// Deleta o registro local após sucesso na API
327-
$record->delete();
328-
$livewire->dispatch('refresh');
329-
}),
330-
])
317+
ViewAction::make()
318+
->color('primary'),
319+
EditAction::make()
320+
->color('secondary'),
321+
DeleteAction::make()
322+
->action(function ($record, $livewire) {
323+
$service = new DeleteEvolutionInstanceService();
324+
$response = $service->deleteInstance($record->name);
325+
326+
// Deleta o registro local após sucesso na API
327+
$record->delete();
328+
$livewire->dispatch('refresh');
329+
}),
330+
])
331331
->icon('fas-sliders')
332332
->color('warning'),
333333
])
334334
->bulkActions([
335-
Tables\Actions\BulkActionGroup::make([
336-
Tables\Actions\DeleteBulkAction::make(),
337-
]),
338-
]);
335+
Tables\Actions\BulkActionGroup::make([
336+
Tables\Actions\DeleteBulkAction::make(),
337+
]),
338+
]);
339339
}
340340
}

app/Filament/App/Resources/WhatsappInstanceResource.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Filament\App\Resources;
44

5+
use App\Filament\App\Resources\WhatsappInstanceResource\RelationManagers\InstanceTypebotsRelationManager;
56
use App\Filament\App\Resources\WhatsappInstanceResource\{Pages};
67
use App\Models\{WhatsappInstance};
78
use App\Services\Evolution\Instance\{ConnectEvolutionInstanceService, DeleteEvolutionInstanceService, LogOutEvolutionInstanceService};
@@ -144,6 +145,12 @@ public static function table(Table $table): Table
144145
->label('ID da Instância')
145146
->searchable(),
146147

148+
TextColumn::make('bots_count')
149+
->label('Quantidade de Robôs')
150+
->alignCenter()
151+
->getStateUsing(fn ($record) => $record->typebots()->where('is_active', true)->count() ?? 0)
152+
->searchable(),
153+
147154
TextColumn::make('created_at')
148155
->dateTime()
149156
->sortable()
@@ -346,7 +353,7 @@ public static function table(Table $table): Table
346353
public static function getRelations(): array
347354
{
348355
return [
349-
//
356+
InstanceTypebotsRelationManager::class,
350357
];
351358
}
352359

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
namespace App\Filament\App\Resources\WhatsappInstanceResource\RelationManagers;
4+
5+
use App\Enums\Evolution\Typebot\{TriggerOperatorEnum, TriggerTypeEnum};
6+
use Filament\Forms\Components\ToggleButtons;
7+
use Filament\Forms\Components\{Section, Select, TextInput};
8+
use Filament\Forms\Form;
9+
use Filament\Resources\RelationManagers\RelationManager;
10+
use Filament\Tables\Columns\{TextColumn, ToggleColumn};
11+
use Filament\Tables\Table;
12+
use Filament\{Tables};
13+
14+
class InstanceTypebotsRelationManager extends RelationManager
15+
{
16+
protected static string $relationship = 'InstanceTypebots';
17+
18+
protected static ?string $modelLabel = 'Robô TypeBot';
19+
20+
protected static ?string $modelLabelPlural = "TypeBots";
21+
22+
protected static ?string $title = 'Robôs TypeBot';
23+
24+
public function form(Form $form): Form
25+
{
26+
return $form
27+
->schema([
28+
Section::make('Dados Basicos do typebot')
29+
->schema([
30+
31+
TextInput::make('name')
32+
->label('Descrição do Robô')
33+
->prefixIcon('fas-id-card')
34+
->required()
35+
->maxLength(255),
36+
37+
TextInput::make('url')
38+
->label('URL do typebot')
39+
->prefix('https://')
40+
->required()
41+
->maxLength(255),
42+
43+
TextInput::make('type_bot')
44+
->prefixIcon('fas-robot')
45+
->label('Typebot')
46+
->required()
47+
->maxLength(255),
48+
49+
])->columns(3),
50+
51+
Section::make('Dados de Disparo')
52+
->schema([
53+
54+
Select::make('trigger_type')
55+
->label('Tipo de Gatilho')
56+
->required()
57+
->reactive()
58+
->live()
59+
->options(TriggerTypeEnum::class),
60+
61+
Select::make('trigger_operator')
62+
->hidden(fn ($get) => $get('trigger_type') != 'keyword')
63+
->required()
64+
->reactive()
65+
->label('Operador de Gatilho')
66+
->options(TriggerOperatorEnum::class),
67+
68+
TextInput::make('trigger_value')
69+
->hidden(fn ($get) => !in_array($get('trigger_type'), ['advanced', 'keyword']))
70+
->label('Valor do Gatilho')
71+
->prefixIcon('fas-keyboard')
72+
->reactive()
73+
->required()
74+
->maxLength(255),
75+
76+
])->columns(3),
77+
78+
Section::make('Configurações Gerais')
79+
->schema([
80+
81+
TextInput::make('expire')
82+
->label('Expirar em minutos')
83+
->prefixIcon('fas-clock')
84+
->numeric()
85+
->required(),
86+
87+
TextInput::make('keyword_finish')
88+
->label('Palavra-chave de Finalização')
89+
->prefixIcon('fas-arrow-right-from-bracket')
90+
->required()
91+
->maxLength(255),
92+
93+
TextInput::make('delay_message')
94+
->label('Atraso Padrão (Milisegundos)')
95+
->prefixIcon('fas-clock')
96+
->required()
97+
->numeric(),
98+
99+
TextInput::make('unknown_message')
100+
->label('Mensagem Desconhecida')
101+
->prefixIcon('fas-question')
102+
->required()
103+
->maxLength(30),
104+
105+
TextInput::make('debounce_time')
106+
->label('Tempo de Debounce')
107+
->prefixIcon('fas-clock')
108+
->required()
109+
->numeric(),
110+
111+
])->columns(3),
112+
113+
Section::make('Opções Gerais')
114+
->schema([
115+
116+
ToggleButtons::make('listening_from_me')
117+
->label('Ouvindo de mim')
118+
->inline()
119+
->boolean()
120+
->required(),
121+
122+
ToggleButtons::make('stop_bot_from_me')
123+
->label('Parar bot por mim')
124+
->inline()
125+
->boolean()
126+
->required(),
127+
128+
ToggleButtons::make('keep_open')
129+
->label('Manter aberto')
130+
->inline()
131+
->boolean()
132+
->required(),
133+
134+
])->columns(3),
135+
136+
]);
137+
}
138+
139+
public function table(Table $table): Table
140+
{
141+
return $table
142+
->columns([
143+
144+
TextColumn::make('name')
145+
->label('Descrição do'),
146+
147+
TextColumn::make('url')
148+
->label('URL'),
149+
150+
TextColumn::make('type_bot')
151+
->label('Codigo do Typebot'),
152+
153+
TextColumn::make('id_typebot')
154+
->label('Id do Bot'),
155+
156+
ToggleColumn::make('is_active')
157+
->label('Ativo')
158+
->alignCenter(),
159+
160+
])
161+
->filters([
162+
//
163+
])
164+
->headerActions([
165+
Tables\Actions\CreateAction::make(),
166+
])
167+
->actions([
168+
Tables\Actions\EditAction::make(),
169+
Tables\Actions\DeleteAction::make(),
170+
])
171+
->bulkActions([
172+
Tables\Actions\BulkActionGroup::make([
173+
Tables\Actions\DeleteBulkAction::make(),
174+
]),
175+
]);
176+
}
177+
}

app/Models/InstanceTypebot.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Enums\Evolution\Typebot\{TriggerOperatorEnum, TriggerTypeEnum};
6+
use App\Services\Evolution\Typebot\CreateTypeBotService;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9+
use Illuminate\Support\Facades\Log;
10+
11+
class InstanceTypebot extends Model
12+
{
13+
protected $fillable = [
14+
'whatsapp_instance_id',
15+
'name',
16+
'is_active',
17+
'url',
18+
'type_bot',
19+
'trigger_type',
20+
'trigger_operator',
21+
'trigger_value',
22+
'expire',
23+
'keyword_finish',
24+
'delay_message',
25+
'unknown_message',
26+
'listening_from_me',
27+
'stop_bot_from_me',
28+
'keep_open',
29+
'debounce_time',
30+
'id_typebot',
31+
];
32+
33+
protected $casts = [
34+
'trigger_operator' => TriggerOperatorEnum::class,
35+
'trigger_type' => TriggerTypeEnum::class,
36+
'is_active' => 'boolean',
37+
'keep_open' => 'boolean',
38+
'debounce_time' => 'integer',
39+
'delay_message' => 'integer',
40+
'expire' => 'integer',
41+
'listening_from_me' => 'boolean',
42+
'stop_bot_from_me' => 'boolean',
43+
'unknown_message' => 'boolean',
44+
];
45+
46+
public function whatsappInstance(): BelongsTo
47+
{
48+
return $this->belongsTo(WhatsappInstance::class);
49+
}
50+
51+
protected static function boot()
52+
{
53+
parent::boot();
54+
55+
static::created(function ($instanceTypebot) {
56+
57+
$service = new CreateTypeBotService();
58+
$response = $service->createTypeBot($instanceTypebot);
59+
60+
if (isset($response['error'])) {
61+
Log::error("Erro ao criar TypeBot: " . $response['error']);
62+
}
63+
});
64+
}
65+
}

0 commit comments

Comments
 (0)