Reminder / app.py
ak0601's picture
Upload app.py
e6f0cc8 verified
raw
history blame
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 threading
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
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}")
def schedule_daily_sms(message_request: MessageRequest):
scheduled_time = datetime.strptime(message_request.scheduled_time, "%H:%M")
schedule.every().day.at(message_request.scheduled_time).do(send_scheduled_sms, message_request)
print(f"Scheduled daily SMS for {message_request.phone_num} at {scheduled_time}")
@app.post("/send_daily_sms")
async def send_daily_sms_endpoint(message_request: MessageRequest):
schedule_daily_sms(message_request)
return {"message": f"Daily SMS scheduled for {message_request.phone_num} at {message_request.scheduled_time}"}
def run_scheduler():
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == "__main__":
scheduler_thread = threading.Thread(target=schedule_daily_sms)
scheduler_thread.start()
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
# uvicorn test:app --reload