From b754e6f5c0fa8e41270afb3530ee15bf4b934bd7 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Thu, 28 Oct 2021 19:33:34 -0500 Subject: [PATCH] Switch to the new Ring verb with answerCall=false Should actually cause incoming calls to ring properly, and the bug that required pseudo_call_id seems gone. --- views/pause.slim | 3 --- views/ring.slim | 3 +++ views/voicemail.slim | 4 ++-- web.rb | 54 +++++++++++++------------------------------- 4 files changed, 21 insertions(+), 43 deletions(-) delete mode 100644 views/pause.slim create mode 100644 views/ring.slim diff --git a/views/pause.slim b/views/pause.slim deleted file mode 100644 index e892a58..0000000 --- a/views/pause.slim +++ /dev/null @@ -1,3 +0,0 @@ -doctype xml -Response - Pause duration=duration diff --git a/views/ring.slim b/views/ring.slim new file mode 100644 index 0000000..825dcb7 --- /dev/null +++ b/views/ring.slim @@ -0,0 +1,3 @@ +doctype xml +Response + Ring duration=duration answerCall="false" diff --git a/views/voicemail.slim b/views/voicemail.slim index f3dc348..e3195d7 100644 --- a/views/voicemail.slim +++ b/views/voicemail.slim @@ -5,6 +5,6 @@ Response PlayAudio= "/beep.mp3" Record{ transcribe=transcription_enabled.to_s - recordingAvailableUrl="/inbound/calls/#{pseudo_call_id}/voicemail/audio" - transcriptionAvailableUrl="/inbound/calls/#{pseudo_call_id}/voicemail/transcription" + recordingAvailableUrl="/inbound/calls/#{params['callId']}/voicemail/audio" + transcriptionAvailableUrl="/inbound/calls/#{params['callId']}/voicemail/transcription" fileFormat="mp3"} / diff --git a/web.rb b/web.rb index d303001..5da7aa5 100644 --- a/web.rb +++ b/web.rb @@ -55,11 +55,10 @@ class Web < Roda plugin RodaEMPromise # Must go last! class << self - attr_reader :customer_repo, :log, :true_inbound_call, :outbound_transfers + attr_reader :customer_repo, :log, :outbound_transfers def run(log, *listen_on) plugin :common_logger, log, method: :info - @true_inbound_call = {} @outbound_transfers = {} Thin::Logging.logger = log Thin::Server.start( @@ -71,7 +70,7 @@ class Web < Roda end extend Forwardable - def_delegators :'self.class', :true_inbound_call, :outbound_transfers + def_delegators :'self.class', :outbound_transfers def_delegators :request, :params def log @@ -98,11 +97,6 @@ class Web < Roda end end - def pseudo_call_id - request.captures_hash[:pseudo_call_id] || - Digest::SHA256.hexdigest("#{params['from']},#{params['to']}") - end - TEL_CANDIDATES = { "Restricted" => "14", "anonymous" => "15", @@ -130,7 +124,7 @@ class Web < Roda end def inbound_calls_path(suffix) - ["/inbound/calls/#{pseudo_call_id}", suffix].compact.join("/") + ["/inbound/calls/#{params['callId']}", suffix].compact.join("/") end def url(path) @@ -152,38 +146,29 @@ class Web < Roda r.on "calls" do r.post "status" do if params["eventType"] == "disconnect" - p_call_id = pseudo_call_id - call_id = params["callId"] - EM.promise_timer(2).then { - next unless true_inbound_call[p_call_id] == call_id - - true_inbound_call.delete(p_call_id) - - if (outbound_leg = outbound_transfers.delete(p_call_id)) - modify_call(outbound_leg) do |call| - call.state = "completed" - end + if (outbound_leg = outbound_transfers.delete(params["callId"])) + modify_call(outbound_leg) do |call| + call.state = "completed" end + end - CustomerRepo.new.find_by_tel(params["to"]).then do |customer| - CDR.for_inbound(customer.customer_id, params).save - end - }.catch(&method(:log_error)) + CustomerRepo.new.find_by_tel(params["to"]).then do |customer| + CDR.for_inbound(customer.customer_id, params).save + end end "OK" end - r.on :pseudo_call_id do |pseudo_call_id| + r.on :call_id do |call_id| r.post "transfer_complete" do - outbound_leg = outbound_transfers.delete(pseudo_call_id) + outbound_leg = outbound_transfers.delete(call_id) if params["cause"] == "hangup" log.debug "Normal hangup", loggable_params elsif !outbound_leg log.debug "Inbound disconnected", loggable_params else log.debug "Go to voicemail", loggable_params - true_call_id = true_inbound_call[pseudo_call_id] - modify_call(true_call_id) do |call| + modify_call(call_id) do |call| call.redirect_url = url inbound_calls_path(:voicemail) end end @@ -245,22 +230,15 @@ class Web < Roda end r.post do - true_call_id = true_inbound_call[pseudo_call_id] - render :bridge, locals: { call_id: true_call_id } + render :bridge, locals: { call_id: call_id } end end r.post do - if true_inbound_call[pseudo_call_id] - true_inbound_call[pseudo_call_id] = params["callId"] - return render :pause, locals: { duration: 300 } - end - CustomerRepo.new( sgx_repo: Bwmsgsv2Repo.new ).find_by_tel(params["to"]).then(&:fwd).then do |fwd| call = fwd.create_call(CONFIG[:creds][:account]) { |cc| - true_inbound_call[pseudo_call_id] = params["callId"] cc.from = params["from"] cc.application_id = params["applicationId"] cc.answer_url = url inbound_calls_path(nil) @@ -268,8 +246,8 @@ class Web < Roda } if call - outbound_transfers[pseudo_call_id] = call - render :pause, locals: { duration: 300 } + outbound_transfers[params["callId"]] = call + render :ring, locals: { duration: 300 } else render :redirect, locals: { to: inbound_calls_path(:voicemail) } end -- 2.38.5