|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from fastapi import FastAPI, HTTPException |
|
from pydantic import BaseModel |
|
from vonage import Client, Sms |
|
from datetime import datetime, timedelta |
|
import schedule |
|
import asyncio |
|
import time |
|
import vonage |
|
|
|
app = FastAPI() |
|
client = vonage.Client(key="712db024", secret="L4RYeQ2EWbHG6UTY") |
|
sms = vonage.Sms(client) |
|
|
|
class MessageRequest(BaseModel): |
|
phone_num: str |
|
text: str |
|
scheduled_time: str |
|
|
|
async def send_scheduled_sms(message_request: MessageRequest): |
|
try: |
|
response = sms.send_message( |
|
{ |
|
"from": "Vonage APIs", |
|
"to": message_request.phone_num, |
|
"text": f"This is your medicine reminder for {message_request.text}" |
|
} |
|
) |
|
print(f"SMS sent to {message_request.phone_num} at {message_request.scheduled_time}") |
|
except Exception as e: |
|
print(f"Failed to send SMS to {message_request.phone_num}: {e}") |
|
|
|
|
|
async def schedule_sms(message_request: MessageRequest): |
|
scheduled_time = datetime.strptime(message_request.scheduled_time, "%H:%M") |
|
now = datetime.now() |
|
delta = scheduled_time - now |
|
seconds_until_scheduled_time = delta.total_seconds() |
|
|
|
if seconds_until_scheduled_time > 0: |
|
print(f"Scheduling SMS for {message_request.phone_num} at {scheduled_time}") |
|
await asyncio.sleep(seconds_until_scheduled_time) |
|
await send_scheduled_sms(message_request) |
|
|
|
|
|
@app.post("/send_scheduled_sms") |
|
async def send_scheduled_sms_endpoint(message_request: MessageRequest): |
|
asyncio.create_task(schedule_sms(message_request)) |
|
return {"message": f"SMS scheduled for {message_request.phone_num} at {message_request.scheduled_time}"} |
|
|
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
|
|
uvicorn.run(app, host="127.0.0.1", port=8000) |
|
|
|
|
|
|
|
|