| name | twilio-communications |
| description | Build communication features with Twilio: SMS messaging, voice calls, WhatsApp Business API, and user verification (2FA). Covers the full spectrum from simple notifications to complex IVR systems a... |
Twilio Communications
Patterns
SMS Sending Pattern
Basic pattern for sending SMS messages with Twilio.
Handles the fundamentals: phone number formatting, message delivery,
and delivery status callbacks.
Key considerations:
- Phone numbers must be in E.164 format (+1234567890)
- Default rate limit: 80 messages per second (MPS)
- Messages over 160 characters are split (and cost more)
- Carrier filtering can block messages (especially to US numbers)
When to use: ['Sending notifications to users', 'Transactional messages (order confirmations, shipping)', 'Alerts and reminders']
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
import os
import re
class TwilioSMS:
"""
SMS sending with proper error handling and validation.
"""
def __init__(self):
self.client = Client(
os.environ["TWILIO_ACCOUNT_SID"],
os.environ["TWILIO_AUTH_TOKEN"]
)
self.from_number = os.environ["TWILIO_PHONE_NUMBER"]
() -> :
pattern =
(re.(pattern, phone))
() -> :
.validate_e164(to):
{
: ,
:
}
segment_count = ((body) + ) //
segment_count > :
()
:
message = .client.messages.create(
to=to,
from_=.from_number,
body=body,
status_callback=status_callback
)
{
: ,
: message.sid,
: message.status,
: segment_count
}
TwilioRestException e:
._handle_error(e)
(