Skip to content

Commit

Permalink
use get_route_url instead of url_for
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderGi committed Jul 11, 2024
1 parent ae1abcb commit 936aac5
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions routers/twilio_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
from app_users.models import AppUser
from bots.models import Conversation, BotIntegration, SavedRun, PublishedRun, Platform
from phonenumber_field.phonenumber import PhoneNumber
from daras_ai_v2 import settings

from furl import furl
from fastapi import APIRouter, Response
from starlette.background import BackgroundTasks
from daras_ai_v2.fastapi_tricks import fastapi_request_urlencoded_body
from daras_ai_v2.fastapi_tricks import fastapi_request_urlencoded_body, get_route_url
import base64

router = APIRouter()
Expand Down Expand Up @@ -87,7 +85,10 @@ def twilio_voice_call(

resp = VoiceResponse()
resp.redirect(
url_for(twilio_voice_call_response, bi_id=bi.id, text=text, audio_url=audio_url)
get_route_url(
twilio_voice_call_response,
dict(bi_id=bi.id, text=text, audio_url=audio_url),
)
)

return Response(str(resp), headers={"Content-Type": "text/xml"})
Expand Down Expand Up @@ -135,7 +136,7 @@ def twilio_voice_call_asked(

resp.enqueue(
name=queue_name,
wait_url=url_for(twilio_voice_call_wait, bi_id=bi.id),
wait_url=get_route_url(twilio_voice_call_wait, dict(bi_id=bi.id)),
wait_url_method="POST",
)

Expand Down Expand Up @@ -182,7 +183,7 @@ def twilio_voice_call_asked_audio(
resp = VoiceResponse()
resp.enqueue(
name=queue_name,
wait_url=url_for(twilio_voice_call_wait, bi_id=bi.id),
wait_url=get_route_url(twilio_voice_call_wait, dict(bi_id=bi.id)),
wait_url_method="POST",
)

Expand Down Expand Up @@ -241,8 +242,9 @@ def twilio_voice_call_respond(
assert queue_sid, "Queue not found"

client.queues(queue_sid).members(call_sid).update(
url=url_for(
twilio_voice_call_response, bi_id=bi.id, text=text, audio_url=audio_url
url=get_route_url(
twilio_voice_call_response,
dict(bi_id=bi.id, text=text, audio_url=audio_url),
),
method="POST",
)
Expand Down Expand Up @@ -274,7 +276,7 @@ def twilio_voice_call_response(bi_id: int, text: str, audio_url: str):
# try recording 3 times to give the user a chance to start speaking
for _ in range(3):
resp.record(
action=url_for(twilio_voice_call_asked_audio),
action=get_route_url(twilio_voice_call_asked_audio),
method="POST",
timeout=3,
finish_on_key="0",
Expand All @@ -285,7 +287,7 @@ def twilio_voice_call_response(bi_id: int, text: str, audio_url: str):
input="speech", # also supports dtmf (keypad input) and a combination of both
timeout=20, # users get 20 to start speaking
speechTimeout=3, # a 3 second pause ends the input
action=url_for(
action=get_route_url(
twilio_voice_call_asked
), # the URL to send the user's question to
method="POST",
Expand Down Expand Up @@ -403,7 +405,10 @@ def start_voice_call_session(
audio_url = base64.b64encode(audio_url.encode()).decode() if audio_url else "N"

resp.redirect(
url_for(twilio_voice_call_response, bi_id=bi.id, text=text, audio_url=audio_url)
get_route_url(
twilio_voice_call_response,
dict(bi_id=bi.id, text=text, audio_url=audio_url),
)
)

call = client.calls.create(
Expand Down Expand Up @@ -463,17 +468,6 @@ def send_sms_message(
return message


def url_for(fx, **kwargs):
"""Get the URL for the given Twilio endpoint."""

return (
furl(
settings.APP_BASE_URL,
)
/ router.url_path_for(fx.__name__, **kwargs)
).tostr()


def twilio_connect(
current_run: SavedRun,
published_run: PublishedRun,
Expand All @@ -489,15 +483,15 @@ def twilio_connect(
client = Client(twilio_account_sid, twilio_auth_token)
client.incoming_phone_numbers(twilio_phone_number_sid).update(
sms_fallback_method="POST",
sms_fallback_url=url_for(twilio_sms_error),
sms_fallback_url=get_route_url(twilio_sms_error),
sms_method="POST",
sms_url=url_for(twilio_sms),
sms_url=get_route_url(twilio_sms),
# status_callback_method="POST", # uncomment for debugging
# status_callback=url_for(twilio_voice_call_status),
# status_callback=get_route_url(twilio_voice_call_status),
voice_fallback_method="POST",
voice_fallback_url=url_for(twilio_voice_call_error),
voice_fallback_url=get_route_url(twilio_voice_call_error),
voice_method="POST",
voice_url=url_for(twilio_voice_call),
voice_url=get_route_url(twilio_voice_call),
)

# create bot integration
Expand Down

0 comments on commit 936aac5

Please sign in to comment.