-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
161 lines (142 loc) · 6.11 KB
/
app.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import random
from sarufi import Sarufi
from dotenv import load_dotenv
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import Response
from mangum import Mangum
# Load environment variables
load_dotenv()
# Initialize Sarufi and get the bot
sarufi = Sarufi(api_key=os.getenv("SARUFI_API_KEY"))
sarufi_bot = sarufi.get_bot(os.getenv("SARUFI_BOT_ID"))
app = FastAPI()
handler = Mangum(app)
@app.post("/")
async def voicemail(request: Request):
"""
Handle incoming voice call requests.
Parameters:
- request: FastAPI Request object containing the incoming call data.
Returns:
- FastAPI Response object with appropriate XML response.
"""
try:
form_data = await request.form()
session_id = form_data.get("sessionId")
call_number = form_data.get("callerNumber")
is_active = int(form_data.get("isActive"))
dtfm_digits = form_data.get("dtmfDigits", None)
sarufi_chat_id = f"{call_number}-{session_id}"
if dtfm_digits:
dtfm_digits = int(dtfm_digits.strip())
if is_active == 1:
# If the call is active, respond with a message and play an audio file
sarufi_response = (
sarufi_bot.respond("start", chat_id=sarufi_chat_id, channel="whatsapp")
if not dtfm_digits
else sarufi_bot.respond(dtfm_digits, chat_id=sarufi_chat_id, channel="whatsapp")
)
print(sarufi_response)
next_state = sarufi_response.get("next_state")
if sarufi_response.get("actions"):
actions = sarufi_response.get("actions")
for action in actions:
if action.get("send_audios"):
s_response = action.get("send_audios")
url_obj = random.choice(s_response)
if url_obj.get("link"):
s_response = url_obj.get("link")
if s_response:
if next_state != "end":
response = VoiceResponse.get_digits(
s_response, action="play"
)
else:
response = VoiceResponse.play(s_response)
template = (
'<?xml version="1.0" encoding="UTF-8"?>'
"<Response>"
f"{response}"
"</Response>"
)
response = template.format(response=response)
print(response)
return Response(
content=response, media_type="application/xml"
)
print("No audio link found 1")
print("No audio link found 2")
if action.get("send_message"):
s_response = action.get("send_message")
s_response = ".".join(s_response)
if s_response and s_response != ".":
if next_state != "end":
response = VoiceResponse.get_digits(s_response)
else:
response = VoiceResponse.say(s_response)
template = (
'<?xml version="1.0" encoding="UTF-8"?>'
"<Response>"
f"{response}"
"</Response>"
)
response = template.format(response=response)
return Response(
content=response, media_type="application/xml"
)
else:
# If the call is not active, read in call details
duration = form_data["durationInSeconds"]
currency_code = form_data["currencyCode"]
amount = form_data["amount"]
print("Call duration is {} seconds".format(duration))
print("The currency code is {}".format(currency_code))
print("The amount received is {}".format(amount))
print("The session ID is {}".format(session_id))
print("The call has ended")
# You can store this information in the database for your records
return ""
except Exception as e:
# Log any unexpected exceptions and return an error response
print(f"Error: {str(e)}")
raise HTTPException(status_code=500, detail="Internal Server Error")
class VoiceResponse:
"""
Class to handle voice response.
"""
@staticmethod
def say(response):
return f"<Say>{response}</Say>"
@staticmethod
def play(response):
return f'<Play url="{response}" />'
@staticmethod
def get_digits(response, timeout=20, finishOnKey="#", action="say"):
if action == "play":
action = f'<Play url="{response}" />'
return f"""
<GetDigits timeout="{timeout}" finishOnKey="{finishOnKey}">
{action}
</GetDigits>
"""
return f"""
<GetDigits timeout="{timeout}" finishOnKey="{finishOnKey}">
<Say>{response}</Say>
</GetDigits>
"""
@staticmethod
def partial_record(
response, trim_silence=True, play_beep=True, finishOnKey="#", maxLength=10
):
return f"""
<Record finishOnKey="{finishOnKey}" maxLength="{maxLength}" trimSilence="{trim_silence}" playBeep="{play_beep}">
<Say>{response}</Say>
</Record>
"""
@staticmethod
def terminal_record(response, play_beep=True):
return f"""
<Say playBeep="{play_beep}">{response}</Say>
<Record />
"""