Skip to content

Commit

Permalink
Handle new dial (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkie authored Jul 9, 2024
1 parent c0f7d0b commit 17ae194
Show file tree
Hide file tree
Showing 19 changed files with 511 additions and 435 deletions.
2 changes: 1 addition & 1 deletion components/app/app/call_controllers/call_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def build_call_properties

call_serializer = CallSerializer.new(call)

response = call_platform_client.create_call(
response = call_platform_client.create_inbound_call(
to: call.variables.fetch("variable_sip_h_x_somleng_callee_identity"),
from: call.variables.fetch("variable_sip_h_x_somleng_caller_identity"),
external_id: call_serializer.id,
Expand Down
6 changes: 1 addition & 5 deletions components/app/app/models/call_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@
:sip_headers,
:default_tts_voice,
keyword_init: true
) do
def inbound?
direction == "inbound"
end
end
)
80 changes: 38 additions & 42 deletions components/app/app/workflows/execute_dial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,50 @@ class ExecuteDial < ExecuteTwiMLVerb

def call
answer!
dial_status = context.dial(build_dial_strings)
phone_calls = create_outbound_calls
dial_params = build_dial_params(phone_calls)
dial_status = context.dial(dial_params)

return if verb.action.blank?

redirect(build_callback_params(dial_status))
callback_params = build_callback_params(dial_status)
redirect(callback_params)
end

private

def create_outbound_calls
call_platform_client.create_outbound_calls(
destinations: verb.nested_nouns.map { |nested_noun| nested_noun.content.strip },
parent_call_sid: call_properties.call_sid,
from: verb.caller_id
)
end

def build_dial_params(phone_calls)
phone_calls.each_with_object({}) do |phone_call, result|
dial_string, from = build_dial_string(phone_call)

result[dial_string.to_s] = {
from:,
for: verb.timeout.seconds,
headers: SIPHeaders.new(
call_sid: phone_call.sid,
account_sid: phone_call.account_sid
).to_h
}.compact
end
end

def build_dial_string(phone_call_response)
if phone_call_response.address.present?
DialString.new(address: phone_call_response.address)
else
dial_string = DialString.new(phone_call_response.routing_parameters)
[ dial_string, dial_string.format_number(phone_call_response.from) ]
end
end

def redirect(params)
throw(
:redirect,
Expand Down Expand Up @@ -47,44 +83,4 @@ def find_joined_call(dial_status)
return outbound_call if join_status.result == :joined
end
end

def build_dial_strings
verb.nested_nouns.each_with_object(Hash.new({})) do |nested_noun, result|
dial_string, from = build_dial_string(nested_noun)

result[dial_string.to_s] = { from:, for: verb.timeout.seconds }.compact
end
end

def build_dial_string(nested_noun)
dial_content = nested_noun.content.strip

if dial_to_number?(nested_noun)
dial_string = DialString.new(build_routing_parameters(dial_content))
[ dial_string, dial_string.format_number(caller_id) ]
elsif dial_to_sip?(nested_noun)
DialString.new(address: dial_content.delete_prefix("sip:"))
end
end

def dial_to_number?(nested_noun)
nested_noun.text? || nested_noun.name == "Number"
end

def dial_to_sip?(nested_noun)
nested_noun.name == "Sip"
end

def caller_id
return verb.caller_id if verb.caller_id.present?

call_properties.inbound? ? call_properties.from : call_properties.to
end

def build_routing_parameters(number)
call_platform_client.build_routing_parameters(
phone_number: number,
account_sid: call_properties.account_sid
)
end
end
31 changes: 26 additions & 5 deletions components/app/lib/call_platform/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class InvalidRequestError < StandardError; end
keyword_init: true
)

OutboundPhoneCallResponse = Struct.new(
:sid,
:from,
:account_sid,
:parent_call_sid,
:routing_parameters,
:address,
keyword_init: true
)

RecordingResponse = Struct.new(
:id,
:url,
Expand Down Expand Up @@ -49,11 +59,7 @@ def notify_media_stream_event(params)
notify_request("/services/media_stream_events", params)
end

def build_routing_parameters(params)
make_request("/services/routing_parameters", params: params)
end

def create_call(params)
def create_inbound_call(params)
json_response = make_request("/services/inbound_phone_calls", params: params)
InboundPhoneCallResponse.new(
voice_url: json_response.fetch("voice_url"),
Expand All @@ -70,6 +76,21 @@ def create_call(params)
)
end

def create_outbound_calls(params)
json_response = make_request("/services/outbound_phone_calls", params: params.compact)

json_response.fetch("phone_calls").map do |phone_call_response|
OutboundPhoneCallResponse.new(
sid: phone_call_response.fetch("sid"),
parent_call_sid: phone_call_response.fetch("parent_call_sid"),
account_sid: phone_call_response.fetch("account_sid"),
from: phone_call_response.fetch("from"),
routing_parameters: phone_call_response.fetch("routing_parameters"),
address: phone_call_response.fetch("address")
)
end
end

def create_recording(params)
json_response = make_request("/services/recordings", params:)
RecordingResponse.new(
Expand Down
2 changes: 1 addition & 1 deletion components/app/lib/call_platform/fake_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def audio_file_url
ConnectTestNumberWithTwiMLResponse.new(number: "2222")
].freeze

def create_call(params)
def create_inbound_call(params)
validate_gateway_headers(params)

test_number = find_test_number(params.fetch(:to))
Expand Down
Loading

0 comments on commit 17ae194

Please sign in to comment.