Twilio (SMS & WhatsApp API)

The artiframe add twilio extension integrates the twilio/sdk package, providing a simple wrapper under src/Service/TwilioService.php to send programmable SMS and WhatsApp messages globally.

1. Installation and Configuration

Add the extension to your project via the terminal:

terminal
$ artiframe add twilio

After execution, the following configurations will be added to your .env file. Get these credentials from your Twilio Console:

.env
TWILIO_ACCOUNT_SID=AC_your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_FROM_NUMBER=+1234567890
# TWILIO_WHATSAPP_FROM=+14155238886
2. Sending SMS Messages

Sending a standard text message to a mobile number is straightforward.

NotificationController.php
<?php
use Src\Service\TwilioService;

$twilio = new TwilioService();

$toPhoneNumber = '+905551234567'; // Must include country code
$messageBody = 'Your verification code is: 456789';

$response = $twilio->sendSms($toPhoneNumber, $messageBody);

if ($response['status'] === true) {
    echo "SMS Sent! Message SID: " . $response['sid'];
} else {
    echo "Error: " . $response['error'];
}
3. Sending WhatsApp Messages

To send WhatsApp messages, your Twilio number must be approved for WhatsApp Sandbox or Business API.

OrderController.php
<?php
use Src\Service\TwilioService;

$twilio = new TwilioService();

$toPhoneNumber = '+905551234567';
$messageBody = 'Your order #1005 has been shipped. Track: https://cargo.com/tr/1005';

$response = $twilio->sendWhatsApp($toPhoneNumber, $messageBody);

if ($response['status'] === true) {
    echo "WhatsApp Message Sent!";
}
WhatsApp Restrictions: Twilio WhatsApp integration requires you to initiate a conversation using pre-approved templates (unless the user has messaged you within the last 24 hours). Ensure your $messageBody perfectly matches an approved template if starting a new conversation.