forked from keploy/samples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
45 lines (37 loc) · 1.27 KB
/
Copy pathmain.py
File metadata and controls
45 lines (37 loc) · 1.27 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
37
38
39
40
41
42
43
44
45
from fastapi import FastAPI
import requests
from dotenv import load_dotenv
from pydantic import BaseModel
import os
import asyncio
# Load environment variables from a .env file
load_dotenv()
class Message(BaseModel):
Body: str
To: str
app = FastAPI()
@app.post('/send-sms/')
def send_sms(data: Message):
twilio_account_sid = os.getenv('TWILIO_ACCOUNT_SID')
twilio_auth_token = os.getenv('TWILIO_AUTH_TOKEN')
twilio_phone_number = os.getenv('TWILIO_NUMBER')
url = f'https://api.twilio.com/2010-04-01/Accounts/{twilio_account_sid}/Messages.json'
payload = {
'Body': data.Body,
'From': twilio_phone_number,
'To': data.To
}
try:
response = requests.post(url=url, data=payload, auth=(twilio_account_sid, twilio_auth_token))
if response.status_code == 201:
return {"message": "SMS sent successfully!"}
else:
return {"message": "Failed to send SMS. Please check the provided phone number."}
except Exception as e:
return {"message": str(e)}
# Graceful shutdown
@app.on_event("shutdown")
async def shutdown_event():
print("Shutting down gracefully...")
# Perform any additional cleanup here if needed
# For example, closing database connections or other resources