forked from nivishnaa-sri/SHEshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
36 lines (30 loc) · 1.4 KB
/
views.py
File metadata and controls
36 lines (30 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import vonage # New import
from django.conf import settings
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import EmergencyContact
class TriggerSOS(APIView):
def post(self, request):
user = request.user
location_url = request.data.get('location_url', 'No location provided')
contacts = EmergencyContact.objects.filter(user=user)
if not contacts.exists():
return Response({"error": "No contacts found"}, status=status.HTTP_400_BAD_REQUEST)
# 1. Initialize Vonage Client
client = vonage.Client(key=settings.VONAGE_API_KEY, secret=settings.VONAGE_API_SECRET)
sms = vonage.Sms(client)
success_count = 0
for contact in contacts:
# 2. Send the message
response = sms.send_message({
"from": settings.VONAGE_FROM_NUMBER,
"to": contact.phone_number,
"text": f"🚨 EMERGENCY! {user.username} is in danger. Location: {location_url}",
})
# 3. Nexmo response check
if response["messages"][0]["status"] == "0":
success_count += 1
else:
print(f"Error: {response['messages'][0]['error-text']}")
return Response({"status": f"Alerts sent to {success_count} contacts!"})