Reminder / app.py
ak0601's picture
Update app.py
b72e388 verified
raw
history blame contribute delete
No virus
2.31 kB
# import vonage
# client = vonage.Client(key="712db024", secret="L4RYeQ2EWbHG6UTY")
# sms = vonage.Sms(client)
# responseData = sms.send_message(
# {
# "from": "Vonage APIs",
# "to": "917869844761",
# "text": "A text message sent using the Nexmo SMS API",
# }
# )
# if responseData["messages"][0]["status"] == "0":
# print("Message sent successfully.")
# else:
# print(f"Message failed with error: {responseData['messages'][0]['error-text']}")
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 # HH:MM format
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)
# uvicorn test:app --reload