mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
- Curated guides under docs/; trim China-only integration docs and screenshots - Root and main README set aligned with westernized product positioning - LICENSE text update where applicable
4 KiB
4 KiB
Twilio SMS Integration
This document describes how to configure and use Twilio SMS service for sending verification codes in the Xiaozhi system.
Overview
Twilio SMS service has been implemented as a replacement for the previous Aliyun SMS service, providing Western-based SMS functionality for user verification.
Configuration
1. Twilio Account Setup
- Visit Twilio Console
- Create a new account or sign in to existing account
- Navigate to the Console Dashboard
- Get your Account SID and Auth Token from the Account Info section
- Purchase a phone number for sending SMS messages
2. System Configuration
Configure the following parameters in the system configuration:
Required Parameters
- twilio.sms.account_sid: Your Twilio Account SID
- twilio.sms.auth_token: Your Twilio Auth Token
- twilio.sms.phone_number: Your Twilio phone number (in E.164 format, e.g., +1234567890)
- twilio.sms.template_message: SMS message template (default: "Your verification code is: %s")
Optional Parameters
- server.sms_max_send_count: Maximum SMS messages per phone number per day (default: 10)
- server.enable_mobile_register: Enable mobile phone registration (default: false)
3. Configuration Examples
Basic Configuration
twilio.sms.account_sid=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
twilio.sms.auth_token=your_auth_token_here
twilio.sms.phone_number=+1234567890
twilio.sms.template_message=Your verification code is: %s
Custom Template
twilio.sms.template_message=Welcome to Xiaozhi! Your verification code is %s. This code will expire in 10 minutes.
Features
SMS Verification
- Send verification codes via SMS
- Rate limiting (configurable daily limit per phone number)
- 60-second cooldown between SMS sends
- Automatic retry mechanism on failure
Error Handling
- Connection failure detection
- Invalid phone number handling
- Rate limit enforcement
- Comprehensive error logging
API Usage
Sending Verification Code
@Autowired
private SmsService smsService;
// Send verification code
smsService.sendVerificationCodeSms("+1234567890", "123456");
Rate Limiting
The system implements the following rate limiting:
- Daily Limit: Configurable via
server.sms_max_send_count(default: 10 messages per day) - Cooldown: 60 seconds between consecutive SMS sends to the same number
- Redis Storage: Rate limit data stored in Redis for persistence
Security Considerations
- API Keys: Store Twilio credentials securely in system configuration
- Rate Limiting: Prevents SMS abuse and spam
- Phone Number Validation: Ensures proper E.164 format
- Error Handling: Prevents credential exposure in logs
Troubleshooting
Common Issues
-
Invalid Phone Number Format
- Ensure phone numbers are in E.164 format (+1234567890)
- Remove spaces, dashes, and parentheses
-
Authentication Failures
- Verify Account SID and Auth Token are correct
- Check if credentials have SMS permissions
-
Rate Limiting
- Check daily send limits in Twilio console
- Verify system rate limiting configuration
-
Phone Number Issues
- Ensure Twilio phone number is verified
- Check if phone number supports SMS in target region
Error Codes
SMS_SEND_FAILED: Failed to send SMS messageSMS_CONNECTION_FAILED: Failed to connect to Twilio serviceSMS_SEND_TOO_FREQUENTLY: Rate limit exceeded (60-second cooldown)TODAY_SMS_LIMIT_REACHED: Daily limit exceeded
Migration from Aliyun SMS
If migrating from the previous Aliyun SMS service:
- Remove old Aliyun SMS configuration parameters
- Add new Twilio SMS configuration parameters
- Update phone number formats to E.164 standard
- Test SMS functionality with new configuration
Support
For Twilio-specific issues, refer to: