F
Filament14mo ago
Renzo

Twilio Integration

Hey guys, I've been trying add SMS notification everytime a new user is created. I've already added user_contact on my user schema also added values on E.164 format. I've also defined my twilio variables on .ENV file. TWILIO_ACCOUNT_SID=AC8c4bb20a1d51ae1a9a7134b1fb99eab2 TWILIO_AUTH_TOKEN=67ae0e279a32c1a4210a793368f35cb2 TWILIO_PHONE_NUMBER=+19389999868 For some reason, I'm not receiving any SMS, but I'm able to create data. Here's my Create code:
<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use App\Models\User;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use Twilio\Rest\Client;

class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;

protected function save(): void
{
parent::save(); // Save the user first

// Notify other users via SMS about the new user
$this->sendSmsNotification();
}

protected function sendSmsNotification(): void
{
$sid = config('services.twilio.sid');
$token = config('services.twilio.token');
$twilioPhoneNumber = config('services.twilio.phone_number');

$twilio = new Client($sid, $token);

// Here, I'm assuming you want to notify all other users. Adjust this as necessary.
$usersToNotify = User::whereNotNull('user_contact')->get();

foreach ($usersToNotify as $user) {
$twilio->messages->create(
$user->user_contact, // User's contact number in E.164 format
[
"body" => "A new work order has been created.",
"from" => $twilioPhoneNumber
]
);
}
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use App\Models\User;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use Twilio\Rest\Client;

class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;

protected function save(): void
{
parent::save(); // Save the user first

// Notify other users via SMS about the new user
$this->sendSmsNotification();
}

protected function sendSmsNotification(): void
{
$sid = config('services.twilio.sid');
$token = config('services.twilio.token');
$twilioPhoneNumber = config('services.twilio.phone_number');

$twilio = new Client($sid, $token);

// Here, I'm assuming you want to notify all other users. Adjust this as necessary.
$usersToNotify = User::whereNotNull('user_contact')->get();

foreach ($usersToNotify as $user) {
$twilio->messages->create(
$user->user_contact, // User's contact number in E.164 format
[
"body" => "A new work order has been created.",
"from" => $twilioPhoneNumber
]
);
}
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
4 Replies
Dennis Koch
Dennis Koch14mo ago
If the Filament part is working not sure how we can help with this? You need to debug your Twilio implementation
Renzo
RenzoOP14mo ago
Got it, but it's possible to integrate Twilio with Filament right?
Dennis Koch
Dennis Koch14mo ago
Sure why not? I'd probably use an Observer for this though
Renzo
RenzoOP14mo ago
Gotcha, thanks for the tip
Want results from more Discord servers?
Add your server