Installation
Add the bundle to your composer.json:
composer require dotsmart/sms-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Belyas\DotSmartSmsBundle\BelyasDotSmartSmsBundle::class => ['all' => true],
];
Configuration
Publish the default config and adjust config/packages/dotsmart_sms.yaml:
dotsmart_sms:
api_key: 'your_dot_smart_api_key'
sender_id: 'your_sender_id'
timeout: 30
First Use Case
Inject the DotSmartSmsService into a controller or service:
use Belyas\DotSmartSmsBundle\Service\DotSmartSmsService;
public function sendSms(DotSmartSmsService $smsService)
{
$response = $smsService->sendSms(
'+212612345678', // Recipient
'Test message', // Message
'YOUR_SENDER_ID' // Optional: Override sender_id
);
return new JsonResponse($response);
}
Sending SMS Use the service in controllers, commands, or event listeners:
$smsService->sendSms($phoneNumber, $message, $senderId = null);
Async Processing
Dispatch a Symfony Message via a queue (e.g., Symfony Messenger):
$message = new SendSmsMessage($phone, $text, $senderId);
$bus->dispatch($message);
Configure a handler to use DotSmartSmsService.
Validation
Validate phone numbers before sending (e.g., with libphonenumber):
use libphonenumber\PhoneNumberUtil;
$phoneUtil = PhoneNumberUtil::getInstance();
$phone = $phoneUtil->parse($phoneNumber);
if (!$phoneUtil->isValidNumber($phone)) {
throw new \InvalidArgumentException('Invalid phone number');
}
Logging Responses Extend the service to log responses (e.g., using Monolog):
$logger->info('SMS sent', ['recipient' => $phone, 'response' => $response]);
SmsType form field for user-submitted messages.UserRegisteredEvent).API Key Exposure
api_key to version control. Use environment variables:
# .env
DOTSMART_API_KEY=your_key_here
config/packages/dev/dotsmart_sms.yaml for local testing.Rate Limits
$attempts = 0;
while ($attempts < 3) {
try {
$response = $smsService->sendSms(...);
break;
} catch (\Exception $e) {
$attempts++;
sleep(2 ** $attempts); // Exponential delay
}
}
Character Limits
$message = mb_strimwidth($message, 0, 160, '...', 'UTF-8');
Sender ID Restrictions
debug: true in config to log raw API responses.try-catch to handle:
401: Invalid API key.429: Rate limit exceeded.500: Server error (retry or notify admin).Custom Responses
Extend DotSmartSmsService to transform responses:
class CustomSmsService extends DotSmartSmsService {
public function sendSms($phone, $message, $senderId = null) {
$response = parent::sendSms($phone, $message, $senderId);
return ['success' => $response['status'] === 'success', 'data' => $response];
}
}
Template Engine Use Twig to render SMS templates:
{# templates/sms/welcome.txt.twig #}
Hello {{ user.name }},
Welcome to our service!
$message = $twig->render('sms/welcome.txt.twig', ['user' => $user]);
$smsService->sendSms($user->phone, $message);
Webhook Validation If DotSmart supports webhooks, validate signatures:
$expectedSignature = hash_hmac('sha256', $payload, config('dotsmart_sms.webhook_secret'));
if (!hash_equals($expectedSignature, $_SERVER['HTTP_X_SIGNATURE'])) {
throw new \RuntimeException('Invalid webhook signature');
}
How can I help you explore Laravel packages today?