ak0601 commited on
Commit
e6f0cc8
1 Parent(s): c2d09a0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import vonage
2
+ # client = vonage.Client(key="712db024", secret="L4RYeQ2EWbHG6UTY")
3
+ # sms = vonage.Sms(client)
4
+ # responseData = sms.send_message(
5
+ # {
6
+ # "from": "Vonage APIs",
7
+ # "to": "917869844761",
8
+ # "text": "A text message sent using the Nexmo SMS API",
9
+ # }
10
+ # )
11
+
12
+ # if responseData["messages"][0]["status"] == "0":
13
+ # print("Message sent successfully.")
14
+ # else:
15
+ # print(f"Message failed with error: {responseData['messages'][0]['error-text']}")
16
+ from fastapi import FastAPI, HTTPException
17
+ from pydantic import BaseModel
18
+ from vonage import Client, Sms
19
+ from datetime import datetime, timedelta
20
+ import schedule
21
+ import threading
22
+ import time
23
+ import vonage
24
+
25
+ app = FastAPI()
26
+ client = vonage.Client(key="712db024", secret="L4RYeQ2EWbHG6UTY")
27
+ sms = vonage.Sms(client)
28
+
29
+ class MessageRequest(BaseModel):
30
+ phone_num: str
31
+ text: str
32
+ scheduled_time: str # HH:MM format
33
+
34
+ def send_scheduled_sms(message_request: MessageRequest):
35
+ try:
36
+ response = sms.send_message(
37
+ {
38
+ "from": "Vonage APIs",
39
+ "to": message_request.phone_num,
40
+ "text": f"This is your medicine reminder for {message_request.text}"
41
+ }
42
+ )
43
+ print(f"SMS sent to {message_request.phone_num} at {message_request.scheduled_time}")
44
+ except Exception as e:
45
+ print(f"Failed to send SMS to {message_request.phone_num}: {e}")
46
+
47
+ def schedule_daily_sms(message_request: MessageRequest):
48
+ scheduled_time = datetime.strptime(message_request.scheduled_time, "%H:%M")
49
+ schedule.every().day.at(message_request.scheduled_time).do(send_scheduled_sms, message_request)
50
+ print(f"Scheduled daily SMS for {message_request.phone_num} at {scheduled_time}")
51
+
52
+ @app.post("/send_daily_sms")
53
+ async def send_daily_sms_endpoint(message_request: MessageRequest):
54
+ schedule_daily_sms(message_request)
55
+ return {"message": f"Daily SMS scheduled for {message_request.phone_num} at {message_request.scheduled_time}"}
56
+
57
+ def run_scheduler():
58
+ while True:
59
+ schedule.run_pending()
60
+ time.sleep(1)
61
+
62
+ if __name__ == "__main__":
63
+ scheduler_thread = threading.Thread(target=schedule_daily_sms)
64
+ scheduler_thread.start()
65
+ import uvicorn
66
+
67
+ uvicorn.run(app, host="127.0.0.1", port=8000)
68
+
69
+
70
+
71
+ # uvicorn test:app --reload