Messaging Manager
Configure your WhatsApp messaging system
Create a .env.local file in your project root:
# MongoDB Connection MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/messaging_db # WhatsApp Cloud API WHATSAPP_API_TOKEN=your_whatsapp_token_here WHATSAPP_BUSINESS_ACCOUNT_ID=your_business_account_id WHATSAPP_PHONE_NUMBER_ID=your_phone_number_id # SMS Provider (Fast2SMS) SMS_PROVIDER=fast2sms FAST2SMS_API_KEY=your_fast2sms_api_key SMS_SENDER_ID=YourSenderId # Optional: Other SMS Providers # MSG91_API_KEY=your_msg91_key # TEXTLOCAL_API_KEY=your_textlocal_key # Webhook Verification WEBHOOK_VERIFY_TOKEN=your_random_webhook_token WEBHOOK_SECRET=your_webhook_secret_key # API Rate Limiting API_RATE_LIMIT=100 # requests per minute BATCH_SIZE=50 # messages per batch
When a student pays their fee, trigger a WhatsApp message to the parent with receipt details:
// In your fee payment endpoint
app.post('/api/fees/pay', async (req, res) => {
const { studentId, parentPhone, feeAmount, month, year } = req.body;
// Process fee payment
const feeRecord = await Fee.create({
studentId,
amount: feeAmount,
month,
year,
paidAt: new Date(),
});
// Trigger WhatsApp message to parent
const message = `Fee Receipt
Student: [${studentId}]
Amount: ₹${feeAmount}
Month: ${month} ${year}
Paid: ${new Date().toLocaleDateString()}
Thank you!`;
const response = await fetch('https://yourdomain.com/api/send-message', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
phone: parentPhone,
message,
channel: 'whatsapp',
messageType: 'text',
idempotencyKey: `fee-${feeRecord._id}`,
}),
});
if (response.ok) {
res.json({ success: true, message: 'Fee recorded and parent notified' });
} else {
res.status(500).json({ error: 'Failed to notify parent' });
}
});# views.py
import requests
from django.http import JsonResponse
from .models import Fee
def pay_fee(request):
if request.method == 'POST':
data = request.POST
student_id = data.get('student_id')
parent_phone = data.get('parent_phone')
fee_amount = data.get('fee_amount')
month = data.get('month')
year = data.get('year')
# Save fee record
fee = Fee.objects.create(
student_id=student_id,
amount=fee_amount,
month=month,
year=year,
)
# Send WhatsApp message
message = f"""Fee Receipt
Student: {student_id}
Amount: ₹{fee_amount}
Month: {month} {year}
Paid: {fee.created_at.strftime('%d-%m-%Y')}
Thank you!"""
payload = {
'phone': parent_phone,
'message': message,
'channel': 'whatsapp',
'messageType': 'text',
'idempotencyKey': f'fee-{fee.id}',
}
response = requests.post(
'https://yourdomain.com/api/send-message',
json=payload
)
return JsonResponse({'success': True})curl -X POST https://yourdomain.com/api/send-message \
-H "Content-Type: application/json" \
-d '{
"phone": "+919876543210",
"message": "Fee Receipt\n\nStudent: ABC123\nAmount: ₹5000\nMonth: November 2024\nPaid: 20-11-2024\n\nThank you!",
"channel": "whatsapp",
"messageType": "text",
"idempotencyKey": "fee-student-abc123-nov2024"
}'Configure your WhatsApp Cloud API to send webhooks to your application:
Webhook URL:
https://yourdomain.com/api/webhookVerify Token:
Use the value from WEBHOOK_VERIFY_TOKEN in your .env.local