Overview
VoicePact uses Africa’s Talking as its primary telecommunications provider for voice calls, SMS messaging, USSD sessions, and mobile money payments. The integration provides a robust, circuit-breaker-protected client with retry logic and comprehensive error handling.Architecture
The Africa’s Talking client is implemented inapp/services/africastalking_client.py and provides:
- Voice Services: Automated voice calls and conference recording
- SMS Services: Bulk SMS with delivery tracking
- USSD Services: Interactive menu-based contract management
- Payment Services: Mobile money checkout and escrow management
- Circuit Breaker: Automatic failover protection
- Webhook Verification: HMAC-based signature validation
Configuration
Environment Variables
Configure the following environment variables in your.env file:
Configuration Class
The settings are managed by theSettings class in app/core/config.py:80-104:
Client Initialization
Basic Setup
The client is initialized as a singleton instance:Client Architecture
TheAfricasTalkingClient class (lines 68-113) includes:
Voice Services
Making Voice Calls
Initiate voice calls with automatic retry logic:Voice Webhooks
Voice webhooks are handled at/api/v1/voice/webhook (see app/api/v1/endpoints/voice.py:223-258):
Webhook Payload:
SMS Services
Sending SMS
Send SMS with automatic retry and circuit breaker protection:Bulk SMS
Send messages to multiple recipients:SMS Templates
The client provides built-in SMS templates:Contract Notification
Payment Notification
SMS Webhooks
Handle incoming SMS and delivery reports at/api/v1/sms/webhook (see app/api/v1/endpoints/sms.py:293-328):
Webhook Payload:
USSD Services
USSD Menu Flow
USSD menus are handled at/api/v1/ussd/ (see app/api/v1/endpoints/ussd.py):
Main Menu:
Building USSD Responses
Use the utility methods to build USSD responses:USSD Request Format
Webhook Payload:USSD Handler Implementation
Payment Services
See Mobile Money Integration for detailed payment documentation.Mobile Checkout
Circuit Breaker
The client implements circuit breaker pattern for fault tolerance:Circuit Breaker States
- CLOSED: Normal operation, requests pass through
- OPEN: Too many failures, requests blocked
- HALF_OPEN: Testing if service recovered
Usage Example
Webhook Security
Signature Verification
Verify webhook authenticity using HMAC signatures:Implementation
Error Handling
Custom Exceptions
Retry Logic
Automatic retry with exponential backoff:Health Checks
Service Status
Testing
Sandbox Mode
Use Africa’s Talking sandbox for testing:Test Endpoints
Best Practices
- Use Circuit Breakers: Let the built-in circuit breakers protect your services
- Verify Webhooks: Always verify webhook signatures in production
- Format Phone Numbers: Use
format_phone_number()for consistent formatting - Handle Retries: Configure appropriate retry limits for your use case
- Monitor Health: Regularly check service health status
- Log Everything: Comprehensive logging helps debug integration issues
- Test in Sandbox: Always test with sandbox credentials first
Troubleshooting
Common Issues
SMS Not Sending:- Verify webhook URL is publicly accessible
- Check firewall settings
- Validate webhook signature configuration
- Review Africa’s Talking dashboard logs
Next Steps
Mobile Money
Learn about mobile money integration
Webhooks
Complete webhook configuration guide
API Reference
Full API documentation
Voice Processing
Voice recording and processing